1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml\Element; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use MageTest\PhpSpec\MagentoExtension\CodeGenerator\Generator\Xml\XmlGeneratorException; |
7
|
|
|
|
8
|
|
|
abstract class SimpleElementAbstract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \SimpleXMLElement $xml |
12
|
|
|
* @param string $type |
13
|
|
|
* @param string $moduleName |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function elementExistsInXml(\SimpleXMLElement $xml, $type, $moduleName) |
17
|
|
|
{ |
18
|
|
|
$targetElements = $xml->xpath('/config/global/'.$type.'s'); |
19
|
|
|
if (!count($targetElements)) { |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
$classElements = $xml->xpath('/config/global/'.$type.'s'.'/'.strtolower($moduleName).'/class'); |
23
|
|
|
return (bool) count($classElements); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \SimpleXMLElement $xml |
28
|
|
|
* @param string $type |
29
|
|
|
* @param string $moduleName |
30
|
|
|
* @throws XmlGeneratorException |
31
|
|
|
* @return null |
32
|
|
|
*/ |
33
|
|
|
public function addElementToXml(\SimpleXMLElement $xml, $type, $moduleName) |
34
|
|
|
{ |
35
|
|
|
$globalElements = $xml->xpath('/config/global'); |
36
|
|
|
|
37
|
|
|
if (!count($globalElements)) { |
38
|
|
|
throw new XmlGeneratorException(sprintf('Global element not found in %s config file', $moduleName)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$globalElements[0]->addChild($type.'s') |
42
|
|
|
->addChild(strtolower($moduleName)) |
43
|
|
|
->addChild('class', $moduleName . '_' . ucfirst($type)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \SimpleXMLElement $xml |
48
|
|
|
* @param string $path |
49
|
|
|
* @return \SimpleXMLElement |
50
|
|
|
*/ |
51
|
|
|
protected function getElement(\SimpleXMLElement $xml, $path) |
52
|
|
|
{ |
53
|
|
|
$elements = $xml->xpath($path); |
54
|
|
|
|
55
|
|
|
if (!count($elements)) { |
56
|
|
|
return $xml->addChild($path); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $elements[0]; |
60
|
|
|
} |
61
|
|
|
} |