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 Nette\DI\CompilerExtension; |
10
|
|
|
use Nette\DI\Helpers; |
11
|
|
|
use Rostenkowski\Doctrine\Debugger\TracyBar; |
12
|
|
|
use Rostenkowski\Doctrine\Logger\FileLogger; |
13
|
|
|
use Rostenkowski\Doctrine\Repository\Repository; |
14
|
|
|
|
15
|
|
|
class Extension extends CompilerExtension |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $defaults = [ |
22
|
|
|
'default' => [ |
23
|
|
|
'connection' => [ |
24
|
|
|
'driver' => NULL, |
25
|
|
|
'path' => NULL, |
26
|
|
|
'host' => NULL, |
27
|
|
|
'dbname' => NULL, |
28
|
|
|
'user' => NULL, |
29
|
|
|
'password' => NULL, |
30
|
|
|
], |
31
|
|
|
'entity' => [ |
32
|
|
|
'%appDir%/entities' |
33
|
|
|
], |
34
|
|
|
'repository' => Repository::class, |
35
|
|
|
'debugger' => [ |
36
|
|
|
'enabled' => '%debugMode%', |
37
|
|
|
'factory' => TracyBar::class, |
38
|
|
|
], |
39
|
|
|
'logger' => [ |
40
|
|
|
'enabled' => true, |
41
|
|
|
'factory' => FileLogger::class, |
42
|
|
|
'args' => ['%logDir%/query.log'] |
43
|
|
|
], |
44
|
|
|
'cache' => [ |
45
|
|
|
'factory' => PhpFileCache::class, |
46
|
|
|
'args' => ['%tempDir%/doctrine/cache'] |
47
|
|
|
], |
48
|
|
|
'proxy' => [ |
49
|
|
|
'dir' => '%tempDir%/doctrine/proxies', |
50
|
|
|
], |
51
|
|
|
'function' => [], |
52
|
|
|
'type' => [], |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function loadConfiguration() |
58
|
|
|
{ |
59
|
|
|
$builder = $this->getContainerBuilder(); |
60
|
|
|
$configs = Helpers::expand($this->validateConfig($this->defaults), $builder->parameters); |
61
|
|
|
foreach ($configs as $name => $config) { |
62
|
|
|
$configuration = $builder->addDefinition($this->prefix("{$name}.config")) |
63
|
|
|
->setFactory('Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration', [ |
64
|
|
|
$config['entity'], |
65
|
|
|
$config['debugger']['enabled'], |
66
|
|
|
$config['proxy']['dir'], |
67
|
|
|
$this->prefix("@{$name}.cache"), |
68
|
|
|
]); |
69
|
|
|
$log = $builder->addDefinition($this->prefix("{$name}.log")) |
70
|
|
|
->setFactory(LoggerChain::class); |
71
|
|
|
$configuration->addSetup('setDefaultRepositoryClassName', [$config['repository']]); |
72
|
|
|
$configuration->addSetup('setSQLLogger', [$this->prefix("@{$name}.log")]); |
73
|
|
|
if ($config['debugger']['enabled']) { |
74
|
|
|
$builder->addDefinition($this->prefix("{$name}.debugger")) |
75
|
|
|
->setFactory(TracyBar::class) |
76
|
|
|
->addSetup('Tracy\Debugger::getBar()->addPanel(?);', ['@self']); |
77
|
|
|
$log->addSetup('addLogger', [$this->prefix("@{$name}.debugger")]); |
78
|
|
|
} |
79
|
|
|
$cache = $builder->addDefinition($this->prefix("{$name}.cache")); |
80
|
|
|
if ($config['debugger']['enabled']) { |
81
|
|
|
$cache->setFactory(ArrayCache::class); |
82
|
|
|
} else { |
83
|
|
|
$cache->setFactory($config['cache']['factory'], $config['cache']['args']); |
84
|
|
|
} |
85
|
|
|
if ($config['logger']['enabled']) { |
86
|
|
|
$builder->addDefinition($this->prefix("{$name}.logger")) |
87
|
|
|
->setFactory($config['logger']['factory'], $config['logger']['args']); |
88
|
|
|
$log->addSetup('addLogger', [$this->prefix("@{$name}.logger")]); |
89
|
|
|
} |
90
|
|
|
$builder->addDefinition($this->prefix("{$name}.em")) |
91
|
|
|
->setFactory('Doctrine\ORM\EntityManager::create', [$config['connection'], $this->prefix("@{$name}.config")]) |
92
|
|
|
->setAutowired($name === 'default'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|