Skip to content

Commit d18def8

Browse files
committed
Added new tests and fixed errors in commands
http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html states that a default entity manager should be created An em option has been added to commands
1 parent c5f1e30 commit d18def8

17 files changed

+673
-144
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
"require": {
1414
"php": ">=5.5.0",
1515
"illuminate/support": "4.*|5.*",
16-
"illuminate/container": "4.*|5.*",
1716
"doctrine/orm": "2.5.*",
1817
"doctrine/migrations": "1.*"
1918
},
2019
"require-dev": {
20+
"illuminate/container": "4.*|5.*",
21+
"illuminate/console": "4.*|5.*",
2122
"mockery/mockery": "dev-master",
2223
"phpunit/phpunit": "3.7.*"
2324
},

src/Console/GenerateProxiesCommand.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Console\Command;
44
use Doctrine\Common\Persistence\ManagerRegistry;
5+
use Symfony\Component\Console\Input\InputOption;
56

67
class GenerateProxiesCommand extends Command
78
{
@@ -36,24 +37,35 @@ public function __construct(ManagerRegistry $registry)
3637
public function fire()
3738
{
3839
$this->info('Starting proxy generation....');
39-
foreach ($this->registry->getManagerNames() as $key => $value) {
40-
$manager = $this->registry->getManager($key);
41-
$metadata = $manager->getMetadataFactory()->getAllMetadata();
42-
if (empty($metadata)) {
43-
$this->error('No metadata found to generate any entities.');
44-
exit;
45-
}
46-
$directory = $this->laravel['config']['doctrine::doctrine.proxy.directory'];
47-
if ( ! $directory) {
48-
$this->error('The proxy directory has not been set.');
49-
exit;
50-
}
51-
$this->info('Processing entities:');
52-
foreach ($metadata as $item) {
53-
$this->line($item->name);
54-
}
55-
$manager->getProxyFactory()->generateProxyClasses($metadata, $directory);
56-
$this->info('Proxies have been created.');
40+
41+
if ($this->option('em')) {
42+
$manager = $this->registry->getManager($this->option('em'));
43+
} else {
44+
$manager = $this->registry->getManager();
45+
}
46+
47+
$metadata = $manager->getMetadataFactory()->getAllMetadata();
48+
if (empty($metadata)) {
49+
$this->error('No metadata found to generate any entities.');
50+
exit;
51+
}
52+
$directory = $manager->getConfiguration()->getProxyDir();
53+
if (! $directory) {
54+
$this->error('The proxy directory has not been set.');
55+
exit;
5756
}
57+
$this->info('Processing entities:');
58+
foreach ($metadata as $item) {
59+
$this->line($item->name);
60+
}
61+
$manager->getProxyFactory()->generateProxyClasses($metadata, $directory);
62+
$this->info('Proxies have been created.');
63+
}
64+
65+
protected function getOptions()
66+
{
67+
return [
68+
['em', false, InputOption::VALUE_REQUIRED, 'Sets the entity manager when the default is not desired.'],
69+
];
5870
}
5971
}

src/Console/SchemaCreateCommand.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,17 @@ class SchemaCreateCommand extends Command
2121
*/
2222
protected $description = 'Create database schema from models';
2323

24-
/**
25-
* The schema tool.
26-
*
27-
* @var \Doctrine\ORM\Tools\SchemaTool
28-
*/
29-
private $tool;
30-
3124
/**
3225
* The ManagerRegistry
3326
*
3427
* @var \Doctrine\Common\Persistence\ManagerRegistry
3528
*/
3629
private $registry;
3730

38-
public function __construct(SchemaTool $tool, ManagerRegistry $registry)
31+
public function __construct(ManagerRegistry $registry)
3932
{
4033
parent::__construct();
4134

42-
$this->tool = $tool;
4335
$this->registry = $registry;
4436
}
4537

@@ -50,27 +42,30 @@ public function __construct(SchemaTool $tool, ManagerRegistry $registry)
5042
*/
5143
public function fire()
5244
{
45+
if ($this->option('em')) {
46+
$manager = $this->registry->getManager($this->option('em'));
47+
} else {
48+
$manager = $this->registry->getManager();
49+
}
50+
51+
$tool = new SchemaTool($manager);
52+
5353
if ($this->option('sql')) {
5454
$this->info('Outputting create query:'.PHP_EOL);
55-
foreach ($this->registry->getManagerNames() as $key => $value) {
56-
$manager = $this->registry->getManager($key);
57-
$sql = $this->tool->getCreateSchemaSql($manager->getMetadataFactory()->getAllMetadata());
58-
$this->info(implode(';'.PHP_EOL, $sql));
59-
}
55+
$sql = $tool->getCreateSchemaSql($manager->getMetadataFactory()->getAllMetadata());
56+
$this->info(implode(';'.PHP_EOL, $sql));
6057
} else {
6158
$this->info('Creating database schema...');
62-
foreach ($this->registry->getManagerNames() as $key => $value) {
63-
$manager = $this->registry->getManager($key);
64-
$this->tool->createSchema($manager->getMetadataFactory()->getAllMetadata());
65-
}
59+
$tool->createSchema($manager->getMetadataFactory()->getAllMetadata());
6660
$this->info('Schema has been created!');
6761
}
6862
}
6963

7064
protected function getOptions()
7165
{
7266
return [
73-
['sql', false, InputOption::VALUE_NONE, 'Dumps SQL query and does not execute creation.']
67+
['sql', false, InputOption::VALUE_NONE, 'Dumps SQL query and does not execute creation.'],
68+
['em', false, InputOption::VALUE_REQUIRED, 'Sets the entity manager when the default is not desired.'],
7469
];
7570
}
7671
}

