Completed
Pull Request — master (#3)
by Adam
09:18
created

DoctrineBlameableExtension::loadConfiguration()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 24
cp 0
rs 8.9688
c 0
b 0
f 0
cc 5
nc 3
nop 0
crap 30
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
		$builder = $this->getContainerBuilder();
57
		$configuration = $this->getConfig();
58
59
		$builder->addDefinition($this->prefix('configuration'))
60
			->setType(DoctrineBlameable\Configuration::class)
61
			->setArguments([
62
				$configuration->userEntity,
63
				$configuration->lazyAssociation,
64
				$configuration->automapField,
65
			]);
66
67
		$userCallable = NULL;
68
69
		if ($configuration->userCallable !== NULL) {
70
			$definition = $builder->addDefinition($this->prefix(md5($configuration->userCallable)));
71
72
			[$factory] = DI\Helpers::filterArguments([
0 ignored issues
show
Bug introduced by
The variable $factory does not exist. Did you forget to declare it?

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.

Loading history...
73
				is_string($configuration->userCallable) ? new DI\Definitions\Statement($configuration->userCallable) : $configuration->userCallable,
74
			]);
75
76
			$definition->setFactory($factory);
77
78
			[$resolverClass] = (array) $this->normalizeEntity($definition->getFactory());
0 ignored issues
show
Bug introduced by
The variable $resolverClass does not exist. Did you forget to declare it?

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.

Loading history...
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
		$builder = $this->getContainerBuilder();
132
133
		if (
134
			is_string($entity)
135
			&& Nette\Utils\Strings::contains($entity, '::')
136
			&& !Nette\Utils\Strings::contains($entity, '?')
137
		) { // Class::method -> [Class, method]
138
			$entity = explode('::', $entity);
139
		}
140
141
		if (
142
			is_array($entity)
143
			&& $entity[0] instanceof DI\Definitions\ServiceDefinition
144
		) { // [ServiceDefinition, ...] -> [@serviceName, ...]
145
			$entity[0] = '@' . current(array_keys($builder->getDefinitions(), $entity[0], TRUE));
146
147
		} elseif (
148
			$entity instanceof DI\Definitions\ServiceDefinition
149
		) { // ServiceDefinition -> @serviceName
150
			$entity = '@' . current(array_keys($builder->getDefinitions(), $entity, TRUE));
151
152
		} elseif (
153
			is_array($entity)
154
			&& $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
		return $entity;
162
	}
163
}
164