1 | <?php |
||
36 | 1 | final class DoctrineBlameableExtension extends DI\CompilerExtension |
|
37 | { |
||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function getConfigSchema() : Schema\Schema |
||
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 | $userCallableDefinition = NULL; |
|
68 | |||
69 | 1 | if ($configuration->userCallable !== NULL) { |
|
70 | 1 | $userCallableDefinition = $builder->addDefinition($this->prefix('userCallable')); |
|
71 | |||
72 | 1 | $factory = is_string($configuration->userCallable) ? new DI\Definitions\Statement($configuration->userCallable) : $configuration->userCallable; |
|
73 | 1 | $userCallableDefinition->setFactory($factory); |
|
74 | |||
75 | 1 | list($resolverClass) = (array) $this->normalizeEntity($userCallableDefinition->getFactory()->getEntity()); |
|
76 | |||
77 | 1 | if (class_exists($resolverClass)) { |
|
78 | 1 | $userCallableDefinition->setType($resolverClass); |
|
79 | } |
||
80 | } |
||
81 | |||
82 | 1 | $builder->addDefinition($this->prefix('driver')) |
|
83 | 1 | ->setType(Mapping\Driver\Blameable::class); |
|
84 | |||
85 | 1 | $builder->addDefinition($this->prefix('subscriber')) |
|
86 | 1 | ->setType(Events\BlameableSubscriber::class) |
|
87 | 1 | ->setArguments([$userCallableDefinition instanceof DI\Definitions\ServiceDefinition ? $userCallableDefinition : NULL]); |
|
88 | 1 | } |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function beforeCompile() : void |
||
94 | { |
||
95 | 1 | parent::beforeCompile(); |
|
96 | |||
97 | 1 | $builder = $this->getContainerBuilder(); |
|
98 | |||
99 | 1 | $builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface', TRUE)) |
|
100 | 1 | ->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]); |
|
101 | 1 | } |
|
102 | |||
103 | /** |
||
104 | * @param Nette\Configurator $config |
||
105 | * @param string $extensionName |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | public static function register( |
||
117 | |||
118 | /** |
||
119 | * @param $entity |
||
120 | * |
||
121 | * @return string|array Class, @service, [Class, member], [@service, member], [, globalFunc], [Statement, member] |
||
122 | */ |
||
123 | private function normalizeEntity($entity) |
||
158 | } |
||
159 |