1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the hogosha-monitor package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Configuration; |
17
|
|
|
|
18
|
|
|
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException; |
19
|
|
|
use Hogosha\Monitor\Monitor; |
20
|
|
|
use Symfony\Component\Yaml\Yaml; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Guillaume Cavana <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ConfigurationLoader |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* $rootDirectory. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $rootDirectory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* $configFilepath. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $configFilepath; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* $filename. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $filename; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $rootDirectory Directory containing the config file |
50
|
|
|
* @param string $filename Configuration file name |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
$rootDirectory = null, |
54
|
|
|
$filename = Monitor::CONFIG_FILENAME |
55
|
|
|
) { |
56
|
|
|
$this->rootDirectory = $rootDirectory; |
57
|
|
|
$this->filename = $filename; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* loadConfiguration. |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function loadConfiguration() |
66
|
|
|
{ |
67
|
|
|
$filePath = $this->getConfigurationFilepath(); |
68
|
|
|
|
69
|
|
|
if (!file_exists($filePath)) { |
70
|
|
|
throw new ConfigurationLoadingException( |
71
|
|
|
sprintf('Unable to find a configuration file in %s', $this->rootDirectory) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return Yaml::parse(file_get_contents($filePath)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* getConfigurationFilepath. |
80
|
|
|
* |
81
|
|
|
* @return string The configuration filepath |
82
|
|
|
*/ |
83
|
|
|
public function getConfigurationFilepath() |
84
|
|
|
{ |
85
|
|
|
return $this->configFilepath ?: $this->rootDirectory.DIRECTORY_SEPARATOR.$this->filename; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* setConfigurationFilepath. |
90
|
|
|
* |
91
|
|
|
* @param string $configFilepath The configuration filepath |
92
|
|
|
*/ |
93
|
|
|
public function setConfigurationFilepath($configFilepath) |
94
|
|
|
{ |
95
|
|
|
$this->configFilepath = $configFilepath; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* getRootDirectory. |
100
|
|
|
* |
101
|
|
|
* @return string The root directory |
102
|
|
|
*/ |
103
|
|
|
public function getRootDirectory() |
104
|
|
|
{ |
105
|
|
|
return $this->rootDirectory; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* setRootDirectory. |
110
|
|
|
* |
111
|
|
|
* @param string $rootDirectory The root directory |
112
|
|
|
*/ |
113
|
|
|
public function setRootDirectory($rootDirectory) |
114
|
|
|
{ |
115
|
|
|
$this->rootDirectory = $rootDirectory; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|