Cron.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Event cron observer object
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Event\Observer;
  12. class Cron extends \Magento\Framework\Event\Observer
  13. {
  14. /**
  15. * Checks the observer's cron string against event's name
  16. *
  17. * Supports $this->setCronExpr('* 0-5,10-59/5 2-10,15-25 january-june/2 mon-fri')
  18. *
  19. * @param \Magento\Framework\Event $event
  20. * @return boolean
  21. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  22. */
  23. public function isValidFor(\Magento\Framework\Event $event)
  24. {
  25. $e = preg_split('#\s+#', $this->getCronExpr(), null, PREG_SPLIT_NO_EMPTY);
  26. if (sizeof($e) !== 5) {
  27. return false;
  28. }
  29. $d = getdate($this->getNow());
  30. return $this->matchCronExpression(
  31. $e[0],
  32. $d['minutes']
  33. ) && $this->matchCronExpression(
  34. $e[1],
  35. $d['hours']
  36. ) && $this->matchCronExpression(
  37. $e[2],
  38. $d['mday']
  39. ) && $this->matchCronExpression(
  40. $e[3],
  41. $d['mon']
  42. ) && $this->matchCronExpression(
  43. $e[4],
  44. $d['wday']
  45. );
  46. }
  47. /**
  48. * @return int
  49. */
  50. public function getNow()
  51. {
  52. if (!$this->hasNow()) {
  53. $this->setNow(time());
  54. }
  55. return $this->getData('now');
  56. }
  57. /**
  58. * @param string $expr
  59. * @param int $num
  60. * @return bool
  61. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  62. * @SuppressWarnings(PHPMD.NPathComplexity)
  63. */
  64. public function matchCronExpression($expr, $num)
  65. {
  66. // handle ALL match
  67. if ($expr === '*') {
  68. return true;
  69. }
  70. // handle multiple options
  71. if (strpos($expr, ',') !== false) {
  72. foreach (explode(',', $expr) as $e) {
  73. if ($this->matchCronExpression($e, $num)) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. // handle modulus
  80. if (strpos($expr, '/') !== false) {
  81. $e = explode('/', $expr);
  82. if (sizeof($e) !== 2) {
  83. return false;
  84. }
  85. $expr = $e[0];
  86. $mod = $e[1];
  87. if (!is_numeric($mod)) {
  88. return false;
  89. }
  90. } else {
  91. $mod = 1;
  92. }
  93. // handle range
  94. if (strpos($expr, '-') !== false) {
  95. $e = explode('-', $expr);
  96. if (sizeof($e) !== 2) {
  97. return false;
  98. }
  99. $from = $this->getNumeric($e[0]);
  100. $to = $this->getNumeric($e[1]);
  101. return $from !== false && $to !== false && $num >= $from && $num <= $to && $num % $mod === 0;
  102. }
  103. // handle regular token
  104. $value = $this->getNumeric($expr);
  105. return $value !== false && $num == $value && $num % $mod === 0;
  106. }
  107. /**
  108. * @param int|string $value
  109. * @return bool|string
  110. */
  111. public function getNumeric($value)
  112. {
  113. static $data = [
  114. 'jan' => 1,
  115. 'feb' => 2,
  116. 'mar' => 3,
  117. 'apr' => 4,
  118. 'may' => 5,
  119. 'jun' => 6,
  120. 'jul' => 7,
  121. 'aug' => 8,
  122. 'sep' => 9,
  123. 'oct' => 10,
  124. 'nov' => 11,
  125. 'dec' => 12,
  126. 'sun' => 0,
  127. 'mon' => 1,
  128. 'tue' => 2,
  129. 'wed' => 3,
  130. 'thu' => 4,
  131. 'fri' => 5,
  132. 'sat' => 6,
  133. ];
  134. if (is_numeric($value)) {
  135. return $value;
  136. }
  137. if (is_string($value)) {
  138. $value = strtolower(substr($value, 0, 3));
  139. if (isset($data[$value])) {
  140. return $data[$value];
  141. }
  142. }
  143. return false;
  144. }
  145. }