src/Console/SchemaDropCommand.php

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,17 @@ class SchemaDropCommand extends Command
2121
*/
2222
protected $description = 'Drop database schema';
2323

24-
/**
25-
* The schema tool.
26-
*
27-
* @var \Doctrine\ORM\Tools\SchemaTool
28-
*/
29-
private $tool;
30-
3124
/**
3225
* The ManagerRegistry
3326
*
3427
* @var \Doctrine\Common\Persistence\ManagerRegistry
3528
*/
3629
private $registry;
3730

38-
public function __construct(SchemaTool $tool, ManagerRegistry $registry)
31+
public function __construct(ManagerRegistry $registry)
3932
{
4033
parent::__construct();
4134

42-
$this->tool = $tool;
4335
$this->registry = $registry;
4436
}
4537

@@ -50,28 +42,35 @@ public function __construct(SchemaTool $tool, ManagerRegistry $registry)
5042
*/
5143
public function fire()
5244
{
53-
foreach ($this->registry->getManagerNames() as $key => $value) {
54-
$manager = $this->registry->getManager($key);
55-
$sql = $this->tool->getDropSchemaSQL($manager->getMetadataFactory()->getAllMetadata());
56-
if (empty($sql)) {
57-
$this->info('Current models do not exist in schema.');
58-
continue;
59-
}
60-
if ($this->option('sql')) {
61-
$this->info('Outputting drop query:');
62-
$this->info(implode(';' . PHP_EOL, $sql));
63-
} else {
64-
$this->info('Dropping database schema....');
65-
$this->tool->dropSchema($manager->getMetadataFactory()->getAllMetadata());
66-
$this->info('Schema has been dropped!');
67-
}
45+
if ($this->option('em')) {
46+
$manager = $this->registry->getManager($this->option('em'));
47+
} else {
48+
$manager = $this->registry->getManager();
49+
}
50+
51+
$tool = new SchemaTool($manager);
52+
53+
$sql = $tool->getDropSchemaSQL($manager->getMetadataFactory()->getAllMetadata());
54+
if (empty($sql)) {
55+
$this->info('Current models do not exist in schema.');
56+
exit;
57+
}
58+
if ($this->option('sql')) {
59+
$this->info('Outputting drop query:');
60+
$sql = $tool->getDropSchemaSQL($manager->getMetadataFactory()->getAllMetadata());
61+
$this->info(implode(';' . PHP_EOL, $sql));
62+
} else {
63+
$this->info('Dropping database schema....');
64+
$tool->dropSchema($manager->getMetadataFactory()->getAllMetadata());
65+
$this->info('Schema has been dropped!');
6866
}
6967
}
7068

7169
protected function getOptions()
7270
{
7371
return [
7472
['sql', false, InputOption::VALUE_NONE, 'Dumps SQL query and does not execute drop.'],
73+
['em', false, InputOption::VALUE_REQUIRED, 'Sets the entity manager when the default is not desired.'],
7574
];
7675
}
7776
}

src/Console/SchemaUpdateCommand.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ class SchemaUpdateCommand extends Command
3535
*/
3636
private $registry;
3737

38-
public function __construct(SchemaTool $tool, ManagerRegistry $registry)
38+
public function __construct(ManagerRegistry $registry)
3939
{
4040
parent::__construct();
4141

42-
$this->tool = $tool;
4342
$this->registry = $registry;
4443
}
4544

