1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DoctrineBlameableExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:DoctrineBlameable! |
9
|
|
|
* @subpackage DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 01.01.16 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace IPub\DoctrineBlameable\DI; |
16
|
|
|
|
17
|
|
|
use Nette; |
18
|
|
|
use Nette\DI; |
19
|
|
|
use Nette\Utils; |
20
|
|
|
use Nette\PhpGenerator as Code; |
21
|
|
|
|
22
|
|
|
use IPub\DoctrineBlameable; |
23
|
|
|
use IPub\DoctrineBlameable\Events; |
24
|
|
|
use IPub\DoctrineBlameable\Mapping; |
25
|
|
|
use IPub\DoctrineBlameable\Security; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Doctrine blameable extension container |
29
|
|
|
* |
30
|
|
|
* @package iPublikuj:DoctrineBlameable! |
31
|
|
|
* @subpackage DI |
32
|
|
|
* |
33
|
|
|
* @author Adam Kadlec <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
final class DoctrineBlameableExtension extends DI\CompilerExtension |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
1 |
|
*/ |
40
|
|
|
private $defaults = [ |
41
|
|
|
'lazyAssociation' => FALSE, |
42
|
|
|
'userEntity' => NULL, |
43
|
|
|
'automapField' => TRUE, |
44
|
|
|
'userCallable' => Security\UserCallable::CLASS_NAME, |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
public function loadConfiguration() |
48
|
|
|
{ |
49
|
|
|
$config = $this->getConfig($this->defaults); |
50
|
|
|
$builder = $this->getContainerBuilder(); |
51
|
|
|
|
52
|
1 |
|
Utils\Validators::assert($config['userEntity'], 'type|null', 'userEntity'); |
53
|
1 |
|
Utils\Validators::assert($config['lazyAssociation'], 'bool', 'lazyAssociation'); |
54
|
|
|
Utils\Validators::assert($config['automapField'], 'bool', 'automapField'); |
55
|
1 |
|
|
56
|
1 |
|
$builder->addDefinition($this->prefix('configuration')) |
57
|
1 |
|
->setClass(DoctrineBlameable\Configuration::CLASS_NAME) |
58
|
|
|
->setArguments([ |
59
|
1 |
|
$config['userEntity'], |
60
|
1 |
|
$config['lazyAssociation'], |
61
|
1 |
|
$config['automapField'] |
62
|
1 |
|
]); |
63
|
1 |
|
|
64
|
1 |
|
$userCallable = NULL; |
65
|
1 |
|
|
66
|
|
|
if ($config['userCallable'] !== NULL) { |
67
|
1 |
|
$definition = $builder->addDefinition($this->prefix(md5($config['userCallable']))); |
68
|
|
|
|
69
|
1 |
|
list($factory) = DI\Compiler::filterArguments([ |
70
|
1 |
|
is_string($config['userCallable']) ? new DI\Statement($config['userCallable']) : $config['userCallable'] |
71
|
|
|
]); |
72
|
1 |
|
|
73
|
1 |
|
$definition->setFactory($factory); |
74
|
1 |
|
|
75
|
|
|
list($resolverClass) = (array) $builder->normalizeEntity($definition->getFactory()->getEntity()); |
76
|
1 |
|
|
77
|
|
|
if (class_exists($resolverClass)) { |
78
|
1 |
|
$definition->setClass($resolverClass); |
79
|
|
|
} |
80
|
1 |
|
|
81
|
1 |
|
$userCallable = $definition; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
1 |
|
$builder->addDefinition($this->prefix('driver')) |
85
|
1 |
|
->setClass(Mapping\Driver\Blameable::CLASS_NAME); |
86
|
|
|
|
87
|
1 |
|
$builder->addDefinition($this->prefix('subscriber')) |
88
|
1 |
|
->setClass(Events\BlameableSubscriber::CLASS_NAME) |
89
|
|
|
->setArguments([$userCallable instanceof DI\ServiceDefinition ? '@' . $userCallable->getClass() : NULL]); |
90
|
1 |
|
} |
91
|
1 |
|
|
92
|
1 |
|
public function beforeCompile() |
93
|
1 |
|
{ |
94
|
1 |
|
$builder = $this->getContainerBuilder(); |
95
|
|
|
|
96
|
|
|
$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface') ?: 'doctrine.default.entityManager') |
97
|
|
|
->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Nette\Configurator $config |
102
|
1 |
|
* @param string $extensionName |
103
|
1 |
|
*/ |
104
|
1 |
|
public static function register(Nette\Configurator $config, $extensionName = 'doctrineBlameable') |
105
|
1 |
|
{ |
106
|
|
|
$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) { |
107
|
1 |
|
$compiler->addExtension($extensionName, new DoctrineBlameableExtension); |
108
|
|
|
}; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|