Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
14 class Config
15 {
21  protected $_xml = null;
22 
28  protected $_elementClass = \Magento\Framework\Simplexml\Element::class;
29 
35  protected $_xpathExtends = "//*[@extends]";
36 
45  public function __construct($sourceData = null)
46  {
47  if ($sourceData === null) {
48  return;
49  }
50  if ($sourceData instanceof Element) {
51  $this->setXml($sourceData);
52  } elseif (is_string($sourceData) && !empty($sourceData)) {
53  if (strlen($sourceData) < 1000 && is_readable($sourceData)) {
54  $this->loadFile($sourceData);
55  } else {
56  $this->loadString($sourceData);
57  }
58  }
59  }
60 
67  public function setXml(Element $node)
68  {
69  $this->_xml = $node;
70  return $this;
71  }
72 
80  public function getNode($path = null)
81  {
82  if (!$this->getXml() instanceof Element) {
83  return false;
84  } elseif ($path === null) {
85  return $this->getXml();
86  } else {
87  return $this->getXml()->descend($path);
88  }
89  }
90 
97  public function getXpath($xpath)
98  {
99  $xml = $this->getXml();
100  if (empty($xml)) {
101  return false;
102  }
103 
104  if (!($result = @$xml->xpath($xpath))) {
105  return false;
106  }
107 
108  return $result;
109  }
110 
116  public function getXmlString()
117  {
118  return $this->getNode()->asNiceXml('', false);
119  }
120 
127  public function loadFile($filePath)
128  {
129  if (!is_readable($filePath)) {
130  //throw new \Exception('Can not read xml file '.$filePath);
131  return false;
132  }
133 
134  $fileData = file_get_contents($filePath);
135  $fileData = $this->processFileData($fileData);
136  return $this->loadString($fileData);
137  }
138 
145  public function loadString($string)
146  {
147  if (!empty($string)) {
148  $xml = simplexml_load_string($string, $this->_elementClass);
149  if ($xml) {
150  $this->setXml($xml);
151  return true;
152  }
153  }
154  return false;
155  }
156 
163  public function loadDom(\DOMNode $dom)
164  {
165  $xml = simplexml_import_dom($dom, $this->_elementClass);
166  if ($xml) {
167  $this->setXml($xml);
168  return true;
169  }
170 
171  return false;
172  }
173 
182  public function setNode($path, $value, $overwrite = true)
183  {
184  $this->getXml()->setNode($path, $value, $overwrite);
185  return $this;
186  }
187 
193  public function applyExtends()
194  {
195  $targets = $this->getXpath($this->_xpathExtends);
196  if (!$targets) {
197  return $this;
198  }
199  foreach ($targets as $target) {
200  $sources = $this->getXpath((string)$target['extends']);
201  if ($sources) {
202  foreach ($sources as $source) {
203  $target->extend($source);
204  }
205  }
206  }
207  return $this;
208  }
209 
216  public function processFileData($text)
217  {
218  return $text;
219  }
220 
228  public function extend(Config $config, $overwrite = true)
229  {
230  $this->getNode()->extend($config->getNode(), $overwrite);
231  return $this;
232  }
233 
242  public function __destruct()
243  {
244  $this->_xml = null;
245  }
246 
252  protected function getXml()
253  {
254  return $this->_xml;
255  }
256 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
__construct($sourceData=null)
Definition: Config.php:45
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$value
Definition: gender.phtml:16
extend(Config $config, $overwrite=true)
Definition: Config.php:228
setNode($path, $value, $overwrite=true)
Definition: Config.php:182