|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml; |
|
4
|
|
|
|
|
5
|
|
|
use MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml\Element\ConfigElementInterface; |
|
6
|
|
|
use PhpSpec\Util\Filesystem; |
|
7
|
|
|
use PrettyXml\Formatter; |
|
8
|
|
|
|
|
9
|
|
|
class ConfigGenerator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $path; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Filesystem |
|
18
|
|
|
*/ |
|
19
|
|
|
private $filesystem; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Formatter |
|
23
|
|
|
*/ |
|
24
|
|
|
private $formatter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $directory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $elementGenerators = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct($path, Filesystem $filesystem, Formatter $formatter, $codePool = 'local') |
|
40
|
|
|
{ |
|
41
|
|
|
$this->path = $path . $codePool . DIRECTORY_SEPARATOR; |
|
42
|
|
|
$this->filesystem = $filesystem; |
|
43
|
|
|
$this->formatter = $formatter; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function addElementGenerator(ConfigElementInterface $elementGenerator) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->elementGenerators[] = $elementGenerator; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $type |
|
53
|
|
|
*/ |
|
54
|
|
|
public function generateElement($type, $moduleName) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->directory = $this->getDirectoryPath($moduleName); |
|
57
|
|
|
|
|
58
|
|
|
$xml = new \SimpleXMLElement($this->getCurrentConfigXml($moduleName)); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->elementGenerators as $elementGenerator) { |
|
61
|
|
|
if ($elementGenerator->supports($type)) { |
|
62
|
|
|
if ($elementGenerator->elementExistsInXml($xml, $type, $moduleName)) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
$elementGenerator->addElementToXml($xml, $type, $moduleName); |
|
66
|
|
|
$formatted = $this->getIndentedXml($xml); |
|
67
|
|
|
$this->writeConfigFile($formatted); |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
throw new XmlGeneratorException('No element generator found for type: '.$type); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getDirectoryPath($moduleName) |
|
76
|
|
|
{ |
|
77
|
|
|
$modulePath = str_replace('_', DIRECTORY_SEPARATOR, $moduleName); |
|
78
|
|
|
return $this->path . $modulePath . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function getCurrentConfigXml($moduleName) |
|
82
|
|
|
{ |
|
83
|
|
|
if (!$this->moduleFileExists()) { |
|
84
|
|
|
$values = array( |
|
85
|
|
|
'%module_name%' => $moduleName |
|
86
|
|
|
); |
|
87
|
|
|
return strtr(file_get_contents(__DIR__ . '/templates/config.template'), $values); |
|
88
|
|
|
} |
|
89
|
|
|
return $this->filesystem->getFileContents($this->getFilePath()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getFilePath() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->directory . 'config.xml'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function moduleFileExists() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->filesystem->pathExists($this->getFilePath()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function getIndentedXml(\SimpleXMLElement $xml) |
|
103
|
|
|
{ |
|
104
|
|
|
$xmlText = $xml->asXML(); |
|
105
|
|
|
|
|
106
|
|
|
if (false === $xmlText) { |
|
107
|
|
|
throw new \RuntimeException('Failed to convert XML object to string'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->formatter->format($xmlText); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $xml |
|
115
|
|
|
*/ |
|
116
|
|
|
private function writeConfigFile($xml) |
|
117
|
|
|
{ |
|
118
|
|
|
if (!$this->filesystem->isDirectory($this->directory)) { |
|
119
|
|
|
$this->filesystem->makeDirectory($this->directory); |
|
120
|
|
|
} |
|
121
|
|
|
$this->filesystem->putFileContents($this->getFilePath(), $xml); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|