Xml.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cron\Model\Config\Converter;
  7. /**
  8. * Converts cron parameters from XML files
  9. */
  10. class Xml implements \Magento\Framework\Config\ConverterInterface
  11. {
  12. /**
  13. * Converting data to array type
  14. *
  15. * @param \DOMDocument $source
  16. * @return array
  17. * @throws \InvalidArgumentException
  18. */
  19. public function convert($source)
  20. {
  21. $output = [];
  22. if (!$source instanceof \DOMDocument) {
  23. return $output;
  24. }
  25. $groups = $source->getElementsByTagName('group');
  26. foreach ($groups as $group) {
  27. /** @var $group \DOMElement */
  28. if (!$group->hasAttribute('id')) {
  29. throw new \InvalidArgumentException('Attribute "id" does not exist');
  30. }
  31. /** @var \DOMElement $jobConfig */
  32. foreach ($group->childNodes as $jobConfig) {
  33. if ($jobConfig->nodeName != 'job') {
  34. continue;
  35. }
  36. $jobName = $jobConfig->getAttribute('name');
  37. if (!$jobName) {
  38. throw new \InvalidArgumentException('Attribute "name" does not exist');
  39. }
  40. $config = [];
  41. $config['name'] = $jobName;
  42. $config += $this->convertCronConfig($jobConfig);
  43. $config += $this->convertCronSchedule($jobConfig);
  44. $config += $this->convertCronConfigPath($jobConfig);
  45. $output[$group->getAttribute('id')][$jobName] = $config;
  46. }
  47. }
  48. return $output;
  49. }
  50. /**
  51. * Convert specific cron configurations
  52. *
  53. * @param \DOMElement $jobConfig
  54. * @return array
  55. * @throws \InvalidArgumentException
  56. */
  57. protected function convertCronConfig(\DOMElement $jobConfig)
  58. {
  59. $instanceName = $jobConfig->getAttribute('instance');
  60. $methodName = $jobConfig->getAttribute('method');
  61. if (!isset($instanceName)) {
  62. throw new \InvalidArgumentException('Attribute "instance" does not exist');
  63. }
  64. if (!isset($methodName)) {
  65. throw new \InvalidArgumentException('Attribute "method" does not exist');
  66. }
  67. return ['instance' => $instanceName, 'method' => $methodName];
  68. }
  69. /**
  70. * Convert schedule cron configurations
  71. *
  72. * @param \DOMElement $jobConfig
  73. * @return array
  74. */
  75. protected function convertCronSchedule(\DOMElement $jobConfig)
  76. {
  77. $result = [];
  78. /** @var \DOMText $schedules */
  79. foreach ($jobConfig->childNodes as $schedules) {
  80. if ($schedules->nodeName == 'schedule') {
  81. if (!empty($schedules->nodeValue)) {
  82. $result['schedule'] = $schedules->nodeValue;
  83. break;
  84. }
  85. }
  86. continue;
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Convert schedule cron configurations
  92. *
  93. * @param \DOMElement $jobConfig
  94. * @return array
  95. */
  96. protected function convertCronConfigPath(\DOMElement $jobConfig)
  97. {
  98. $result = [];
  99. /** @var \DOMText $schedules */
  100. foreach ($jobConfig->childNodes as $schedules) {
  101. if ($schedules->nodeName == 'config_path') {
  102. if (!empty($schedules->nodeValue)) {
  103. $result['config_path'] = $schedules->nodeValue;
  104. break;
  105. }
  106. }
  107. continue;
  108. }
  109. return $result;
  110. }
  111. }