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 / update / factory.php
vikappointments / site / helpers / libraries / update Last commit date
adapters 5 days ago adapter.php 5 days ago factory.php 5 days ago index.html 5 days ago rule.php 5 days ago urihandler.php 5 days ago
factory.php
141 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 VAPLoader::import('libraries.update.adapter');
15
16 /**
17 * Factory used to handle the update adapters.
18 *
19 * ------------------------------------------------------------------------------------
20 *
21 * Update adapters CLASS name must have the following structure:
22 *
23 * "VAPUpdateAdapter" + VERSION (replace dots with underscores)
24 * eg. VAPUpdateAdapter1_2_5 (com_vikappointments 1.2.5)
25 *
26 * ------------------------------------------------------------------------------------
27 *
28 * Update adapters FILE name must have the following structure:
29 *
30 * VERSION (replace dots with underscores) + ".php"
31 * eg. 1_2_5.php (com_vikappointments 1.2.5)
32 *
33 * @since 1.7
34 */
35 class VAPUpdateFactory
36 {
37 /**
38 * Executes the requested method.
39 *
40 * @param string $method The method name to launch.
41 * @param string $version The version to consider.
42 * @param mixed $caller The object that invoked this method.
43 *
44 * @return boolean True on success, false otherwise.
45 */
46 public static function run($method, $version, $caller = null)
47 {
48 // get all adapters
49 $adapters = glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'adapters' . DIRECTORY_SEPARATOR . '*.php');
50
51 $eligible = [];
52
53 // iterate each supported version
54 foreach ($adapters as $file)
55 {
56 // get filename
57 $filename = preg_replace("/\.php$/i", '', basename($file));
58
59 // get class name of update adapter for current loop version
60 $classname = 'VAPUpdateAdapter' . $filename;
61
62 // get version from filename
63 $v = preg_replace("/_+/", '.', $filename);
64
65 // in case the software version is lower than loop version, launch the rule
66 if (version_compare($version, $v, '<'))
67 {
68 // load updater adapter file
69 if (VAPLoader::import('libraries.update.adapters.' . $filename))
70 {
71 // inject the class into a list of eligible adapters to be executed later
72 $eligible[] = [
73 'version' => $v,
74 'class' => $classname,
75 ];
76 }
77 }
78 }
79
80 /**
81 * Make sure the adapters are properly executed per ascending version rather than alphabetically.
82 *
83 * @since 1.7.8
84 */
85 usort($eligible, fn($a, $b) => version_compare($a['version'], $b['version']));
86
87 // iterate all the eligible adapters one by one
88 foreach ($eligible as $adapter)
89 {
90 $classname = $adapter['class'];
91
92 try
93 {
94 // check whether the requested class exists
95 $reflection = new ReflectionClass($classname);
96
97 // Get method details.
98 // In case the method doesn't exist, an exception will be thrown.
99 $methodData = $reflection->getMethod($method);
100
101 if ($methodData->isStatic())
102 {
103 // use static class
104 $object = $classname;
105 }
106 else
107 {
108 // instantiate object
109 $object = new $classname();
110 }
111
112 // then run update callback function
113 $success = call_user_func(array($object, $method), $caller);
114
115 if ($success === false)
116 {
117 // stop adapters in case something gone wrong
118 return false;
119 }
120 }
121 catch (Exception $e)
122 {
123 if (!$e instanceof ReflectionException)
124 {
125 // prompt error message
126 JFactory::getApplication()->enqueueMessage($e->getMessage(), 'warning');
127 }
128
129 // One of the following errors occurred:
130 // - the class does not exist;
131 // - the method does not exist;
132 // - the launched method thrown an exception.
133 return false;
134 }
135 }
136
137 // no error found
138 return true;
139 }
140 }
141