Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PackageXmlParser.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $_moduleDir = null;
17 
21  protected $_file = null;
22 
26  protected $_targets = array();
27 
35  public function __construct($moduleDir, $packageXmlFile, $translations = array(), $pathSuffix)
36  {
37  parent::__construct($translations, $pathSuffix);
38  $this->setModuleDir($moduleDir);
39  $this->setFile($this->getModuleDir() . '/' . $packageXmlFile);
40  }
41 
48  public function setModuleDir($moduleDir)
49  {
50  // Remove trailing slash
51  if (!is_null($moduleDir)) {
52  $moduleDir = rtrim($moduleDir, '\\/');
53  }
54 
55  $this->_moduleDir = $moduleDir;
56  return $this;
57  }
58 
62  public function getModuleDir()
63  {
64  return $this->_moduleDir;
65  }
66 
71  public function setFile($file)
72  {
73  if (is_string($file)) {
74  $file = new \SplFileObject($file);
75  }
76  $this->_file = $file;
77  return $this;
78  }
79 
83  public function getFile()
84  {
85  return $this->_file;
86  }
87 
92  public function getMappings()
93  {
94  $file = $this->getFile();
95 
96  if (!$file->isReadable()) {
97  throw new \ErrorException(sprintf('Package file "%s" not readable', $file->getPathname()));
98  }
99 
100  $map = $this->_parseMappings();
101  $map = $this->translatePathMappings($map);
102  return $map;
103  }
104 
109  protected function _parseMappings()
110  {
111  $map = array();
112 
114  $package = simplexml_load_file($this->getFile()->getPathname());
115  if (isset($package)) {
116  foreach ($package->xpath('//contents/target') as $target) {
117  try {
118  $basePath = $this->getTargetPath($target);
119 
120  foreach ($target->children() as $child) {
121  foreach ($this->getElementPaths($child) as $elementPath) {
122  $relativePath = $basePath . '/' . $elementPath;
123  $map[] = array($relativePath, $relativePath);
124  }
125  }
126 
127  }
128  catch (RuntimeException $e) {
129  // Skip invalid targets
130  throw $e;
131  continue;
132  }
133  }
134  }
135  return $map;
136  }
137 
143  protected function getTargetPath(\SimpleXMLElement $target)
144  {
145  $name = (string) $target->attributes()->name;
146  $targets = $this->getTargetsDefinitions();
147  if (! isset($targets[$name])) {
148  throw new RuntimeException('Invalid target type ' . $name);
149  }
150  return $targets[$name];
151  }
152 
156  protected function getTargetsDefinitions()
157  {
158  if (! $this->_targets) {
159 
160  $targets = simplexml_load_file(__DIR__ . '/../../../../res/target.xml');
161  foreach ($targets as $target) {
162  $attributes = $target->attributes();
163  $this->_targets["{$attributes->name}"] = "{$attributes->uri}";
164  }
165  }
166  return $this->_targets;
167  }
168 
174  protected function getElementPaths(\SimpleXMLElement $element) {
175  $type = $element->getName();
176  $name = $element->attributes()->name;
177  $elementPaths = array();
178 
179  switch ($type) {
180  case 'dir':
181  if ($element->children()) {
182  foreach ($element->children() as $child) {
183  foreach ($this->getElementPaths($child) as $elementPath) {
184  $elementPaths[] = $name == '.' ? $elementPath : $name . '/' . $elementPath;
185  }
186  }
187  } else {
188  $elementPaths[] = $name;
189  }
190  break;
191 
192  case 'file':
193  $elementPaths[] = $name;
194  break;
195 
196  default:
197  throw new RuntimeException('Unknown path type: ' . $type);
198  }
199 
200  return $elementPaths;
201  }
202 
207  protected function getFirstChild(\SimpleXMLElement$element)
208  {
209  foreach ($element->children() as $child) {
210  return $child;
211  }
212  }
213 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$target
Definition: skip.phtml:8
$type
Definition: item.phtml:13
$attributes
Definition: matrix.phtml:13
__construct($moduleDir, $packageXmlFile, $translations=array(), $pathSuffix)
$relativePath
Definition: get.php:35
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12