|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace LidskaSila\Prooph; |
|
4
|
|
|
|
|
5
|
|
|
use LidskaSila\Prooph\Common\Configurator; |
|
6
|
|
|
use LidskaSila\Prooph\Common\NetteContainerWrapper; |
|
7
|
|
|
use Nette\DI\CompilerExtension; |
|
8
|
|
|
use Nette\DI\Container; |
|
9
|
|
|
use Nette\DI\ContainerBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class ProophExtension extends CompilerExtension |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
public $defaults = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Configurator */ |
|
18
|
|
|
public $configurator; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->configurator = new ProophExtensionConfigurator($this); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function loadConfiguration(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->defaults = $this->configurator->buildDefaultConfig(); |
|
28
|
|
|
$this->config = $this->mergeConfigIntoDefaults($this->config, $this->defaults); |
|
29
|
|
|
|
|
30
|
|
|
$this->registerContainerWrapper(); |
|
31
|
|
|
|
|
32
|
|
|
$this->configurator->loadConfiguration($this->config); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function mergeConfigIntoDefaults($original, $default): array |
|
36
|
|
|
{ |
|
37
|
|
|
return array_replace_recursive($default, $original); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Container wrapper is needed in every interop factory, so we register it first. |
|
42
|
|
|
*/ |
|
43
|
|
|
private function registerContainerWrapper(): void |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var ContainerBuilder $containerBuilder */ |
|
46
|
|
|
$containerBuilder = $this->getContainerBuilder(); |
|
47
|
|
|
|
|
48
|
|
|
if (!$containerBuilder->getByType(NetteContainerWrapper::class)) { |
|
|
|
|
|
|
49
|
|
|
$jsonConfig = $this->determineJsonConfig(); |
|
50
|
|
|
$containerServiceId = $this->getContainerServiceId($containerBuilder); |
|
51
|
|
|
|
|
52
|
|
|
$containerBuilder |
|
|
|
|
|
|
53
|
|
|
->addDefinition($this->getNetteContainerWrapperDefinitionId()) |
|
54
|
|
|
->setClass(NetteContainerWrapper::class) |
|
55
|
|
|
->setFactory(static::class . '::createContainerWrapper', [ $jsonConfig, '@' . $containerServiceId ]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function createContainerWrapper(string $jsonConfig, $container): NetteContainerWrapper |
|
60
|
|
|
{ |
|
61
|
|
|
$config = json_decode($jsonConfig, true); |
|
62
|
|
|
|
|
63
|
|
|
return new NetteContainerWrapper($config, $container); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function getNetteContainerWrapperDefinitionId(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->prefix('NetteContainerWrapper'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function determineJsonConfig(): string |
|
72
|
|
|
{ |
|
73
|
|
|
// Putting config array to json, because we want keep service links (@serviceId) in config as string. |
|
74
|
|
|
// If it would be array, Nette would replace all these string based links with real services. |
|
75
|
|
|
$config = [ $this->configurator->getConfigKey() => $this->config ]; |
|
76
|
|
|
return json_encode($config); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function getContainerServiceId(ContainerBuilder $containerBuilder): string |
|
80
|
|
|
{ |
|
81
|
|
|
return (string) $containerBuilder->getByType(Container::class); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: