1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyEightPublishers\Application\DI; |
4
|
|
|
|
5
|
|
|
use Nette\DI\CompilerExtension; |
6
|
|
|
use SixtyEightPublishers\Application\ConfigurationException; |
7
|
|
|
use SixtyEightPublishers\Application\Environment; |
8
|
|
|
use SixtyEightPublishers\Application\ProfileContainer; |
9
|
|
|
use SixtyEightPublishers\Application\IEnvironmentDetector; |
10
|
|
|
use SixtyEightPublishers\Application\Detector\NetteRequestDetector; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class EnvironmentExtension extends CompilerExtension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $defaults = [ |
19
|
|
|
'debugger' => FALSE, |
20
|
|
|
'localeDomain' => FALSE, |
21
|
|
|
'mode' => 'tolerant', |
22
|
|
|
'profile' => [], |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function loadConfiguration() |
29
|
|
|
{ |
30
|
|
|
$builder = $this->getContainerBuilder(); |
31
|
|
|
$config = $this->getConfig($this->defaults); |
32
|
|
|
|
33
|
|
|
$builder->addDefinition($this->prefix('environment')) |
34
|
|
|
->setClass(Environment::class); |
35
|
|
|
|
36
|
|
|
$builder->addDefinition($this->prefix('detector')) |
37
|
|
|
->setClass(NetteRequestDetector::class); |
38
|
|
|
|
39
|
|
|
$profileContainer = $builder->addDefinition($this->prefix('profileContainer')) |
40
|
|
|
->setClass(ProfileContainer::class); |
41
|
|
|
|
42
|
|
|
if (empty($config['profile'])) |
43
|
|
|
throw new ConfigurationException("You must define some profile combination in your configuration."); |
44
|
|
|
|
45
|
|
|
$requiredProfileParams = ['country', 'language', 'currency']; |
46
|
|
|
foreach ($config['profile'] as $profileName => $profile) |
47
|
|
|
{ |
48
|
|
|
if (!empty(array_diff($requiredProfileParams, array_keys($profile)))) |
49
|
|
|
throw new ConfigurationException("Problem with \"{$profileName}\" profile configuration. There are missing some of the required parameters (country, language, currency)."); |
50
|
|
|
|
51
|
|
|
$profileContainer->addSetup('addProfile', [ |
52
|
|
|
$profileName, |
53
|
|
|
(array) $profile['country'], |
54
|
|
|
(array) $profile['language'], |
55
|
|
|
(array) $profile['currency'], |
56
|
|
|
array_key_exists('domain', $profile) ? (array) $profile['domain'] : [] |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function beforeCompile() |
65
|
|
|
{ |
66
|
|
|
$builder = $this->getContainerBuilder(); |
67
|
|
|
$detectors = $builder->findByType(IEnvironmentDetector::class); |
68
|
|
|
|
69
|
|
|
if (count($detectors) > 1) |
70
|
|
|
foreach ($detectors as $name => $definition) |
71
|
|
|
if ($definition->getClass() === NetteRequestDetector::class) |
72
|
|
|
$builder->removeDefinition($name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|