1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\System\Cron; |
4
|
|
|
|
5
|
|
|
use AppendIterator; |
6
|
|
|
use Mage; |
7
|
|
|
use Mage_Core_Model_Config_Element; |
8
|
|
|
use Mage_Cron_Exception; |
9
|
|
|
use Mage_Cron_Model_Schedule; |
10
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
11
|
|
|
use Traversable; |
12
|
|
|
|
13
|
|
|
abstract class AbstractCronCommand extends AbstractMagentoCommand |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
protected function getJobs() |
19
|
|
|
{ |
20
|
|
|
$table = array(); |
21
|
|
|
|
22
|
|
|
$jobs = $this->getJobConfigElements(); |
23
|
|
|
|
24
|
|
|
foreach ($jobs as $name => $job) { |
25
|
|
|
$table[$name] = array('Job' => $name) + $this->getSchedule($job); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
ksort($table, SORT_STRING); |
29
|
|
|
|
30
|
|
|
return $table; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Mage_Core_Model_Config_Element $job |
35
|
|
|
* @return array of five cron values,keyed by 'm', 'h', 'D', 'M' and 'WD' |
36
|
|
|
*/ |
37
|
|
|
protected function getSchedule(Mage_Core_Model_Config_Element $job) |
38
|
|
|
{ |
39
|
|
|
$keys = array('m', 'h', 'D', 'M', 'WD'); |
40
|
|
|
$expr = null; |
41
|
|
|
|
42
|
|
|
if (isset($job->schedule->config_path)) { |
43
|
|
|
$expr = Mage::getStoreConfig((string)$job->schedule->config_path); |
44
|
|
|
} elseif (isset($job->schedule->cron_expr)) { |
45
|
|
|
$expr = $job->schedule->cron_expr; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($cronExpressions = $this->parseCronExpression($expr)) { |
49
|
|
|
return array_combine($keys, $cronExpressions); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return array_combine($keys, array_fill(0, 5, ' ')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get job configuration from XML and database. Expression priority is given to the database. |
57
|
|
|
* |
58
|
|
|
* @return Traversable|Mage_Core_Model_Config_Element[] |
59
|
|
|
*/ |
60
|
|
|
private function getJobConfigElements() |
61
|
|
|
{ |
62
|
|
|
$jobs = new AppendIterator(); |
63
|
|
|
|
64
|
|
|
$paths = array('crontab/jobs', 'default/crontab/jobs'); |
65
|
|
|
|
66
|
|
|
foreach ($paths as $path) { |
67
|
|
|
if ($jobConfig = Mage::getConfig()->getNode($path)) { |
68
|
|
|
$jobs->append(new \IteratorIterator($jobConfig->children())); |
69
|
|
|
}; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $jobs; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* parse a cron expression into an array, false-ly if unable to handle |
77
|
|
|
* |
78
|
|
|
* uses magento 1 internal parser of cron expressions |
79
|
|
|
* |
80
|
|
|
* @return array with five values (zero-indexed) or FALSE in case it does not exists. |
81
|
|
|
*/ |
82
|
|
|
private function parseCronExpression($expr) |
83
|
|
|
{ |
84
|
|
|
if ($expr === 'always') { |
85
|
|
|
return array_fill(0, 5, '*'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var $schedule Mage_Cron_Model_Schedule */ |
89
|
|
|
$schedule = Mage::getModel('cron/schedule'); |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$schedule->setCronExpr($expr); |
93
|
|
|
} catch (Mage_Cron_Exception $e) { |
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$array = $schedule->getData('cron_expr_arr'); |
98
|
|
|
|
99
|
|
|
$array = array_slice($array, 0, 5); // year is optional and never parsed |
100
|
|
|
|
101
|
|
|
// parse each entry |
102
|
|
|
foreach ($array as $expression) { |
103
|
|
|
try { |
104
|
|
|
$schedule->matchCronExpression($expression, 1); |
105
|
|
|
} catch (Mage_Cron_Exception $e) { |
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $array; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: