Skip to content

Commit ff22c8c

Browse files
committed
Merge branch 'master' of github.com:kirkbushell/laravel-doctrine into kirkbushell-master
Conflicts: src/Filters/TrashedFilter.php src/LaravelDoctrineServiceProvider.php
2 parents 4dbfd73 + 6ef07ce commit ff22c8c

13 files changed

+448
-141
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
composer.phar
44
composer.lock
55
.DS_Store
6+
.idea/*

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
"doctrine/orm": "2.5.*",
1717
"doctrine/migrations": "1.*"
1818
},
19+
"require-dev": {
20+
"mockery/mockery": "dev-master",
21+
"phpunit/phpunit": "3.7.*"
22+
},
1923
"autoload": {
2024
"psr-4": {
21-
"Mitch\\LaravelDoctrine\\": "src/"
25+
"Mitch\\LaravelDoctrine\\": "src/",
26+
"Tests\\": "tests/"
2227
}
2328
},
2429
"minimum-stability": "dev"

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="all">
15+
<directory suffix="Test.php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/Configuration/DriverMapper.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace Mitch\LaravelDoctrine\Configuration;
2+
3+
class DriverMapper
4+
{
5+
/**
6+
* An array of mappers that can be cycled through to determine which mapper
7+
* is appropriate for a given configuration arrangement.
8+
*
9+
* @var array
10+
*/
11+
private $configurationMappers = [];
12+
13+
/**
14+
* Register a new driver configuration mapper.
15+
*
16+
* @param Mapper $mapper
17+
*/
18+
public function registerMapper(Mapper $mapper)
19+
{
20+
$this->configurationMappers[] = $mapper;
21+
}
22+
23+
/**
24+
* Map the Laravel configuration to a configuration driver, return the result.
25+
*
26+
* @param $configuration
27+
* @return array
28+
* @throws \Exception
29+
*/
30+
public function map($configuration)
31+
{
32+
foreach ($this->configurationMappers as $mapper) {
33+
if ($mapper->isAppropriate($configuration)) {
34+
return $mapper->map($configuration);
35+
}
36+
}
37+
38+
throw new \Exception("Driver {$configuration['driver']} unsupported by package at this time.");
39+
}
40+
}

src/Configuration/Mapper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace Mitch\LaravelDoctrine\Configuration;
2+
3+
interface Mapper
4+
{
5+
/**
6+
* Handles the mapping of configuration.
7+
*
8+
* @param array $configuration
9+
* @return mixed
10+
*/
11+
public function map(array $configuration);
12+
13+
/**
14+
* Determines whether the configuration array is appropriate for the mapper.
15+
*
16+
* @param array $configuration
17+
* @return mixed
18+
*/
19+
public function isAppropriate(array $configuration);
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace Mitch\LaravelDoctrine\Configuration;
2+
3+
class SqlConfigurationMapper implements Mapper
4+
{
5+
/**
6+
* Creates the configuration mapping for SQL database engines, including SQL server, MySQL and PostgreSQL.
7+
*
8+
* @param array $configuration
9+
* @return array
10+
*/
11+
public function map(array $configuration)
12+
{
13+
return [
14+
'driver' => $this->driver($configuration['driver']),
15+
'host' => $configuration['host'],
16+
'dbname' => $configuration['database'],
17+
'user' => $configuration['username'],
18+
'password' => $configuration['password'],
19+
'charset' => $configuration['charset']
20+
];
21+
}
22+
23+
/**
24+
* Is suitable for mapping configurations that use a mysql, postgres or sqlserv setup.
25+
*
26+
* @param array $configuration
27+
* @return boolean
28+
*/
29+
public function isAppropriate(array $configuration)
30+
{
31+
return in_array($configuration['driver'], ['sqlsrv', 'mysql', 'pgsql']);
32+
}
33+
34+
/**
35+
* Maps the Laravel driver syntax to an Sql doctrine format.
36+
*
37+
* @param $l4Driver
38+
* @return string
39+
*/
40+
public function driver($l4Driver)
41+
{
42+
$doctrineDrivers = ['mysql' => 'pdo_mysql', 'sqlsrv' => 'pdo_sqlsrv', 'pgsql' => 'pdo_pgsql'];
43+
44+
return $doctrineDrivers[$l4Driver];
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php namespace Mitch\LaravelDoctrine\Configuration;
2+
3+
class SqliteConfigurationMapper implements Mapper
4+
{
5+
/**
6+
* Map the L4 configuration array to a sqlite-friendly doctrine configuration.
7+
*
8+
* @param array $configuration
9+
* @return array
10+
*/
11+
public function map(array $configuration)
12+
{
13+
$sqliteConfig = [
14+
'driver' => 'pdo_sqlite',
15+
'user' => @$configuration['username'],
16+
'password' => @$configuration['password']
17+
];
18+
19+
$this->databaseLocation($configuration, $sqliteConfig);
20+
21+
return $sqliteConfig;
22+
}
23+
24+
/**
25+
* Is only suitable for sqlite configuration mapping.
26+
*
27+
* @param array $configuration
28+
* @return bool
29+
*/
30+
public function isAppropriate(array $configuration)
31+
{
32+
return $configuration['driver'] == 'sqlite';
33+
}
34+
35+
/**
36+
* Determines the location of the database and appends this to the sqlite configuration.
37+
*
38+
* @param $configuration
39+
* @param $sqliteConfig
40+
*/
41+
private function databaseLocation($configuration, &$sqliteConfig)
42+
{
43+
if ($configuration['database'] == ':memory:') {
44+
$sqliteConfig['memory'] = true;
45+
}
46+
else {
47+
$sqliteConfig['path'] = app_path('database').'/'.$configuration['database'].'.sqlite';
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)