Completed
Push — master ( 294889...618737 )
by Vojtěch
02:15
created

EnvironmentExtension::loadConfiguration()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 6
nop 0
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'));
0 ignored issues
show
Bug Best Practice introduced by
The expression $bar of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
129
	}
130
}
131