Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kirkbushell/laravel-doctrine into…
Browse files Browse the repository at this point in the history
… kirkbushell-master

Conflicts:
	src/Filters/TrashedFilter.php
	src/LaravelDoctrineServiceProvider.php
  • Loading branch information
mitchellvanw committed Sep 22, 2014
2 parents 4dbfd73 + 6ef07ce commit ff22c8c
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 141 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
composer.phar
composer.lock
.DS_Store
.idea/*
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
"doctrine/orm": "2.5.*",
"doctrine/migrations": "1.*"
},
"require-dev": {
"mockery/mockery": "dev-master",
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-4": {
"Mitch\\LaravelDoctrine\\": "src/"
"Mitch\\LaravelDoctrine\\": "src/",
"Tests\\": "tests/"
}
},
"minimum-stability": "dev"
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="all">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
40 changes: 40 additions & 0 deletions src/Configuration/DriverMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php namespace Mitch\LaravelDoctrine\Configuration;

class DriverMapper
{
/**
* An array of mappers that can be cycled through to determine which mapper
* is appropriate for a given configuration arrangement.
*
* @var array
*/
private $configurationMappers = [];

/**
* Register a new driver configuration mapper.
*
* @param Mapper $mapper
*/
public function registerMapper(Mapper $mapper)
{
$this->configurationMappers[] = $mapper;
}

/**
* Map the Laravel configuration to a configuration driver, return the result.
*
* @param $configuration
* @return array
* @throws \Exception
*/
public function map($configuration)
{
foreach ($this->configurationMappers as $mapper) {
if ($mapper->isAppropriate($configuration)) {
return $mapper->map($configuration);
}
}

throw new \Exception("Driver {$configuration['driver']} unsupported by package at this time.");
}
}
20 changes: 20 additions & 0 deletions src/Configuration/Mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace Mitch\LaravelDoctrine\Configuration;

interface Mapper
{
/**
* Handles the mapping of configuration.
*
* @param array $configuration
* @return mixed
*/
public function map(array $configuration);

/**
* Determines whether the configuration array is appropriate for the mapper.
*
* @param array $configuration
* @return mixed
*/
public function isAppropriate(array $configuration);
}
46 changes: 46 additions & 0 deletions src/Configuration/SqlConfigurationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php namespace Mitch\LaravelDoctrine\Configuration;

class SqlConfigurationMapper implements Mapper
{
/**
* Creates the configuration mapping for SQL database engines, including SQL server, MySQL and PostgreSQL.
*
* @param array $configuration
* @return array
*/
public function map(array $configuration)
{
return [
'driver' => $this->driver($configuration['driver']),
'host' => $configuration['host'],
'dbname' => $configuration['database'],
'user' => $configuration['username'],
'password' => $configuration['password'],
'charset' => $configuration['charset']
];
}

/**
* Is suitable for mapping configurations that use a mysql, postgres or sqlserv setup.
*
* @param array $configuration
* @return boolean
*/
public function isAppropriate(array $configuration)
{
return in_array($configuration['driver'], ['sqlsrv', 'mysql', 'pgsql']);
}

/**
* Maps the Laravel driver syntax to an Sql doctrine format.
*
* @param $l4Driver
* @return string
*/
public function driver($l4Driver)
{
$doctrineDrivers = ['mysql' => 'pdo_mysql', 'sqlsrv' => 'pdo_sqlsrv', 'pgsql' => 'pdo_pgsql'];

return $doctrineDrivers[$l4Driver];
}
}
50 changes: 50 additions & 0 deletions src/Configuration/SqliteConfigurationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php namespace Mitch\LaravelDoctrine\Configuration;

class SqliteConfigurationMapper implements Mapper
{
/**
* Map the L4 configuration array to a sqlite-friendly doctrine configuration.
*
* @param array $configuration
* @return array
*/
public function map(array $configuration)
{
$sqliteConfig = [
'driver' => 'pdo_sqlite',
'user' => @$configuration['username'],
'password' => @$configuration['password']
];

$this->databaseLocation($configuration, $sqliteConfig);

return $sqliteConfig;
}

/**
* Is only suitable for sqlite configuration mapping.
*
* @param array $configuration
* @return bool
*/
public function isAppropriate(array $configuration)
{
return $configuration['driver'] == 'sqlite';
}

/**
* Determines the location of the database and appends this to the sqlite configuration.
*
* @param $configuration
* @param $sqliteConfig
*/
private function databaseLocation($configuration, &$sqliteConfig)
{
if ($configuration['database'] == ':memory:') {
$sqliteConfig['memory'] = true;
}
else {
$sqliteConfig['path'] = app_path('database').'/'.$configuration['database'].'.sqlite';
}
}
}
Loading

0 comments on commit ff22c8c

Please sign in to comment.