PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/Simplexml/Config.php

https://gitlab.com/daigiangaitu91/magento
PHP | 589 lines | 264 code | 55 blank | 270 comment | 34 complexity | 570d4e8e0f62af08313811362446e2c3 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Simplexml;
  7. /**
  8. * Base class for simplexml based configurations
  9. */
  10. class Config
  11. {
  12. /**
  13. * Configuration xml
  14. *
  15. * @var Element
  16. */
  17. protected $_xml = null;
  18. /**
  19. * Enter description here...
  20. *
  21. * @var string
  22. */
  23. protected $_cacheId = null;
  24. /**
  25. * Enter description here...
  26. *
  27. * @var array
  28. */
  29. protected $_cacheTags = [];
  30. /**
  31. * Enter description here...
  32. *
  33. * @var int
  34. */
  35. protected $_cacheLifetime = null;
  36. /**
  37. * Enter description here...
  38. *
  39. * @var string|null|false
  40. */
  41. protected $_cacheChecksum = false;
  42. /**
  43. * Enter description here...
  44. *
  45. * @var boolean
  46. */
  47. protected $_cacheSaved = false;
  48. /**
  49. * Cache resource object
  50. *
  51. * @var Config\Cache\AbstractCache
  52. */
  53. protected $_cache = null;
  54. /**
  55. * Class name of simplexml elements for this configuration
  56. *
  57. * @var string
  58. */
  59. protected $_elementClass = 'Magento\Framework\Simplexml\Element';
  60. /**
  61. * Xpath describing nodes in configuration that need to be extended
  62. *
  63. * @example <allResources extends="/config/modules//resource"/>
  64. */
  65. protected $_xpathExtends = "//*[@extends]";
  66. /**
  67. * Constructor
  68. *
  69. * Initializes XML for this configuration
  70. *
  71. * @see \Magento\Framework\Simplexml\Config::setXml
  72. * @param string|Element $sourceData
  73. */
  74. public function __construct($sourceData = null)
  75. {
  76. if ($sourceData === null) {
  77. return;
  78. }
  79. if ($sourceData instanceof Element) {
  80. $this->setXml($sourceData);
  81. } elseif (is_string($sourceData) && !empty($sourceData)) {
  82. if (strlen($sourceData) < 1000 && is_readable($sourceData)) {
  83. $this->loadFile($sourceData);
  84. } else {
  85. $this->loadString($sourceData);
  86. }
  87. }
  88. }
  89. /**
  90. * Sets xml for this configuration
  91. *
  92. * @param Element $node
  93. * @return $this
  94. */
  95. public function setXml(Element $node)
  96. {
  97. $this->_xml = $node;
  98. return $this;
  99. }
  100. /**
  101. * Returns node found by the $path
  102. *
  103. * @see \Magento\Framework\Simplexml\Element::descend
  104. * @param string $path
  105. * @return Element|bool
  106. */
  107. public function getNode($path = null)
  108. {
  109. if (!$this->getXml() instanceof Element) {
  110. return false;
  111. } elseif ($path === null) {
  112. return $this->getXml();
  113. } else {
  114. return $this->getXml()->descend($path);
  115. }
  116. }
  117. /**
  118. * Returns nodes found by xpath expression
  119. *
  120. * @param string $xpath
  121. * @return Element[]|bool
  122. */
  123. public function getXpath($xpath)
  124. {
  125. $xml = $this->getXml();
  126. if (empty($xml)) {
  127. return false;
  128. }
  129. if (!($result = @$xml->xpath($xpath))) {
  130. return false;
  131. }
  132. return $result;
  133. }
  134. /**
  135. * Enter description here...
  136. *
  137. * @param Config\Cache\AbstractCache $cache
  138. * @return $this
  139. */
  140. public function setCache($cache)
  141. {
  142. $this->_cache = $cache;
  143. return $this;
  144. }
  145. /**
  146. * Enter description here...
  147. *
  148. * @return Config\Cache\AbstractCache
  149. */
  150. public function getCache()
  151. {
  152. return $this->_cache;
  153. }
  154. /**
  155. * Enter description here...
  156. *
  157. * @param boolean $flag
  158. * @return $this
  159. */
  160. public function setCacheSaved($flag)
  161. {
  162. $this->_cacheSaved = $flag;
  163. return $this;
  164. }
  165. /**
  166. * Enter description here...
  167. *
  168. * @return boolean
  169. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  170. */
  171. public function getCacheSaved()
  172. {
  173. return $this->_cacheSaved;
  174. }
  175. /**
  176. * Enter description here...
  177. *
  178. * @param string $id
  179. * @return $this
  180. */
  181. public function setCacheId($id)
  182. {
  183. $this->_cacheId = $id;
  184. return $this;
  185. }
  186. /**
  187. * Enter description here...
  188. *
  189. * @return string
  190. */
  191. public function getCacheId()
  192. {
  193. return $this->_cacheId;
  194. }
  195. /**
  196. * Enter description here...
  197. *
  198. * @param array $tags
  199. * @return $this
  200. */
  201. public function setCacheTags($tags)
  202. {
  203. $this->_cacheTags = $tags;
  204. return $this;
  205. }
  206. /**
  207. * Enter description here...
  208. *
  209. * @return array
  210. */
  211. public function getCacheTags()
  212. {
  213. return $this->_cacheTags;
  214. }
  215. /**
  216. * Enter description here...
  217. *
  218. * @param int $lifetime
  219. * @return $this
  220. */
  221. public function setCacheLifetime($lifetime)
  222. {
  223. $this->_cacheLifetime = $lifetime;
  224. return $this;
  225. }
  226. /**
  227. * Enter description here...
  228. *
  229. * @return int
  230. */
  231. public function getCacheLifetime()
  232. {
  233. return $this->_cacheLifetime;
  234. }
  235. /**
  236. * Enter description here...
  237. *
  238. * @param string $data
  239. * @return $this
  240. */
  241. public function setCacheChecksum($data)
  242. {
  243. if ($data === null) {
  244. $this->_cacheChecksum = null;
  245. } elseif (false === $data || 0 === $data) {
  246. $this->_cacheChecksum = false;
  247. } else {
  248. $this->_cacheChecksum = md5($data);
  249. }
  250. return $this;
  251. }
  252. /**
  253. * Enter description here...
  254. *
  255. * @param string $data
  256. * @return $this
  257. */
  258. public function updateCacheChecksum($data)
  259. {
  260. if (false === $this->getCacheChecksum()) {
  261. return $this;
  262. }
  263. if (false === $data || 0 === $data) {
  264. $this->_cacheChecksum = false;
  265. } else {
  266. $this->setCacheChecksum($this->getCacheChecksum() . ':' . $data);
  267. }
  268. return $this;
  269. }
  270. /**
  271. * Enter description here...
  272. *
  273. * @return string
  274. */
  275. public function getCacheChecksum()
  276. {
  277. return $this->_cacheChecksum;
  278. }
  279. /**
  280. * Enter description here...
  281. *
  282. * @return string
  283. */
  284. public function getCacheChecksumId()
  285. {
  286. return $this->getCacheId() . '__CHECKSUM';
  287. }
  288. /**
  289. * Enter description here...
  290. *
  291. * @return boolean
  292. */
  293. public function fetchCacheChecksum()
  294. {
  295. return false;
  296. }
  297. /**
  298. * Enter description here...
  299. *
  300. * @return boolean
  301. */
  302. public function validateCacheChecksum()
  303. {
  304. $newChecksum = $this->getCacheChecksum();
  305. if (false === $newChecksum) {
  306. return false;
  307. }
  308. if ($newChecksum === null) {
  309. return true;
  310. }
  311. $cachedChecksum = $this->getCache()->load($this->getCacheChecksumId());
  312. return $newChecksum === false && $cachedChecksum === false || $newChecksum === $cachedChecksum;
  313. }
  314. /**
  315. * Enter description here...
  316. *
  317. * @return boolean
  318. */
  319. public function loadCache()
  320. {
  321. if (!$this->validateCacheChecksum()) {
  322. return false;
  323. }
  324. $xmlString = $this->_loadCache($this->getCacheId());
  325. if ($this->loadString($xmlString)) {
  326. $this->setCacheSaved(true);
  327. return true;
  328. }
  329. return false;
  330. }
  331. /**
  332. * Enter description here...
  333. *
  334. * @param array $tags
  335. * @return $this
  336. */
  337. public function saveCache($tags = null)
  338. {
  339. if ($this->getCacheSaved() || $this->getCacheChecksum() === false) {
  340. return $this;
  341. }
  342. if ($tags === null) {
  343. $tags = $this->getCacheTags();
  344. }
  345. if ($this->getCacheChecksum() === null) {
  346. $this->_saveCache($this->getCacheChecksum(), $this->getCacheChecksumId(), $tags, $this->getCacheLifetime());
  347. }
  348. $this->_saveCache($this->getXmlString(), $this->getCacheId(), $tags, $this->getCacheLifetime());
  349. $this->setCacheSaved(true);
  350. return $this;
  351. }
  352. /**
  353. * Return Xml of node as string
  354. *
  355. * @return string
  356. */
  357. public function getXmlString()
  358. {
  359. return $this->getNode()->asNiceXml('', false);
  360. }
  361. /**
  362. * Enter description here...
  363. *
  364. * @return $this
  365. */
  366. public function removeCache()
  367. {
  368. $this->_removeCache($this->getCacheId());
  369. $this->_removeCache($this->getCacheChecksumId());
  370. return $this;
  371. }
  372. /**
  373. * Enter description here...
  374. *
  375. * @param string $id
  376. * @return boolean
  377. */
  378. protected function _loadCache($id)
  379. {
  380. return $this->getCache()->load($id);
  381. }
  382. /**
  383. * Enter description here...
  384. *
  385. * @param string $data
  386. * @param string $id
  387. * @param array $tags
  388. * @param int|boolean $lifetime
  389. * @return boolean
  390. */
  391. protected function _saveCache($data, $id, $tags = [], $lifetime = false)
  392. {
  393. return $this->getCache()->save($data, $id, $tags, $lifetime);
  394. }
  395. /**
  396. * Enter description here...
  397. *
  398. * @param string $id
  399. * @return mixed
  400. * @todo check this, as there are no caches that implement remove() method
  401. */
  402. protected function _removeCache($id)
  403. {
  404. return $this->getCache()->remove($id);
  405. }
  406. /**
  407. * Imports XML file
  408. *
  409. * @param string $filePath
  410. * @return boolean
  411. */
  412. public function loadFile($filePath)
  413. {
  414. if (!is_readable($filePath)) {
  415. //throw new \Exception('Can not read xml file '.$filePath);
  416. return false;
  417. }
  418. $fileData = file_get_contents($filePath);
  419. $fileData = $this->processFileData($fileData);
  420. return $this->loadString($fileData);
  421. }
  422. /**
  423. * Imports XML string
  424. *
  425. * @param string $string
  426. * @return boolean
  427. */
  428. public function loadString($string)
  429. {
  430. if (!empty($string)) {
  431. $xml = simplexml_load_string($string, $this->_elementClass);
  432. if ($xml) {
  433. $this->setXml($xml);
  434. return true;
  435. }
  436. }
  437. return false;
  438. }
  439. /**
  440. * Imports DOM node
  441. *
  442. * @param \DOMNode $dom
  443. * @return bool
  444. */
  445. public function loadDom(\DOMNode $dom)
  446. {
  447. $xml = simplexml_import_dom($dom, $this->_elementClass);
  448. if ($xml) {
  449. $this->setXml($xml);
  450. return true;
  451. }
  452. return false;
  453. }
  454. /**
  455. * Create node by $path and set its value.
  456. *
  457. * @param string $path separated by slashes
  458. * @param string $value
  459. * @param boolean $overwrite
  460. * @return $this
  461. */
  462. public function setNode($path, $value, $overwrite = true)
  463. {
  464. $this->getXml()->setNode($path, $value, $overwrite);
  465. return $this;
  466. }
  467. /**
  468. * Process configuration xml
  469. *
  470. * @return $this
  471. */
  472. public function applyExtends()
  473. {
  474. $targets = $this->getXpath($this->_xpathExtends);
  475. if (!$targets) {
  476. return $this;
  477. }
  478. foreach ($targets as $target) {
  479. $sources = $this->getXpath((string)$target['extends']);
  480. if ($sources) {
  481. foreach ($sources as $source) {
  482. $target->extend($source);
  483. }
  484. } else {
  485. #echo "Not found extend: ".(string)$target['extends'];
  486. }
  487. #unset($target['extends']);
  488. }
  489. return $this;
  490. }
  491. /**
  492. * Stub method for processing file data right after loading the file text
  493. *
  494. * @param string $text
  495. * @return string
  496. */
  497. public function processFileData($text)
  498. {
  499. return $text;
  500. }
  501. /**
  502. * Enter description here...
  503. *
  504. * @param Config $config
  505. * @param boolean $overwrite
  506. * @return $this
  507. */
  508. public function extend(Config $config, $overwrite = true)
  509. {
  510. $this->getNode()->extend($config->getNode(), $overwrite);
  511. return $this;
  512. }
  513. /**
  514. * Cleanup circular references
  515. *
  516. * Destructor should be called explicitly in order to work around the PHP bug
  517. * https://bugs.php.net/bug.php?id=62468
  518. *
  519. * @return void
  520. */
  521. public function __destruct()
  522. {
  523. $this->_xml = null;
  524. }
  525. /**
  526. * Getter for xml element
  527. *
  528. * @return Element
  529. */
  530. protected function getXml()
  531. {
  532. return $this->_xml;
  533. }
  534. }