9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Cron
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Cron
23 * Allows callbacks to be registered for asynchronous execution.
26 * @package Phergie_Plugin_Cron
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie_Plugin_Cron
31 class Phergie_Plugin_Cron extends Phergie_Plugin_Abstract
34 * Array of all registered callbacks with delays and arguments
41 * Returns a human-readable representation of a callback for debugging
44 * @param callback $callback Callback to analyze
46 * @return string|boolean String representation of the callback or FALSE
47 * if the specified value is not a valid callback
49 protected function getCallbackString($callback)
51 if (!is_callable($callback)) {
55 if (is_array($callback)) {
56 $class = is_string($callback[0]) ?
57 $callback[0] : get_class($callback[0]);
58 $method = $class . '::' . $callback[1];
66 * Registers a callback for execution sometime after a given delay
69 * @param callback $callback Callback to be registered
70 * @param int $delay Delay in seconds from now when the callback
72 * @param array $arguments Arguments to pass to the callback when
74 * @param bool $repeat TRUE to automatically re-register the
75 * callback for the same delay after it's executed, FALSE
80 public function registerCallback($callback, $delay,
81 array $arguments = array(), $repeat = false)
83 $callbackString = $this->getCallbackString($callback);
84 if ($callbackString === false) {
85 echo 'DEBUG(Cron): Invalid callback specified - ',
86 var_export($callback, true), PHP_EOL;
91 $scheduled = $registered + $delay;
93 $this->callbacks[] = array(
94 'callback' => $callback,
96 'arguments' => $arguments,
97 'registered' => $registered,
98 'scheduled' => $scheduled,
102 echo 'DEBUG(Cron): Callback ', $callbackString,
103 ' scheduled for ', date('H:i:s', $scheduled), PHP_EOL;
107 * Handles callback execution.
111 public function onTick()
114 foreach ($this->callbacks as $key => &$callback) {
115 $callbackString = $this->getCallbackString($callback);
117 $scheduled = $callback['scheduled'];
118 if ($time < $scheduled) {
122 if (empty($callback['arguments'])) {
123 call_user_func($callback['callback']);
125 call_user_func_array(
126 $callback['callback'],
127 $callback['arguments']
131 echo 'DEBUG(Cron): Callback ', $callbackString,
132 ' scheduled for ', date('H:i:s', $scheduled), ',',
133 ' executed at ', date('H:i:s', $now), PHP_EOL;
135 if ($callback['repeat']) {
136 $callback['scheduled'] = $time + $callback['delay'];
137 echo 'DEBUG(Cron): Callback ', $callbackString,
138 ' scheduled for ', date('H:i:s', $callback['scheduled']),
141 echo 'DEBUG(Cron): Callback ', $callbackString,
142 ' removed from callback list', PHP_EOL;
143 unset($this->callbacks[$key]);