snippet: view plain - save this
1 <?php
2
3 class tick {
4 private $ticks = array();
5 private $ok;
6
7 static public $instance = false;
8
9 public static function GetInstance() {
10 if (!self::$instance) {
11 self::$instance = new tick();
12 }
13 return self::$instance;
14 }
15
16 private function __construct() {
17 $this->ok = true;
18 }
19
20 /**
21 * Adding a job to the tick list $tickname
22 *
23 * @param string $tickname
24 * @param string $jobname
25 * @param string $function Function to call
26 * @param array $params Parameters send to the $function
27 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
28 * @param object $object Reference to an object if $function is a method
29 * @return boolean
30 */
31 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
32 if (isset($this->ticks[$tickname])) {
33 if (!is_null($object)) {
34 $this->ticks[$tickname]['jobs'][] = array(
35 'jname' => $jobname,
36 'function' => $function,
37 'params' => $params,
38 'temporary' => $temporary,
39 'objectRef' => &$object
40 );
41 } else {
42 $this->ticks[$tickname]['jobs'][] = array(
43 'jname' => $jobname,
44 'function' => $function,
45 'params' => $params,
46 'temporary' => $temporary
47 );
48 }
49 return true;
50 }
51 return false;
52 }
53
54 /**
55 * Create a tick if not exist
56 *
57 * @param string $name
58 * @param int $timer Number of seconds before the next tick
59 * @return boolean
60 */
61 public function setTick($name,$timer) {
62 if (!isset($this->ticks[$name])) {
63 $this->ticks[$name] = array(
64 'timer' => $timer,
65 'reference' => time(),
66 'tickOn' => strtotime("+$timer seconds"),
67 'jobs' => array()
68 );
69 return true;
70 }
71 return false;
72 }
73
74 /**
75 * Delete a tick
76 *
77 * @param string $name
78 * @return boolean
79 */
80 public function delTick($name) {
81 if (isset($this->ticks[$name])) {
82 unset($this->ticks[$name]);
83 return true;
84 }
85 return false;
86 }
87
88 public function doAllTicks() {
89 $curtime = time();
90
91 foreach($this->ticks as $name => $options) {
92
93 if ($options['tickOn']<=$curtime) {
94 echo $options['tickOn']," - $curtime\n";
95
96 foreach ($options['jobs'] as $functions) {
97
98 if (!$functions['temporary']) {
99 $jlist[] = $functions;
100 }
101
102 if (!$this->ircmain->ok) {
103 continue;
104 }
105
106 switch (isset($functions['objectRef'])) {
107 case true:
108 if (!is_object($functions['objectRef'])) {
109 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
110 }
111 //print_r($functions);break;
112 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
113 break;
114
115 case false:
116 call_user_func_array($functions['function'],$functions['params']);
117 break;
118 }
119
120 }
121
122 $options['jobs'] = $jlist;
123
124 $this->ticks[$name] = array(
125 'timer' => $options['timer'],
126 'reference' => time(),
127 'tickOn' => strtotime("+{$options['timer']} seconds"),
128 'jobs' => $options['jobs']
129 );
130
131 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";
132
133 }
134
135 }
136
137 return true;
138
139 }
140 }
141
142 ?>

0 comments