1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DoctrineBlameableExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://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
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\DoctrineBlameable\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\DI; |
21
|
|
|
use Nette\Schema; |
22
|
|
|
|
23
|
|
|
use IPub\DoctrineBlameable; |
24
|
|
|
use IPub\DoctrineBlameable\Events; |
25
|
|
|
use IPub\DoctrineBlameable\Mapping; |
26
|
|
|
use IPub\DoctrineBlameable\Security; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Doctrine blameable extension container |
30
|
|
|
* |
31
|
|
|
* @package iPublikuj:DoctrineBlameable! |
32
|
|
|
* @subpackage DI |
33
|
|
|
* |
34
|
|
|
* @author Adam Kadlec <[email protected]> |
35
|
|
|
*/ |
36
|
1 |
|
final class DoctrineBlameableExtension extends DI\CompilerExtension |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function getConfigSchema() : Schema\Schema |
42
|
|
|
{ |
43
|
1 |
|
return Schema\Expect::structure([ |
44
|
1 |
|
'lazyAssociation' => Schema\Expect::bool(FALSE), |
45
|
1 |
|
'userEntity' => Schema\Expect::anyOf(Schema\Expect::string(), Schema\Expect::type(DI\Definitions\Statement::class))->nullable(), |
46
|
1 |
|
'automapField' => Schema\Expect::bool(TRUE), |
47
|
1 |
|
'userCallable' => Schema\Expect::anyOf(Schema\Expect::string(), Schema\Expect::type(DI\Definitions\Statement::class))->default(Security\UserCallable::class), |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function loadConfiguration() : void |
55
|
|
|
{ |
56
|
1 |
|
$builder = $this->getContainerBuilder(); |
57
|
1 |
|
$configuration = $this->getConfig(); |
58
|
|
|
|
59
|
1 |
|
$builder->addDefinition($this->prefix('configuration')) |
60
|
1 |
|
->setType(DoctrineBlameable\Configuration::class) |
61
|
1 |
|
->setArguments([ |
62
|
1 |
|
$configuration->userEntity, |
63
|
1 |
|
$configuration->lazyAssociation, |
64
|
1 |
|
$configuration->automapField, |
65
|
|
|
]); |
66
|
|
|
|
67
|
1 |
|
$userCallable = NULL; |
68
|
|
|
|
69
|
1 |
|
if ($configuration->userCallable !== NULL) { |
70
|
1 |
|
$definition = $builder->addDefinition($this->prefix(md5($configuration->userCallable))); |
71
|
|
|
|
72
|
1 |
|
[$factory] = DI\Helpers::filterArguments([ |
|
|
|
|
73
|
1 |
|
is_string($configuration->userCallable) ? new DI\Definitions\Statement($configuration->userCallable) : $configuration->userCallable, |
74
|
|
|
]); |
75
|
|
|
|
76
|
1 |
|
$definition->setFactory($factory); |
77
|
|
|
|
78
|
1 |
|
[$resolverClass] = (array) $this->normalizeEntity($definition->getFactory()); |
|
|
|
|
79
|
|
|
|
80
|
|
|
if (class_exists($resolverClass)) { |
81
|
|
|
$definition->setType($resolverClass); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$userCallable = $definition; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$builder->addDefinition($this->prefix('driver')) |
88
|
|
|
->setType(Mapping\Driver\Blameable::class); |
89
|
|
|
|
90
|
|
|
$builder->addDefinition($this->prefix('subscriber')) |
91
|
|
|
->setType(Events\BlameableSubscriber::class) |
92
|
|
|
->setArguments([$userCallable instanceof DI\Definitions\ServiceDefinition ? '@' . $userCallable->getType() : NULL]); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function beforeCompile() : void |
99
|
|
|
{ |
100
|
|
|
parent::beforeCompile(); |
101
|
|
|
|
102
|
|
|
$builder = $this->getContainerBuilder(); |
103
|
|
|
|
104
|
|
|
$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface', TRUE)) |
105
|
|
|
->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Nette\Configurator $config |
110
|
|
|
* @param string $extensionName |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public static function register( |
115
|
|
|
Nette\Configurator $config, |
116
|
|
|
string $extensionName = 'doctrineBlameable' |
117
|
|
|
) : void { |
118
|
1 |
|
$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) { |
119
|
1 |
|
$compiler->addExtension($extensionName, new DoctrineBlameableExtension); |
120
|
1 |
|
}; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $entity |
125
|
|
|
* |
126
|
|
|
* @return string|array Class, @service, [Class, member], [@service, member], [, globalFunc], [Statement, member] |
127
|
|
|
*/ |
128
|
|
|
private function normalizeEntity($entity) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
// Get container builder |
131
|
1 |
|
$builder = $this->getContainerBuilder(); |
132
|
|
|
|
133
|
|
|
if ( |
134
|
1 |
|
is_string($entity) |
135
|
1 |
|
&& Nette\Utils\Strings::contains($entity, '::') |
136
|
1 |
|
&& !Nette\Utils\Strings::contains($entity, '?') |
137
|
|
|
) { // Class::method -> [Class, method] |
138
|
|
|
$entity = explode('::', $entity); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( |
142
|
1 |
|
is_array($entity) |
143
|
1 |
|
&& $entity[0] instanceof DI\Definitions\ServiceDefinition |
|
|
|
|
144
|
|
|
) { // [ServiceDefinition, ...] -> [@serviceName, ...] |
145
|
|
|
$entity[0] = '@' . current(array_keys($builder->getDefinitions(), $entity[0], TRUE)); |
146
|
|
|
|
147
|
|
|
} elseif ( |
148
|
1 |
|
$entity instanceof DI\Definitions\ServiceDefinition |
|
|
|
|
149
|
|
|
) { // ServiceDefinition -> @serviceName |
150
|
|
|
$entity = '@' . current(array_keys($builder->getDefinitions(), $entity, TRUE)); |
151
|
|
|
|
152
|
|
|
} elseif ( |
153
|
1 |
|
is_array($entity) |
154
|
1 |
|
&& $entity[0] === $builder |
155
|
|
|
) { // [$builder, ...] -> [@container, ...] |
156
|
|
|
trigger_error("Replace object ContainerBuilder in Statement entity with '@container'.", E_USER_DEPRECATED); |
157
|
|
|
|
158
|
|
|
$entity[0] = '@' . md5($entity); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $entity; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.