]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Cron.php
Documentation + filename uniqueness in File class
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Cron.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
23  * Allows callbacks to be registered for asynchronous execution.
24  *
25  * @category Phergie
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
30  */
31 class Phergie_Plugin_Cron extends Phergie_Plugin_Abstract
32 {
33     /**
34      * Array of all registered callbacks with delays and arguments
35      *
36      * @var array
37      */
38     protected $callbacks;
39
40     /**
41      * Returns a human-readable representation of a callback for debugging
42      * purposes.
43      *
44      * @param callback $callback Callback to analyze
45      *
46      * @return string|boolean String representation of the callback or FALSE
47      *         if the specified value is not a valid callback
48      */
49     protected function getCallbackString($callback)
50     {
51         if (!is_callable($callback)) {
52             return false;
53         }
54
55         if (is_array($callback)) {
56             $class = is_string($callback[0]) ?
57                 $callback[0] : get_class($callback[0]);
58             $method = $class . '::' . $callback[1];
59             return $method;
60         }
61
62         return $callback;
63     }
64
65     /**
66      * Registers a callback for execution sometime after a given delay
67      * relative to now.
68      *
69      * @param callback $callback  Callback to be registered
70      * @param int      $delay     Delay in seconds from now when the callback
71      *        will be executed
72      * @param array    $arguments Arguments to pass to the callback when
73      *        it's executed
74      * @param bool     $repeat    TRUE to automatically re-register the
75      *        callback for the same delay after it's executed, FALSE
76      *        otherwise
77      *
78      * @return void
79      */
80     public function registerCallback($callback, $delay,
81         array $arguments = array(), $repeat = false)
82     {
83         $callbackString = $this->getCallbackString($callback);
84         if ($callbackString === false) {
85             echo 'DEBUG(Cron): Invalid callback specified - ',
86                 var_export($callback, true), PHP_EOL;
87             return;
88         }
89
90         $registered = time();
91         $scheduled = $registered + $delay;
92
93         $this->callbacks[] = array(
94             'callback'   => $callback,
95             'delay'      => $delay,
96             'arguments'  => $arguments,
97             'registered' => $registered,
98             'scheduled'  => $scheduled,
99             'repeat'     => $repeat,
100         );
101
102         echo 'DEBUG(Cron): Callback ', $callbackString,
103             ' scheduled for ', date('H:i:s', $scheduled), PHP_EOL;
104     }
105
106     /**
107      * Handles callback execution.
108      *
109      * @return void
110      */
111     public function onTick()
112     {
113         $time = time();
114         foreach ($this->callbacks as $key => &$callback) {
115             $callbackString = $this->getCallbackString($callback);
116
117             $scheduled = $callback['scheduled'];
118             if ($time < $scheduled) {
119                 continue;
120             }
121
122             if (empty($callback['arguments'])) {
123                 call_user_func($callback['callback']);
124             } else {
125                 call_user_func_array(
126                     $callback['callback'],
127                     $callback['arguments']
128                 );
129             }
130
131             echo 'DEBUG(Cron): Callback ', $callbackString,
132                 ' scheduled for ', date('H:i:s', $scheduled), ',',
133                 ' executed at ', date('H:i:s', $now), PHP_EOL;
134
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']),
139                     PHP_EOL;
140             } else {
141                 echo 'DEBUG(Cron): Callback ', $callbackString,
142                     ' removed from callback list', PHP_EOL;
143                 unset($this->callbacks[$key]);
144             }
145         }
146     }
147 }