1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Rostenkowski\Doctrine; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
7
|
|
|
use Doctrine\Common\Cache\PhpFileCache; |
8
|
|
|
use Doctrine\DBAL\Logging\LoggerChain; |
9
|
|
|
use Doctrine\DBAL\Tools\Console\Command\ImportCommand; |
10
|
|
|
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand; |
11
|
|
|
use Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand; |
12
|
|
|
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand; |
13
|
|
|
use Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand; |
14
|
|
|
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand; |
15
|
|
|
use Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand; |
16
|
|
|
use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand; |
17
|
|
|
use Doctrine\ORM\Tools\Console\Command\InfoCommand; |
18
|
|
|
use Doctrine\ORM\Tools\Console\Command\RunDqlCommand; |
19
|
|
|
use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand; |
20
|
|
|
use Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand; |
21
|
|
|
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand; |
22
|
|
|
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand; |
23
|
|
|
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; |
24
|
|
|
use Nette\DI\CompilerExtension; |
25
|
|
|
use Nette\DI\Helpers; |
26
|
|
|
use Nette\DI\Statement; |
27
|
|
|
use Rostenkowski\Doctrine\Debugger\TracyBar; |
28
|
|
|
use Rostenkowski\Doctrine\Logger\FileLogger; |
29
|
|
|
use Rostenkowski\Doctrine\Repository\Repository; |
30
|
|
|
use Symfony\Component\Console\Application; |
31
|
|
|
use Symfony\Component\Console\Helper\DebugFormatterHelper; |
32
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
33
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
34
|
|
|
use Symfony\Component\Console\Helper\ProcessHelper; |
35
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
36
|
|
|
|
37
|
|
|
class Extension extends CompilerExtension |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $defaults = [ |
44
|
|
|
'connection' => [ |
45
|
|
|
'driver' => NULL, |
46
|
|
|
'path' => NULL, |
47
|
|
|
'host' => NULL, |
48
|
|
|
'dbname' => NULL, |
49
|
|
|
'user' => NULL, |
50
|
|
|
'password' => NULL, |
51
|
|
|
], |
52
|
|
|
'entity' => [ |
53
|
|
|
'%appDir%/entities' |
54
|
|
|
], |
55
|
|
|
'repository' => Repository::class, |
56
|
|
|
'debugger' => [ |
57
|
|
|
'enabled' => '%debugMode%', |
58
|
|
|
'factory' => TracyBar::class, |
59
|
|
|
'width' => '960px', |
60
|
|
|
'height' => '720px', |
61
|
|
|
], |
62
|
|
|
'logger' => [ |
63
|
|
|
'enabled' => true, |
64
|
|
|
'factory' => FileLogger::class, |
65
|
|
|
'args' => ['%logDir%/query.log'] |
66
|
|
|
], |
67
|
|
|
'cache' => [ |
68
|
|
|
'factory' => PhpFileCache::class, |
69
|
|
|
'args' => ['%tempDir%/doctrine/cache'] |
70
|
|
|
], |
71
|
|
|
'proxy' => [ |
72
|
|
|
'dir' => '%tempDir%/doctrine/proxies', |
73
|
|
|
], |
74
|
|
|
'function' => [], |
75
|
|
|
'type' => [], |
76
|
|
|
'console' => [ |
77
|
|
|
'commands' => [ |
78
|
|
|
'doctrine:info' => InfoCommand::class, |
79
|
|
|
'doctrine:production' => EnsureProductionSettingsCommand::class, |
80
|
|
|
'doctrine:repositories' => GenerateRepositoriesCommand::class, |
81
|
|
|
'doctrine:proxies' => GenerateProxiesCommand::class, |
82
|
|
|
'doctrine:schema-check' => ValidateSchemaCommand::class, |
83
|
|
|
'doctrine:schema-update' => UpdateCommand::class, |
84
|
|
|
'doctrine:schema-create' => CreateCommand::class, |
85
|
|
|
'doctrine:schema-drop' => DropCommand::class, |
86
|
|
|
'doctrine:cache-metadata' => MetadataCommand::class, |
87
|
|
|
'doctrine:cache-query' => QueryCommand::class, |
88
|
|
|
'doctrine:cache-result' => ResultCommand::class, |
89
|
|
|
'doctrine:query' => RunDqlCommand::class, |
90
|
|
|
'doctrine:exec' => RunSqlCommand::class, |
91
|
|
|
'doctrine:import' => ImportCommand::class, |
92
|
|
|
], |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
public function loadConfiguration() |
98
|
|
|
{ |
99
|
|
|
$builder = $this->getContainerBuilder(); |
100
|
|
|
|
101
|
|
|
$this->config = $config = Helpers::expand($this->validateConfig($this->defaults), $builder->parameters); |
102
|
|
|
|
103
|
|
|
// create configuration |
104
|
|
|
$configuration = $builder->addDefinition($this->prefix('config')) |
105
|
|
|
->setFactory('Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration', [ |
106
|
|
|
$config['entity'], |
107
|
|
|
$config['debugger']['enabled'], |
108
|
|
|
$config['proxy']['dir'], |
109
|
|
|
$this->prefix('@cache'), |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
// create logger |
113
|
|
|
$log = $builder->addDefinition($this->prefix('log')) |
114
|
|
|
->setFactory(LoggerChain::class); |
115
|
|
|
if ($config['logger']['enabled']) { |
116
|
|
|
$builder->addDefinition($this->prefix('logger')) |
117
|
|
|
->setFactory($config['logger']['factory'], $config['logger']['args']); |
118
|
|
|
$log->addSetup('addLogger', [$this->prefix('@logger')]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// create debugger |
122
|
|
|
if ($config['debugger']['enabled']) { |
123
|
|
|
$builder->addDefinition($this->prefix('debugger')) |
124
|
|
|
->setFactory(TracyBar::class) |
125
|
|
|
->addSetup('Tracy\Debugger::getBar()->addPanel(?);', ['@self']) |
126
|
|
|
->addSetup('setWidth', [$config['debugger']['width']]) |
127
|
|
|
->addSetup('setHeight', [$config['debugger']['height']]); |
128
|
|
|
$log->addSetup('addLogger', [$this->prefix('@debugger')]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// create cache |
132
|
|
|
$cache = $builder->addDefinition($this->prefix('cache')); |
133
|
|
|
if ($config['debugger']['enabled']) { |
134
|
|
|
$cache->setFactory(ArrayCache::class); |
135
|
|
|
} else { |
136
|
|
|
$cache->setFactory($config['cache']['factory'], $config['cache']['args']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// set repository class |
140
|
|
|
$configuration->addSetup('setDefaultRepositoryClassName', [$config['repository']]); |
141
|
|
|
|
142
|
|
|
// set logger chain as logger |
143
|
|
|
$configuration->addSetup('setSQLLogger', [$this->prefix('@log')]); |
144
|
|
|
|
145
|
|
|
// create entity manager |
146
|
|
|
$builder->addDefinition($this->prefix('entityManager')) |
147
|
|
|
->setFactory('Doctrine\ORM\EntityManager::create', [$config['connection'], $this->prefix('@config')]) |
148
|
|
|
->setAutowired(true); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
public function beforeCompile() |
153
|
|
|
{ |
154
|
|
|
$config = $this->getConfig(); |
155
|
|
|
$builder = $this->getContainerBuilder(); |
156
|
|
|
|
157
|
|
|
// add console |
158
|
|
|
if ($builder->findByType(Application::class)) { |
159
|
|
|
$console = $builder->getDefinition($builder->getByType(Application::class)); |
160
|
|
|
} else { |
161
|
|
|
$console = $builder->addDefinition($this->prefix('console')); |
162
|
|
|
$console->setFactory(Application::class, ['doctrine console']); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// create entity manager helper |
166
|
|
|
$em = $builder->addDefinition($this->prefix('em')); |
167
|
|
|
$em->setFactory(EntityManagerHelper::class); |
168
|
|
|
|
169
|
|
|
// create helper set |
170
|
|
|
if ($builder->findByType(HelperSet::class)) { |
171
|
|
|
$helpers = $builder->getDefinition($builder->getByType(HelperSet::class)); |
172
|
|
|
} else { |
173
|
|
|
// default helper set |
174
|
|
|
$helpers = $builder->addDefinition($this->prefix('helpers')); |
175
|
|
|
$helpers->setFactory(HelperSet::class, [[ |
176
|
|
|
new Statement(FormatterHelper::class), |
177
|
|
|
new Statement(DebugFormatterHelper::class), |
178
|
|
|
new Statement(ProcessHelper::class), |
179
|
|
|
new Statement(QuestionHelper::class), |
180
|
|
|
]]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// add entity manager helper to application helper set |
184
|
|
|
$helpers->addSetup('set', [$this->prefix('@em'), 'em']); |
185
|
|
|
|
186
|
|
|
// set helper set as application helper set |
187
|
|
|
$console->addSetup('setHelperSet'); |
188
|
|
|
|
189
|
|
|
// add doctrine commands |
190
|
|
|
foreach ($config['console']['commands'] as $commandName => $class) { |
191
|
|
|
$commandServiceName = 'command_' . preg_replace('/[^a-z]/i', '_', $commandName); |
192
|
|
|
$command = $builder->addDefinition($this->prefix($commandServiceName)); |
193
|
|
|
$command->setFactory($class); |
194
|
|
|
$command->addSetup('setName', [$commandName]); |
195
|
|
|
$console->addSetup('add', [$this->prefix("@$commandServiceName")]); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|