1 | <?php |
||
38 | 1 | final class DoctrineBlameableExtension extends DI\CompilerExtension |
|
39 | { |
||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function getConfigSchema() : Schema\Schema |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function loadConfiguration() : void |
||
57 | { |
||
58 | $builder = $this->getContainerBuilder(); |
||
59 | $configuration = $this->getConfig(); |
||
60 | |||
61 | $builder->addDefinition($this->prefix('configuration')) |
||
62 | ->setType(DoctrineBlameable\Configuration::class) |
||
63 | ->setArguments([ |
||
64 | $configuration->userEntity, |
||
65 | $configuration->lazyAssociation, |
||
66 | $configuration->autoMapField, |
||
67 | ]); |
||
68 | |||
69 | $userCallableDefinition = NULL; |
||
70 | |||
71 | if ($configuration->userCallable !== NULL) { |
||
72 | $userCallableDefinition = $builder->addDefinition($this->prefix('userCallable')); |
||
73 | |||
74 | $factory = is_string($configuration->userCallable) ? new DI\Definitions\Statement($configuration->userCallable) : $configuration->userCallable; |
||
75 | $userCallableDefinition->setFactory($factory); |
||
76 | |||
77 | list($resolverClass) = (array) $this->normalizeEntity($userCallableDefinition->getFactory()->getEntity()); |
||
78 | |||
79 | if (class_exists($resolverClass)) { |
||
80 | $userCallableDefinition->setType($resolverClass); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $builder->addDefinition($this->prefix('driver')) |
||
85 | ->setType(Mapping\Driver\Blameable::class); |
||
86 | |||
87 | $builder->addDefinition($this->prefix('subscriber')) |
||
88 | ->setType(Events\BlameableSubscriber::class) |
||
89 | ->setArguments([$userCallableDefinition instanceof DI\Definitions\ServiceDefinition ? $userCallableDefinition : NULL]); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function beforeCompile() : void |
||
96 | { |
||
97 | parent::beforeCompile(); |
||
98 | |||
99 | $builder = $this->getContainerBuilder(); |
||
100 | |||
101 | $builder->getDefinition($builder->getByType(Doctrine\ORM\EntityManagerInterface::class, TRUE)) |
||
102 | ->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param Nette\Configurator $config |
||
107 | * @param string $extensionName |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public static function register( |
||
119 | |||
120 | /** |
||
121 | * @param $entity |
||
122 | * |
||
123 | * @return string|array Class, @service, [Class, member], [@service, member], [, globalFunc], [Statement, member] |
||
124 | */ |
||
125 | private function normalizeEntity($entity) |
||
160 | } |
||
161 |