Completed
Push — master ( d86016...8a7fa8 )
by Vojtěch
02:06
created

EnvironmentExtension::loadConfiguration()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 31
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 6
nop 0
crap 56
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
class EnvironmentExtension extends CompilerExtension
15
{
16
	/**
17
	 * @var array
18
	 */
19
	private $defaults = [
20
		'debugger' => FALSE,
21
		'localeDomain' => FALSE,
22
		'mode' => 'tolerant',
23
		'profile' => [],
24
	];
25
26
	/**
27
	 * {@inheritdoc}
28
	 */
29
	public function loadConfiguration()
30
	{
31
		$builder = $this->getContainerBuilder();
32
		$config = $this->getConfig($this->defaults);
33
34
		$builder->addDefinition($this->prefix('environment'))
35
			->setClass(Environment::class);
36
37
		$builder->addDefinition($this->prefix('detector'))
38
			->setClass(NetteRequestDetector::class);
39
40
		$profileContainer = $builder->addDefinition($this->prefix('profileContainer'))
41
			->setClass(ProfileContainer::class);
42
43
		if (empty($config['profile'])) {
44
			throw new ConfigurationException("You must define some profile combination in your configuration.");
45
		}
46
47
		$requiredProfileParams = ['country', 'language', 'currency'];
48
		foreach ($config['profile'] as $profileName => $profile) {
49
			if (!empty(array_diff($requiredProfileParams, array_keys($profile)))) {
50
				throw new ConfigurationException("Problem with \"{$profileName}\" profile configuration. There are missing some of the required parameters (country, language, currency).");
51
			}
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
				!(array_key_exists('disable', $profile) && $profile['disable'] === TRUE),
60
			]);
61
		}
62
63
		if ($this->useDebugger()) {
64
			$builder->addDefinition($this->prefix('panel'))
65
				->setClass(Panel::class)
66
				->setInject(FALSE)
67
				->setAutowired(FALSE);
68
		}
69
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public function beforeCompile()
75
	{
76
		$builder = $this->getContainerBuilder();
77
		$detectors = $builder->findByType(IEnvironmentDetector::class);
78
79
		if (count($detectors) > 1) {
80
			foreach ($detectors as $name => $definition) {
81
				if ($definition->getClass() === NetteRequestDetector::class) {
82
					$builder->removeDefinition($name);
83
				}
84
			}
85
		}
86
	}
87
88
	/**
89
	 * {@inheritdoc}
90
	 */
91
	public function afterCompile(ClassType $class)
92
	{
93
		$initialize = $class->methods['initialize'];
94
95
		if ($this->useDebugger()) {
96
			$bar = $this->getContainerBuilder()->getByType('Tracy\Bar');
97
			$initialize->addBody('$this->getService(?)->addPanel($this->getService(?));', [
98
				$bar,
99
				$this->prefix('panel'),
100
			]);
101
		}
102
	}
103
104
	/**
105
	 * @return bool
106
	 */
107
	private function useDebugger()
108
	{
109
		$config = $this->getConfig($this->defaults);
110
		$bar = $this->getContainerBuilder()->getByType('Tracy\Bar');
111
112
		return ($config['debugger'] && $bar && interface_exists('Tracy\IBarPanel'));
113
	}
114
}
115