1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\Application\Environment\DI; |
6
|
|
|
|
7
|
|
|
use Nette\DI\CompilerExtension; |
8
|
|
|
use Nette\PhpGenerator\ClassType; |
9
|
|
|
use SixtyEightPublishers\Application\Environment\ConfigurationException; |
10
|
|
|
use SixtyEightPublishers\Application\Environment\Environment; |
11
|
|
|
use SixtyEightPublishers\Application\Environment\ProfileContainer; |
12
|
|
|
use SixtyEightPublishers\Application\Environment\IEnvironmentDetector; |
13
|
|
|
use SixtyEightPublishers\Application\Environment\Detector\NetteRequestDetector; |
14
|
|
|
use SixtyEightPublishers\Application\Environment\Diagnostics\Panel; |
15
|
|
|
use SixtyEightPublishers\Application\Environment\IProfileStorage; |
16
|
|
|
use SixtyEightPublishers\Application\Environment\Storage\SessionProfileStorage; |
17
|
|
|
|
18
|
|
|
class EnvironmentExtension extends CompilerExtension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $defaults = [ |
24
|
|
|
'debugger' => FALSE, |
25
|
|
|
'localeDomain' => FALSE, |
26
|
|
|
'mode' => 'tolerant', |
27
|
|
|
'profile' => [], |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function loadConfiguration() |
34
|
|
|
{ |
35
|
|
|
$builder = $this->getContainerBuilder(); |
36
|
|
|
$config = $this->getConfig($this->defaults); |
37
|
|
|
|
38
|
|
|
$builder->addDefinition($this->prefix('environment')) |
39
|
|
|
->setClass(Environment::class); |
40
|
|
|
|
41
|
|
|
$builder->addDefinition($this->prefix('detector')) |
42
|
|
|
->setClass(NetteRequestDetector::class); |
43
|
|
|
|
44
|
|
|
$builder->addDefinition($this->prefix('profileStorage')) |
45
|
|
|
->setClass(SessionProfileStorage::class); |
46
|
|
|
|
47
|
|
|
$profileContainer = $builder->addDefinition($this->prefix('profileContainer')) |
48
|
|
|
->setClass(ProfileContainer::class); |
49
|
|
|
|
50
|
|
|
if (empty($config['profile'])) { |
51
|
|
|
throw new ConfigurationException("You must define some profile combination in your configuration."); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$requiredProfileParams = ['country', 'language', 'currency']; |
55
|
|
|
foreach ($config['profile'] as $profileName => $profile) { |
56
|
|
|
if (!empty(array_diff($requiredProfileParams, array_keys($profile)))) { |
57
|
|
|
throw new ConfigurationException("Problem with \"{$profileName}\" profile configuration. There are missing some of the required parameters (country, language, currency)."); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$profileContainer->addSetup('addProfile', [ |
61
|
|
|
$profileName, |
62
|
|
|
(array) $profile['country'], |
63
|
|
|
(array) $profile['language'], |
64
|
|
|
(array) $profile['currency'], |
65
|
|
|
array_key_exists('domain', $profile) ? (array) $profile['domain'] : [], |
66
|
|
|
!(array_key_exists('disable', $profile) && $profile['disable'] === TRUE), |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($this->useDebugger()) { |
71
|
|
|
$builder->addDefinition($this->prefix('panel')) |
72
|
|
|
->setClass(Panel::class) |
73
|
|
|
->setInject(FALSE) |
74
|
|
|
->setAutowired(FALSE); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function beforeCompile() |
82
|
|
|
{ |
83
|
|
|
$builder = $this->getContainerBuilder(); |
84
|
|
|
$detectors = $builder->findByType(IEnvironmentDetector::class); |
85
|
|
|
$storage = $builder->findByType(IProfileStorage::class); |
86
|
|
|
|
87
|
|
|
if (count($detectors) > 1) { |
88
|
|
|
foreach ($detectors as $name => $definition) { |
89
|
|
|
if ($definition->getClass() === NetteRequestDetector::class) { |
90
|
|
|
$builder->removeDefinition($name); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (count($storage) > 1) { |
96
|
|
|
foreach ($storage as $name => $definition) { |
97
|
|
|
if ($definition->getClass() === SessionProfileStorage::class) { |
98
|
|
|
$builder->removeDefinition($name); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function afterCompile(ClassType $class) |
108
|
|
|
{ |
109
|
|
|
$initialize = $class->methods['initialize']; |
110
|
|
|
|
111
|
|
|
if ($this->useDebugger()) { |
112
|
|
|
$bar = $this->getContainerBuilder()->getByType('Tracy\Bar'); |
113
|
|
|
$initialize->addBody('$this->getService(?)->addPanel($this->getService(?));', [ |
114
|
|
|
$bar, |
115
|
|
|
$this->prefix('panel'), |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
private function useDebugger() |
124
|
|
|
{ |
125
|
|
|
$config = $this->getConfig($this->defaults); |
126
|
|
|
$bar = $this->getContainerBuilder()->getByType('Tracy\Bar'); |
127
|
|
|
|
128
|
|
|
return ($config['debugger'] && $bar && interface_exists('Tracy\IBarPanel')); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: