|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saxulum\DoctrineOrmManagerRegistry\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Pimple\Container; |
|
7
|
|
|
use Pimple\ServiceProviderInterface; |
|
8
|
|
|
use Saxulum\DoctrineOrmManagerRegistry\Doctrine\ManagerRegistry; |
|
9
|
|
|
use Saxulum\DoctrineOrmCommands\Command\CreateDatabaseDoctrineCommand; |
|
10
|
|
|
use Saxulum\DoctrineOrmCommands\Command\DropDatabaseDoctrineCommand; |
|
11
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\ClearMetadataCacheDoctrineCommand; |
|
12
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\ClearQueryCacheDoctrineCommand; |
|
13
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\ClearResultCacheDoctrineCommand; |
|
14
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\ConvertMappingDoctrineCommand; |
|
15
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\CreateSchemaDoctrineCommand; |
|
16
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\DropSchemaDoctrineCommand; |
|
17
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\EnsureProductionSettingsDoctrineCommand; |
|
18
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\InfoDoctrineCommand; |
|
19
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\RunDqlDoctrineCommand; |
|
20
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\RunSqlDoctrineCommand; |
|
21
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\UpdateSchemaDoctrineCommand; |
|
22
|
|
|
use Saxulum\DoctrineOrmCommands\Command\Proxy\ValidateSchemaCommand; |
|
23
|
|
|
use Saxulum\DoctrineOrmCommands\Helper\ManagerRegistryHelper; |
|
24
|
|
|
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; |
|
25
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; |
|
26
|
|
|
use Symfony\Bridge\Doctrine\Validator\DoctrineInitializer; |
|
27
|
|
|
use Symfony\Component\Console\Application as ConsoleApplication; |
|
28
|
|
|
|
|
29
|
|
|
class DoctrineOrmManagerRegistryProvider implements ServiceProviderInterface |
|
30
|
|
|
{ |
|
31
|
|
|
public function register(Container $container) |
|
32
|
|
|
{ |
|
33
|
|
|
$container['doctrine'] = function ($container) { |
|
34
|
|
|
return new ManagerRegistry($container); |
|
35
|
|
|
}; |
|
36
|
|
|
|
|
37
|
|
|
if (!isset($container['orm.ems.factory'])) { |
|
38
|
|
|
$container['orm.ems.factory'] = function (Container $container) { |
|
39
|
|
|
$container['orm.ems.options.initializer'](); |
|
40
|
|
|
$factory = new Container(); |
|
41
|
|
|
foreach ($container['orm.ems.options'] as $name => $options) { |
|
42
|
|
|
if ($container['orm.ems.default'] === $name) { |
|
43
|
|
|
// we use shortcuts here in case the default has been overridden |
|
44
|
|
|
$config = $container['orm.em.config']; |
|
45
|
|
|
} else { |
|
46
|
|
|
$config = $container['orm.ems.config'][$name]; |
|
47
|
|
|
} |
|
48
|
|
|
$factory[$name] = $factory->protect( |
|
49
|
|
|
function () use ($container, $options, $config) { |
|
50
|
|
|
return EntityManager::create( |
|
51
|
|
|
$container['dbs'][$options['connection']], |
|
52
|
|
|
$config, |
|
53
|
|
|
$container['dbs.event_manager'][$options['connection']] |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
return $factory; |
|
59
|
|
|
}; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
if (isset($container['form.extensions']) && class_exists('Symfony\\Bridge\\Doctrine\\Form\\DoctrineOrmExtension')) { |
|
64
|
|
|
$container['form.extensions'] = $container->extend('form.extensions', function ($extensions, $container) { |
|
65
|
|
|
$extensions[] = new DoctrineOrmExtension($container['doctrine']); |
|
66
|
|
|
|
|
67
|
|
|
return $extensions; |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (isset($container['validator']) && class_exists('Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\UniqueEntityValidator')) { |
|
72
|
|
|
$container['doctrine.orm.validator.unique_validator'] = function ($container) { |
|
73
|
|
|
return new UniqueEntityValidator($container['doctrine']); |
|
74
|
|
|
}; |
|
75
|
|
|
|
|
76
|
|
|
if (!isset($container['validator.validator_service_ids'])) { |
|
77
|
|
|
$container['validator.validator_service_ids'] = array(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$container['validator.validator_service_ids'] = array_merge( |
|
81
|
|
|
$container['validator.validator_service_ids'], |
|
82
|
|
|
array('doctrine.orm.validator.unique' => 'doctrine.orm.validator.unique_validator') |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$container['validator.object_initializers'] = $container->extend('validator.object_initializers', |
|
86
|
|
|
function (array $objectInitializers) use ($container) { |
|
87
|
|
|
$objectInitializers[] = new DoctrineInitializer($container['doctrine']); |
|
88
|
|
|
|
|
89
|
|
|
return $objectInitializers; |
|
90
|
|
|
} |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (class_exists('Saxulum\\DoctrineOrmCommands\\Command\\CreateDatabaseDoctrineCommand')) { |
|
95
|
|
|
if (isset($container['console'])) { |
|
96
|
|
|
$container['console'] = $container->extend('console', function (ConsoleApplication $consoleApplication) use ($container) { |
|
97
|
|
|
$helperSet = $consoleApplication->getHelperSet(); |
|
98
|
|
|
$helperSet->set(new ManagerRegistryHelper($container['doctrine']), 'doctrine'); |
|
99
|
|
|
|
|
100
|
|
|
return $consoleApplication; |
|
101
|
|
|
}); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (isset($container['console.commands'])) { |
|
105
|
|
|
$container['console.commands'] = $container->extend('console.commands', function ($commands) use ($container) { |
|
106
|
|
|
$commands[] = new CreateDatabaseDoctrineCommand; |
|
107
|
|
|
$commands[] = new DropDatabaseDoctrineCommand; |
|
108
|
|
|
$commands[] = new CreateSchemaDoctrineCommand; |
|
109
|
|
|
$commands[] = new UpdateSchemaDoctrineCommand; |
|
110
|
|
|
$commands[] = new DropSchemaDoctrineCommand; |
|
111
|
|
|
$commands[] = new RunDqlDoctrineCommand; |
|
112
|
|
|
$commands[] = new RunSqlDoctrineCommand; |
|
113
|
|
|
$commands[] = new ConvertMappingDoctrineCommand; |
|
114
|
|
|
$commands[] = new ClearMetadataCacheDoctrineCommand; |
|
115
|
|
|
$commands[] = new ClearQueryCacheDoctrineCommand; |
|
116
|
|
|
$commands[] = new ClearResultCacheDoctrineCommand; |
|
117
|
|
|
$commands[] = new InfoDoctrineCommand; |
|
118
|
|
|
$commands[] = new ValidateSchemaCommand; |
|
119
|
|
|
$commands[] = new EnsureProductionSettingsDoctrineCommand; |
|
120
|
|
|
|
|
121
|
|
|
return $commands; |
|
122
|
|
|
}); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|