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