PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / cron / dispatcher.php
vikappointments / site / helpers / libraries / cron Last commit date
overrides 4 days ago core.php 4 days ago dispatcher.php 4 days ago formbuilder.php 4 days ago formfield.php 4 days ago formfieldconstraints.php 4 days ago index.html 4 days ago job.php 4 days ago response.php 4 days ago
dispatcher.php
314 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Class used to dispatch a cron job.
16 *
17 * @since 1.5
18 * @since 1.7 Renamed from CronDispatcher.
19 *
20 * @see VAPCronJob
21 */
22 class VAPCronDispatcher
23 {
24 /**
25 * Base drivers path.
26 *
27 * @deprecated 1.8
28 */
29 public static $BASE_PATH = null;
30
31 /**
32 * A list of include paths.
33 *
34 * @var array
35 * @since 1.7
36 */
37 protected static $includePaths = array();
38
39 /**
40 * Class constructor.
41 */
42 private function __construct()
43 {
44 // construct is not accessible
45 }
46
47 /**
48 * Instantiates the cron job class based on the specified action.
49 *
50 * @param string $action The name of the file (case insensitive).
51 * @param integer $id The ID of the cron job.
52 * @param mixed $args The parameters of the cron job.
53 *
54 * @return mixed The CronJob object or null.
55 *
56 * @uses includeJob()
57 */
58
59 public static function getJob($action, $id = 0, $args = array())
60 {
61 // include the cronjob and get its classname
62 $classname = self::includeJob($action);
63
64 if ($classname !== null && class_exists($classname))
65 {
66 // if the classname is not NULL and the class exists,
67 // instantiate a new object
68 $obj = new $classname($id, $args);
69
70 // make sure the object in as instance of CronJob class
71 if ($obj instanceof VAPCronJob)
72 {
73 return $obj;
74 }
75 }
76
77 // return NULL if the cronjob or the getJob method don't exist
78 return null;
79 }
80
81 /**
82 * Returns the fields needed for the configuration of the cron job.
83 *
84 * @param string $action The name of the file (not case sensitive).
85 *
86 * @return mixed The CronFormField array or null.
87 *
88 * @uses includeJob()
89 */
90
91 public static function getJobConfiguration($action)
92 {
93 // include the cronjob and get its classname
94 $classname = self::getJob($action);
95
96 if ($classname !== null && method_exists($classname, 'getConfiguration'))
97 {
98 // if the classname is not NULL and the class owns the `getConfiguration`
99 // static method, return the object containing its configuration
100 return $classname->getConfiguration();
101 }
102
103 // return NULL if the cron job or `getConfiguration` method don't exist
104 return null;
105 }
106
107 /**
108 * Includes all the cron jobs contained in the base path.
109 *
110 * @param boolean $assoc True to get an associative array using
111 * the filename as keys. False to return a
112 * sequential array.
113 *
114 * @return array The list of included cron jobs.
115 *
116 * @uses includeJob()
117 * @since 1.6
118 */
119 public static function includeAll($assoc = true)
120 {
121 $pool = array();
122
123 // get all registered include paths
124 foreach (static::getIncludePaths() as $path)
125 {
126 // normalize path
127 $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
128
129 // get the list of files stored into the current path
130 foreach (glob($path . '*.php') as $file)
131 {
132 // strip the full path and take the basename only
133 $basename = basename($file);
134
135 // try to include the cron job
136 if ($job = self::getJob($basename))
137 {
138 // if associative array use the basename as key
139 // and push the cron job into the list
140 if ($assoc)
141 {
142 $pool[$basename] = $job;
143 }
144 // otherwise push the cron job only
145 else
146 {
147 $pool[] = $job;
148 }
149 }
150 }
151 }
152
153 return $pool;
154 }
155
156 /**
157 * Includes the cron job file based on the specified action.
158 * Returns the name of the class on success, otherwise NULL.
159 *
160 * @param string $action The name of the file (case insensitive).
161 *
162 * @return mixed The classname or null.
163 */
164 public static function includeJob($action)
165 {
166 if (empty($action))
167 {
168 return null;
169 }
170
171 // if exists, remove file extension from action
172 $action = preg_replace("/\.php$/i", '', $action);
173
174 // get all registered include paths
175 foreach (static::getIncludePaths() as $path)
176 {
177 // normalize path
178 $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $action . '.php';
179
180 if (is_file($path))
181 {
182 // if the file exists include it
183 require_once $path;
184
185 // make action camelcase
186 $action = preg_replace("/_+/", ' ', $action);
187 $action = preg_replace("/\s+/", '', ucwords($action));
188
189 // create class name
190 $classname = 'VAPCronJob' . $action;
191
192 if (!class_exists($classname))
193 {
194 // let's try also with old notation
195 $classname = preg_replace("/^VAP/", '', $classname);
196 }
197
198 if (class_exists($classname))
199 {
200 // return the classname of the included cron job
201 return $classname;
202 }
203 }
204 }
205
206 // return null if the cron job doesn't exist
207 return null;
208 }
209
210 /**
211 * Gets a list of supported include paths.
212 *
213 * @return array
214 *
215 * @since 1.7
216 */
217 public static function getIncludePaths()
218 {
219 /**
220 * Check whether the folder of the cron jobs
221 * is specified in the old method for backward
222 * compatibility.
223 *
224 * @deprecated 1.8
225 */
226 if (isset(static::$BASE_PATH))
227 {
228 // register path in a temporary variable
229 // to avoid infinite loops
230 $tmp = static::$BASE_PATH;
231 // unset property
232 static::$BASE_PATH = null;
233 // include base path with new technique
234 static::addIncludePath($tmp);
235 }
236
237 return static::$includePaths;
238 }
239
240 /**
241 * Adds one path to include in driver search.
242 * Proxy of addIncludePaths().
243 *
244 * @param string $path The path to search for drivers.
245 *
246 * @return array A list of include paths.
247 *
248 * @since 1.7
249 *
250 * @uses addIncludePaths()
251 */
252 public static function addIncludePath($path)
253 {
254 return static::addIncludePaths($path);
255 }
256
257 /**
258 * Adds one or more paths to include in driver search.
259 *
260 * @param mixed $paths The path or array of paths to search for drivers.
261 *
262 * @return array A list of include paths.
263 *
264 * @since 1.7
265 *
266 * @uses getIncludePaths()
267 * @uses setIncludePaths()
268 */
269 public static function addIncludePaths($paths)
270 {
271 $includePaths = static::getIncludePaths();
272
273 if (!empty($paths))
274 {
275 // in case the path is an array, merge all the paths and make sure we have no duplicates
276 if (is_array($paths))
277 {
278 $includePaths = array_unique(array_merge($includePaths, $paths));
279 }
280 // otherwise add the path as first element
281 else
282 {
283 $includePaths[] = $paths;
284 }
285
286 // update include paths
287 static::setIncludePaths($includePaths);
288 }
289
290 return $includePaths;
291 }
292
293 /**
294 * Sets the include paths to search for drivers.
295 *
296 * @param array $paths Array with paths to search in.
297 *
298 * @return void
299 *
300 * @since 1.7
301 */
302 public static function setIncludePaths($paths)
303 {
304 static::$includePaths = (array) $paths;
305 }
306 }
307
308 /**
309 * Register a class alias for backward compatibility.
310 *
311 * @deprecated 1.8
312 */
313 class_alias('VAPCronDispatcher', 'CronDispatcher');
314