@@ -52,29 +51,37 @@ public function fire()
5251
{
5352
$this->info('Checking if database needs updating....');
5453
$clean = $this->option('clean');
55-
foreach ($this->registry->getManagerNames() as $key => $value) {
56-
$manager = $this->registry->getManager($key);
57-
$sql = $this->tool->getUpdateSchemaSql($manager->getMetadataFactory()->getAllMetadata(), $clean);
58-
if (empty($sql)) {
59-
$this->info('No updates found.');
60-
continue;
61-
}
62-
if ($this->option('sql')) {
63-
$this->info('Outputting update query:');
64-
$this->info(implode(';' . PHP_EOL, $sql));
65-
} else {
66-
$this->info('Updating database schema....');
67-
$this->tool->updateSchema($this->metadata->getAllMetadata());
68-
$this->info('Schema has been updated!');
69-
}
54+
55+
if ($this->option('em')) {
56+
$manager = $this->registry->getManager($this->option('em'));
57+
} else {
58+
$manager = $this->registry->getManager();
59+
}
60+
61+
$tool = new SchemaTool($manager);
62+
63+
$sql = $tool->getUpdateSchemaSql($manager->getMetadataFactory()->getAllMetadata(), $clean);
64+
65+
if (empty($sql)) {
66+
$this->info('No updates found.');
67+
exit;
68+
}
69+
if ($this->option('sql')) {
70+
$this->info('Outputting update query:');
71+
$this->info(implode(';' . PHP_EOL, $sql));
72+
} else {
73+
$this->info('Updating database schema....');
74+
$tool->updateSchema($manager->getMetadataFactory()->getAllMetadata());
75+
$this->info('Schema has been updated!');
7076
}
7177
}
7278

7379
protected function getOptions()
7480
{
7581
return [
7682
['sql', false, InputOption::VALUE_NONE, 'Dumps SQL query and does not execute update.'],
77-
['clean', null, InputOption::VALUE_OPTIONAL, 'When using clean model all non-relevant to this metadata assets will be cleared.']
83+
['clean', null, InputOption::VALUE_OPTIONAL, 'When using clean model all non-relevant to this metadata assets will be cleared.'],
84+
['em', false, InputOption::VALUE_REQUIRED, 'Sets the entity manager when the default is not desired.'],
7885
];
7986
}
8087
}

src/IlluminateRegistry.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ final class IlluminateRegistry extends AbstractManagerRegistry
1414
public function __construct(
1515
Container $container,
1616
array $connections,
17-
array $entityManagers,
18-
$defaultConnection,
19-
$defaultEntityManager
17+
array $entityManagers
2018
) {
2119
$this->container = $container;
2220
parent::__construct(
2321
'ORM',
2422
$connections,
2523
$entityManagers,
26-
$defaultConnection,
27-
$defaultEntityManager,
24+
'default',
25+
'default',
2826
'Doctrine\ORM\Proxy\Proxy'
2927
);
3028
}

src/LaravelDoctrineServiceProvider.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ private function createManagerInstances($config, $databaseConnections, $debug, C
129129
{
130130
$registryConnections = [];
131131
$registryManagers = [];
132-
$defaults = ['connection' => null, 'entityManager' => null];
133132

134133
$proxyNamespace = isset($config['proxy']['namespace']) ? $config['proxy']['namespace'] : null;
135134

@@ -144,11 +143,6 @@ private function createManagerInstances($config, $databaseConnections, $debug, C
144143
continue;
145144
}
146145

147-
if ($connectionName === $config['default_connection']) {
148-
$defaults['connection'] = $connectionName;
149-
$defaults['entityManager'] = $name;
150-
}
151-
152146
$databaseConfig = $databaseConnections[$connectionName];
153147
$cacheProvider = isset($managerConfig['cache_provider']) ? $managerConfig['cache_provider'] : $config['cache_provider'];
154148
$repository = isset($managerConfig['repository']) ? $managerConfig['repository'] : $config['repository'];
@@ -182,9 +176,17 @@ private function createManagerInstances($config, $databaseConnections, $debug, C
182176
$this->app->instance($registryConnections[$connectionName], $connection);
183177
$this->app->instance($registryManagers[$name], $entityManager);
184178

179+
if ($connectionName === $config['default_connection']) {
180+
$registryConnections['default'] = 'doctrine.dbal.default_connection';
181+
$registryManagers['default'] = 'doctrine.orm.default_entity_manager';
182+
183+
$this->app->instance('doctrine.dbal.default_connection', $connection);
184+
$this->app->instance('doctrine.orm.default_entity_manager', $entityManager);
185+
}
186+
185187
}
186188

187-
return [$registryConnections, $registryManagers, $defaults];
189+
return [$registryConnections, $registryManagers];
188190
}
189191

190192
private function registerManagerRegistry()
@@ -196,7 +198,7 @@ private function registerManagerRegistry()
196198

197199
$config = $this->mapEntityManagers($config, $defaultDatabase);
198200

199-
list($registryConnections, $registryManagers, $defaults) = $this->createManagerInstances(
201+
list($registryConnections, $registryManagers) = $this->createManagerInstances(
200202
$config,
201203
$databaseConnections,
202204
$app['config']['app.debug'],
@@ -206,9 +208,7 @@ private function registerManagerRegistry()
206208
return new IlluminateRegistry(
207209
$app,
208210
$registryConnections,
209-
$registryManagers,
210-
$defaults['connection'],
211-
$defaults['entityManager']
211+
$registryManagers
212212
);
213213
});
214214
$this->app->singleton(ManagerRegistry::class, IlluminateRegistry::class);

0 commit comments

Comments
 (0)