1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace N98\Magento\Application; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use N98\Util\ArrayFunctions; |
12
|
|
|
use RuntimeException; |
13
|
|
|
use SplFileInfo; |
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ConfigFileParser |
18
|
|
|
* |
19
|
|
|
* @package N98\Magento\Application |
20
|
|
|
*/ |
21
|
|
|
class ConfigFile |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $buffer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $path |
35
|
|
|
* @return ConfigFile |
36
|
|
|
* @throws InvalidArgumentException if $path is invalid (can't be read for whatever reason) |
37
|
|
|
*/ |
38
|
|
|
public static function createFromFile($path) |
39
|
|
|
{ |
40
|
|
|
if (!is_readable($path)) { |
41
|
|
|
throw new InvalidArgumentException(sprintf("Config-file is not readable: '%s'", $path)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$configFile = new static(); |
45
|
|
|
$configFile->loadFile($path); |
46
|
|
|
|
47
|
|
|
return $configFile; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $path |
52
|
|
|
*/ |
53
|
|
|
public function loadFile($path) |
54
|
|
|
{ |
55
|
|
|
$this->path = $path; |
56
|
|
|
$buffer = file_get_contents($path); |
57
|
|
|
if (!is_string($buffer)) { |
58
|
|
|
throw new InvalidArgumentException(sprintf("Invalid path for config file: '%s'", $path)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->setBuffer($buffer); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $buffer |
66
|
|
|
*/ |
67
|
|
|
public function setBuffer($buffer) |
68
|
|
|
{ |
69
|
|
|
$this->buffer = $buffer; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $magentoRootFolder |
74
|
|
|
* @param null|SplFileInfo $file [optional] |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function applyVariables($magentoRootFolder, SplFileInfo $file = null) |
79
|
|
|
{ |
80
|
|
|
$replace = array( |
81
|
|
|
'%module%' => $file ? $file->getPath() : '', |
82
|
|
|
'%root%' => $magentoRootFolder, |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$this->buffer = strtr($this->buffer, $replace); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws RuntimeException |
90
|
|
|
*/ |
91
|
|
|
public function toArray() |
92
|
|
|
{ |
93
|
|
|
$result = Yaml::parse($this->buffer); |
94
|
|
|
|
95
|
|
|
if (!is_array($result)) { |
96
|
|
|
throw new RuntimeException(sprintf("Failed to parse config-file '%s'", $this->path)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function mergeArray(array $array) |
103
|
|
|
{ |
104
|
|
|
$result = $this->toArray(); |
105
|
|
|
|
106
|
|
|
return ArrayFunctions::mergeArrays($array, $result); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|