Converter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Converter of event observers configuration from \DOMDocument to tree array
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Event\Config;
  9. class Converter implements \Magento\Framework\Config\ConverterInterface
  10. {
  11. /**
  12. * Convert dom node tree to array
  13. *
  14. * @param \DOMDocument $source
  15. * @return array
  16. * @throws \InvalidArgumentException
  17. */
  18. public function convert($source)
  19. {
  20. $output = [];
  21. /** @var \DOMNodeList $events */
  22. $events = $source->getElementsByTagName('event');
  23. /** @var \DOMNode $eventConfig */
  24. foreach ($events as $eventConfig) {
  25. $eventName = $eventConfig->attributes->getNamedItem('name')->nodeValue;
  26. $eventObservers = [];
  27. /** @var \DOMNode $observerConfig */
  28. foreach ($eventConfig->childNodes as $observerConfig) {
  29. if ($observerConfig->nodeName != 'observer' || $observerConfig->nodeType != XML_ELEMENT_NODE) {
  30. continue;
  31. }
  32. $observerNameNode = $observerConfig->attributes->getNamedItem('name');
  33. if (!$observerNameNode) {
  34. throw new \InvalidArgumentException('Attribute name is missed');
  35. }
  36. $config = $this->_convertObserverConfig($observerConfig);
  37. $config['name'] = $observerNameNode->nodeValue;
  38. $eventObservers[$observerNameNode->nodeValue] = $config;
  39. }
  40. $output[mb_strtolower($eventName)] = $eventObservers;
  41. }
  42. return $output;
  43. }
  44. /**
  45. * Convert observer configuration
  46. *
  47. * @param \DOMNode $observerConfig
  48. * @return array
  49. */
  50. public function _convertObserverConfig($observerConfig)
  51. {
  52. $output = [];
  53. /** Parse instance configuration */
  54. $instanceAttribute = $observerConfig->attributes->getNamedItem('instance');
  55. if ($instanceAttribute) {
  56. $output['instance'] = $instanceAttribute->nodeValue;
  57. }
  58. /** Parse instance method configuration */
  59. $methodAttribute = $observerConfig->attributes->getNamedItem('method');
  60. if ($methodAttribute) {
  61. $output['method'] = $methodAttribute->nodeValue;
  62. }
  63. /** Parse disabled/enabled configuration */
  64. $disabledAttribute = $observerConfig->attributes->getNamedItem('disabled');
  65. if ($disabledAttribute && $disabledAttribute->nodeValue == 'true') {
  66. $output['disabled'] = true;
  67. }
  68. /** Parse shareability configuration */
  69. $shredAttribute = $observerConfig->attributes->getNamedItem('shared');
  70. if ($shredAttribute && $shredAttribute->nodeValue == 'false') {
  71. $output['shared'] = false;
  72. }
  73. return $output;
  74. }
  75. }