1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Config; |
4
|
|
|
|
5
|
|
|
use phmLabs\Components\Annovent\Dispatcher; |
6
|
|
|
use PhmLabs\Components\Init\Init; |
7
|
|
|
use Symfony\Component\Yaml\Yaml; |
8
|
|
|
use whm\Html\Uri; |
9
|
|
|
use whm\Smoke\Rules\Rule; |
10
|
|
|
|
11
|
|
|
class Configuration |
12
|
|
|
{ |
13
|
|
|
const DEFAULT_SETTINGS = 'analyze.yml'; |
14
|
|
|
|
15
|
|
|
private $startUri; |
16
|
|
|
|
17
|
|
|
private $rules = []; |
18
|
|
|
|
19
|
|
|
private $configArray; |
20
|
|
|
|
21
|
|
|
private $eventDispatcher; |
22
|
|
|
|
23
|
|
|
private $extensions = array(); |
24
|
|
|
|
25
|
|
|
private $runLevels = array(); |
26
|
|
|
|
27
|
|
|
public function __construct(Uri $uri, Dispatcher $eventDispatcher, array $configArray, array $defaultSettings = null) |
28
|
|
|
{ |
29
|
|
|
$this->eventDispatcher = $eventDispatcher; |
30
|
|
|
Init::registerGlobalParameter('_configuration', $this); |
31
|
|
|
|
32
|
|
|
$this->initConfigArray($configArray, $defaultSettings); |
33
|
|
|
|
34
|
|
|
if (array_key_exists('extensions', $this->configArray)) { |
35
|
|
|
$this->addListener($this->configArray['extensions']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!array_key_exists('rules', $this->configArray)) { |
39
|
|
|
$this->configArray['rules'] = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->startUri = $uri; |
43
|
|
|
$this->initRules($this->configArray['rules']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function initConfigArray(array $configArray, array $defaultSettings = null) |
47
|
|
|
{ |
48
|
|
|
if ($defaultSettings === null) { |
49
|
|
|
$defaultSettings = Yaml::parse(file_get_contents(__DIR__ . '/../settings/' . self::DEFAULT_SETTINGS)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (count($configArray) === 0) { |
53
|
|
|
$configArray = $defaultSettings; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (array_key_exists('options', $configArray)) { |
57
|
|
|
if (array_key_exists('extendDefault', $configArray['options'])) { |
58
|
|
|
if ($configArray['options']['extendDefault'] === true) { |
59
|
|
|
$configArray = array_replace_recursive($defaultSettings, $configArray); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->configArray = $configArray; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function addListener(array $listenerArray) |
68
|
|
|
{ |
69
|
|
|
foreach ($listenerArray as $key => $listenerConfig) { |
70
|
|
|
$extension = Init::initialize($listenerConfig); |
71
|
|
|
$this->extensions[$key] = $extension; |
72
|
|
|
$this->eventDispatcher->connectListener($extension); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Uri |
78
|
|
|
*/ |
79
|
|
|
public function getStartUri() |
80
|
|
|
{ |
81
|
|
|
return $this->startUri; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This function initializes all the rules and sets the log level. |
86
|
|
|
* |
87
|
|
|
* @param array $rulesArray |
88
|
|
|
*/ |
89
|
|
|
private function initRules(array $rulesArray) |
90
|
|
|
{ |
91
|
|
|
$this->rules = Init::initializeAll($rulesArray); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the log level of a given rule. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getRuleRunLevel($key) |
102
|
|
|
{ |
103
|
|
|
return $this->runLevels[$key]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Rule[] |
108
|
|
|
*/ |
109
|
|
|
public function getRules() |
110
|
|
|
{ |
111
|
|
|
return $this->rules; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function hasSection($section) |
115
|
|
|
{ |
116
|
|
|
return array_key_exists($section, $this->configArray); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $section |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getSection($section) |
125
|
|
|
{ |
126
|
|
|
if ($this->hasSection($section)) { |
127
|
|
|
return $this->configArray[$section]; |
128
|
|
|
} else { |
129
|
|
|
throw new \RuntimeException('The section (' . $section . ') you are trying to access does not exist.'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
public function getExtension($name) |
134
|
|
|
{ |
135
|
|
|
if (array_key_exists($name, $this->extensions)) { |
136
|
|
|
return $this->extensions[$name]; |
137
|
|
|
} else { |
138
|
|
|
throw new \RuntimeException('The extension ("' . $name . '") you are trying to access does not exist. Registered extensions are: ' . implode(' ,', array_keys($this->extensions)) . '.'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function addExtension($name, $extension) |
143
|
|
|
{ |
144
|
|
|
$this->extensions[$name] = $extension; |
145
|
|
|
$this->eventDispatcher->connectListener($extension); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getConfigArray() |
149
|
|
|
{ |
150
|
|
|
return $this->configArray; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|