@@ -17,8 +17,8 @@ |
||
17 | 17 | */ |
18 | 18 | |
19 | 19 | if (!defined("PLUGIN_INSTALLER")) { |
20 | - echo "You cannot access this file directly!"; |
|
21 | - exit; |
|
20 | + echo "You cannot access this file directly!"; |
|
21 | + exit; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | $preferences = new Preferences("plugin_rollbar"); |
@@ -17,8 +17,8 @@ |
||
17 | 17 | */ |
18 | 18 | |
19 | 19 | if (!defined("PLUGIN_INSTALLER")) { |
20 | - echo "You cannot access this file directly!"; |
|
21 | - exit; |
|
20 | + echo "You cannot access this file directly!"; |
|
21 | + exit; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | $preferences = new Preferences("plugin_rollbar"); |
@@ -26,410 +26,410 @@ |
||
26 | 26 | */ |
27 | 27 | |
28 | 28 | if (!defined('PLUGIN_INSTALLER')) { |
29 | - define('PLUGIN_INSTALLER', true); |
|
29 | + define('PLUGIN_INSTALLER', true); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | class PluginInstaller { |
33 | 33 | |
34 | - //plugin to install / deinstall |
|
35 | - protected $plugin = null; |
|
36 | - |
|
37 | - public function __construct(Plugin $plugin) { |
|
38 | - $this->plugin = $plugin; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * check php version, php extensions and so on |
|
43 | - * |
|
44 | - * @return mixed true, if all required plugins are available, or an array with missing |
|
45 | - */ |
|
46 | - public function checkRequirements (bool $dontCheckPlugins = false) { |
|
47 | - //get require |
|
48 | - $require_array = $this->plugin->getRequiredPlugins(); |
|
49 | - |
|
50 | - //get package list |
|
51 | - require(STORE_PATH . "package_list.php"); |
|
52 | - |
|
53 | - $missing_plugins = array(); |
|
54 | - |
|
55 | - $installed_plugins = Plugins::listInstalledPlugins(); |
|
56 | - |
|
57 | - //iterate through all requirements |
|
58 | - foreach ($require_array as $requirement=>$version) { |
|
59 | - if ($requirement === "php") { |
|
60 | - //check php version |
|
61 | - if (!$this->checkVersion($version, phpversion())) { |
|
62 | - $missing_plugins[] = $requirement; |
|
63 | - |
|
64 | - continue; |
|
65 | - } |
|
66 | - } else if (PHPUtils::startsWith($requirement, "ext-")) { |
|
67 | - //check php extension |
|
68 | - |
|
69 | - $extension = str_replace("ext-", "", $requirement); |
|
70 | - |
|
71 | - //check, if php extension is loaded |
|
72 | - if (!extension_loaded($extension)) { |
|
73 | - $missing_plugins[] = $requirement; |
|
74 | - |
|
75 | - continue; |
|
76 | - } |
|
77 | - |
|
78 | - //get extension version |
|
79 | - $current_version = phpversion($extension); |
|
80 | - |
|
81 | - //check version |
|
82 | - if (!$this->checkVersion($version, $current_version)) { |
|
83 | - $missing_plugins[] = $requirement; |
|
84 | - } |
|
85 | - } else if (PHPUtils::startsWith($requirement, "apache-")) { |
|
86 | - //check for apache module, but no version check is supported |
|
87 | - |
|
88 | - $module = str_replace("apache-", "", $requirement); |
|
89 | - |
|
90 | - if (!function_exists('apache_get_modules')) { |
|
91 | - $missing_plugins[] = "apache"; |
|
92 | - |
|
93 | - continue; |
|
94 | - } |
|
95 | - |
|
96 | - if (!in_array($module, apache_get_modules())) { |
|
97 | - $missing_plugins[] = $requirement; |
|
98 | - } |
|
99 | - } else if (PHPUtils::startsWith($requirement, "package-")) { |
|
100 | - //check if package is installed |
|
101 | - $package = str_replace("package-", "", $requirement); |
|
102 | - |
|
103 | - //packages doesnt supports specific version |
|
104 | - |
|
105 | - if (!isset($package_list[$package])) { |
|
106 | - $missing_plugins[] = $requirement; |
|
107 | - } |
|
108 | - } else if ($requirement === "core") { |
|
109 | - //check core version |
|
110 | - if ($version === "*") { |
|
111 | - //we dont have to check version |
|
112 | - } else { |
|
113 | - //get current version |
|
114 | - $array = explode(" ", Version::current()->getVersion()); |
|
115 | - $current_core_version = $array[0]; |
|
116 | - |
|
117 | - //check version |
|
118 | - if (!$this->checkVersion($version, $current_core_version)) { |
|
119 | - $missing_plugins[] = "core"; |
|
120 | - } |
|
121 | - } |
|
122 | - } else { |
|
123 | - //throw new Exception("plugin requirement check isnt supported yet."); |
|
124 | - |
|
125 | - if (!$dontCheckPlugins) { |
|
126 | - continue; |
|
127 | - } |
|
128 | - |
|
129 | - //check, if plugin is installed |
|
130 | - if (!self::isPluginInstalled($requirement, $installed_plugins)) { |
|
131 | - $missing_plugins[] = $requirement; |
|
132 | - continue; |
|
133 | - } |
|
134 | - |
|
135 | - //check plugin version |
|
136 | - $current_version = self::getPluginVersion($requirement, $installed_plugins); |
|
137 | - |
|
138 | - //check version |
|
139 | - if (!$this->checkVersion($version, $current_version)) { |
|
140 | - $missing_plugins[] = $requirement . ":version"; |
|
141 | - } |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - if (empty($missing_plugins)) { |
|
146 | - return true; |
|
147 | - } else { |
|
148 | - return $missing_plugins; |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - public static function isPluginInstalled (string $plugin_name, array $installed_plugins = array()) : bool { |
|
153 | - if (empty($installed_plugins)) { |
|
154 | - $installed_plugins = Plugins::listInstalledPlugins(); |
|
155 | - } |
|
156 | - |
|
157 | - if (!isset($installed_plugins[$plugin_name])) { |
|
158 | - //plugin is not installed |
|
159 | - return false; |
|
160 | - } else { |
|
161 | - //plugin is installed |
|
162 | - return true; |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - public static function getPluginVersion (string $plugin_name, array $installed_plugins = array()) { |
|
167 | - if (empty($installed_plugins)) { |
|
168 | - $installed_plugins = Plugins::listInstalledPlugins(); |
|
169 | - } |
|
170 | - |
|
171 | - if (!isset($installed_plugins[$plugin_name])) { |
|
172 | - //plugin is not installed |
|
173 | - return false; |
|
174 | - } else { |
|
175 | - //plugin is installed, return installed version |
|
176 | - $plugin = Plugin::castPlugin($installed_plugins[$plugin_name]); |
|
177 | - return $plugin->getInstalledVersion(); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - protected function checkVersion (string $expected_version, $current_version) : bool { |
|
182 | - //remove alpha and beta labels |
|
183 | - /*$expected_version = str_replace("-alpha", "", $expected_version); |
|
34 | + //plugin to install / deinstall |
|
35 | + protected $plugin = null; |
|
36 | + |
|
37 | + public function __construct(Plugin $plugin) { |
|
38 | + $this->plugin = $plugin; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * check php version, php extensions and so on |
|
43 | + * |
|
44 | + * @return mixed true, if all required plugins are available, or an array with missing |
|
45 | + */ |
|
46 | + public function checkRequirements (bool $dontCheckPlugins = false) { |
|
47 | + //get require |
|
48 | + $require_array = $this->plugin->getRequiredPlugins(); |
|
49 | + |
|
50 | + //get package list |
|
51 | + require(STORE_PATH . "package_list.php"); |
|
52 | + |
|
53 | + $missing_plugins = array(); |
|
54 | + |
|
55 | + $installed_plugins = Plugins::listInstalledPlugins(); |
|
56 | + |
|
57 | + //iterate through all requirements |
|
58 | + foreach ($require_array as $requirement=>$version) { |
|
59 | + if ($requirement === "php") { |
|
60 | + //check php version |
|
61 | + if (!$this->checkVersion($version, phpversion())) { |
|
62 | + $missing_plugins[] = $requirement; |
|
63 | + |
|
64 | + continue; |
|
65 | + } |
|
66 | + } else if (PHPUtils::startsWith($requirement, "ext-")) { |
|
67 | + //check php extension |
|
68 | + |
|
69 | + $extension = str_replace("ext-", "", $requirement); |
|
70 | + |
|
71 | + //check, if php extension is loaded |
|
72 | + if (!extension_loaded($extension)) { |
|
73 | + $missing_plugins[] = $requirement; |
|
74 | + |
|
75 | + continue; |
|
76 | + } |
|
77 | + |
|
78 | + //get extension version |
|
79 | + $current_version = phpversion($extension); |
|
80 | + |
|
81 | + //check version |
|
82 | + if (!$this->checkVersion($version, $current_version)) { |
|
83 | + $missing_plugins[] = $requirement; |
|
84 | + } |
|
85 | + } else if (PHPUtils::startsWith($requirement, "apache-")) { |
|
86 | + //check for apache module, but no version check is supported |
|
87 | + |
|
88 | + $module = str_replace("apache-", "", $requirement); |
|
89 | + |
|
90 | + if (!function_exists('apache_get_modules')) { |
|
91 | + $missing_plugins[] = "apache"; |
|
92 | + |
|
93 | + continue; |
|
94 | + } |
|
95 | + |
|
96 | + if (!in_array($module, apache_get_modules())) { |
|
97 | + $missing_plugins[] = $requirement; |
|
98 | + } |
|
99 | + } else if (PHPUtils::startsWith($requirement, "package-")) { |
|
100 | + //check if package is installed |
|
101 | + $package = str_replace("package-", "", $requirement); |
|
102 | + |
|
103 | + //packages doesnt supports specific version |
|
104 | + |
|
105 | + if (!isset($package_list[$package])) { |
|
106 | + $missing_plugins[] = $requirement; |
|
107 | + } |
|
108 | + } else if ($requirement === "core") { |
|
109 | + //check core version |
|
110 | + if ($version === "*") { |
|
111 | + //we dont have to check version |
|
112 | + } else { |
|
113 | + //get current version |
|
114 | + $array = explode(" ", Version::current()->getVersion()); |
|
115 | + $current_core_version = $array[0]; |
|
116 | + |
|
117 | + //check version |
|
118 | + if (!$this->checkVersion($version, $current_core_version)) { |
|
119 | + $missing_plugins[] = "core"; |
|
120 | + } |
|
121 | + } |
|
122 | + } else { |
|
123 | + //throw new Exception("plugin requirement check isnt supported yet."); |
|
124 | + |
|
125 | + if (!$dontCheckPlugins) { |
|
126 | + continue; |
|
127 | + } |
|
128 | + |
|
129 | + //check, if plugin is installed |
|
130 | + if (!self::isPluginInstalled($requirement, $installed_plugins)) { |
|
131 | + $missing_plugins[] = $requirement; |
|
132 | + continue; |
|
133 | + } |
|
134 | + |
|
135 | + //check plugin version |
|
136 | + $current_version = self::getPluginVersion($requirement, $installed_plugins); |
|
137 | + |
|
138 | + //check version |
|
139 | + if (!$this->checkVersion($version, $current_version)) { |
|
140 | + $missing_plugins[] = $requirement . ":version"; |
|
141 | + } |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + if (empty($missing_plugins)) { |
|
146 | + return true; |
|
147 | + } else { |
|
148 | + return $missing_plugins; |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + public static function isPluginInstalled (string $plugin_name, array $installed_plugins = array()) : bool { |
|
153 | + if (empty($installed_plugins)) { |
|
154 | + $installed_plugins = Plugins::listInstalledPlugins(); |
|
155 | + } |
|
156 | + |
|
157 | + if (!isset($installed_plugins[$plugin_name])) { |
|
158 | + //plugin is not installed |
|
159 | + return false; |
|
160 | + } else { |
|
161 | + //plugin is installed |
|
162 | + return true; |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + public static function getPluginVersion (string $plugin_name, array $installed_plugins = array()) { |
|
167 | + if (empty($installed_plugins)) { |
|
168 | + $installed_plugins = Plugins::listInstalledPlugins(); |
|
169 | + } |
|
170 | + |
|
171 | + if (!isset($installed_plugins[$plugin_name])) { |
|
172 | + //plugin is not installed |
|
173 | + return false; |
|
174 | + } else { |
|
175 | + //plugin is installed, return installed version |
|
176 | + $plugin = Plugin::castPlugin($installed_plugins[$plugin_name]); |
|
177 | + return $plugin->getInstalledVersion(); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + protected function checkVersion (string $expected_version, $current_version) : bool { |
|
182 | + //remove alpha and beta labels |
|
183 | + /*$expected_version = str_replace("-alpha", "", $expected_version); |
|
184 | 184 | $expected_version = str_replace("-beta", "", $expected_version); |
185 | 185 | $current_version = str_replace("-alpha", "", $current_version); |
186 | 186 | $current_version = str_replace("-beta", "", $current_version);*/ |
187 | 187 | |
188 | - //validate version numbers, remove suffixes "-alpha", "-beta" and such like -1~dotdeb+8.1 (PHP version 7.0.29-1~dotdeb+8.1) |
|
189 | - $array1 = explode("-", $expected_version); |
|
190 | - $expected_version = $array1[0]; |
|
191 | - |
|
192 | - $array2 = explode("-", $current_version); |
|
193 | - $current_version = $array2[0]; |
|
194 | - |
|
195 | - //check version |
|
196 | - if (is_numeric($expected_version)) { |
|
197 | - //a specific version is required |
|
198 | - if ($current_version !== $expected_version) { |
|
199 | - return false; |
|
200 | - } else { |
|
201 | - return true; |
|
202 | - } |
|
203 | - } else if ($expected_version === "*") { |
|
204 | - //every version is allowed |
|
205 | - return true; |
|
206 | - } else { |
|
207 | - //parse version string |
|
208 | - |
|
209 | - $operator_length = 0; |
|
210 | - |
|
211 | - for ($i = 0; $i < strlen($expected_version); $i++) { |
|
212 | - if (!is_numeric($expected_version[$i])) { |
|
213 | - $operator_length++; |
|
214 | - } else { |
|
215 | - break; |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - //get operator and version |
|
220 | - $operator = substr($expected_version, 0, $operator_length); |
|
221 | - $version = substr($expected_version, $operator_length); |
|
222 | - |
|
223 | - if (!empty($operator_length)) { |
|
224 | - return version_compare($current_version, $version, $operator) === TRUE; |
|
225 | - } else { |
|
226 | - return version_compare($current_version, $expected_version) === 0; |
|
227 | - } |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - public function install () : bool { |
|
232 | - //first, check compatibility |
|
233 | - if (!$this->checkRequirements()) { |
|
234 | - return false; |
|
235 | - } |
|
236 | - |
|
237 | - //check, if plugin is already installed |
|
238 | - if ($this->plugin->isInstalled()) { |
|
239 | - return false; |
|
240 | - } |
|
241 | - |
|
242 | - Logger::log(LogLevel::INFO, "install plugin '" . $this->plugin->getName() . "'"); |
|
243 | - |
|
244 | - //start transaction |
|
245 | - Database::getInstance()->beginTransaction(); |
|
246 | - |
|
247 | - try { |
|
248 | - //check, if install.json is used |
|
249 | - if ($this->plugin->hasInstallJson()) { |
|
250 | - //check, if install.json exists |
|
251 | - if (!file_exists($this->plugin->getPath() . "install.json")) { |
|
252 | - throw new IllegalStateException("plugin '" . $this->plugin->getName() . "' requires a install.json, but plugin directory doesnt contains a install.json file."); |
|
253 | - } |
|
254 | - |
|
255 | - //get content |
|
256 | - $install_json = json_decode(file_get_contents($this->plugin->getPath() . "install.json"), true); |
|
257 | - |
|
258 | - $installer_plugins = self::listInstallerPlugins(); |
|
259 | - |
|
260 | - foreach ($installer_plugins as $i_plugin) { |
|
261 | - //cast plugin |
|
262 | - $i_plugin = PluginInstaller_Plugin::cast($i_plugin); |
|
263 | - |
|
264 | - //execute installer plugin |
|
265 | - $i_plugin->install($this->plugin, $install_json); |
|
266 | - } |
|
267 | - |
|
268 | - if (isset($install_json['install_script'])) { |
|
269 | - $script_filename = $install_json['install_script']; |
|
270 | - $script_path = $this->plugin->getPath() . $script_filename; |
|
271 | - |
|
272 | - //execute script, if exists |
|
273 | - if (file_exists($script_path)) { |
|
274 | - require($script_path); |
|
275 | - } else { |
|
276 | - throw new IllegalStateException("a install script '" . $script_filename . "' is set for plugin '" . $this->plugin->getName() . "', but file doesnt exists (path: " . $script_path . ")."); |
|
277 | - } |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - //set plugin as installed |
|
282 | - $this->setInstalled(); |
|
283 | - } catch (Exception $e) { |
|
284 | - //rollback database on exception |
|
285 | - Database::getInstance()->rollback(); |
|
286 | - |
|
287 | - throw $e; |
|
288 | - } |
|
289 | - |
|
290 | - Database::getInstance()->commit(); |
|
291 | - |
|
292 | - //clear cache |
|
293 | - Cache::clear(); |
|
294 | - |
|
295 | - return true; |
|
296 | - } |
|
297 | - |
|
298 | - public function uninstall () : bool { |
|
299 | - Logger::log(LogLevel::INFO, "uninstall plugin '" . $this->plugin->getName() . "'"); |
|
300 | - |
|
301 | - //check, if install.json is used |
|
302 | - if ($this->plugin->hasInstallJson()) { |
|
303 | - //check, if install.json exists |
|
304 | - if (!file_exists($this->plugin->getPath() . "install.json")) { |
|
305 | - throw new IllegalStateException("plugin '" . $this->plugin->getName() . "' requires a install.json, but plugin directory doesnt contains a install.json file."); |
|
306 | - } |
|
307 | - |
|
308 | - //get content |
|
309 | - $install_json = json_decode(file_get_contents($this->plugin->getPath() . "install.json"), true); |
|
310 | - |
|
311 | - $installer_plugins = self::listInstallerPlugins(); |
|
312 | - |
|
313 | - foreach ($installer_plugins as $i_plugin) { |
|
314 | - //cast plugin |
|
315 | - $i_plugin = PluginInstaller_Plugin::cast($i_plugin); |
|
316 | - |
|
317 | - //execute installer plugin |
|
318 | - $i_plugin->uninstall($this->plugin, $install_json); |
|
319 | - } |
|
320 | - |
|
321 | - if (isset($install_json['uninstall_script'])) { |
|
322 | - $script_filename = $install_json['uninstall_script']; |
|
323 | - $script_path = $this->plugin->getPath() . $script_filename; |
|
324 | - |
|
325 | - //execute script, if exists |
|
326 | - if (file_exists($script_path)) { |
|
327 | - require($script_path); |
|
328 | - } else { |
|
329 | - throw new IllegalStateException("a uninstall script '" . $script_filename . "' is set for plugin '" . $this->plugin->getName() . "', but file doesnt exists (path: " . $script_path . ")."); |
|
330 | - } |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - //set plugin as uninstalled |
|
335 | - $this->setUnInstalled(); |
|
188 | + //validate version numbers, remove suffixes "-alpha", "-beta" and such like -1~dotdeb+8.1 (PHP version 7.0.29-1~dotdeb+8.1) |
|
189 | + $array1 = explode("-", $expected_version); |
|
190 | + $expected_version = $array1[0]; |
|
191 | + |
|
192 | + $array2 = explode("-", $current_version); |
|
193 | + $current_version = $array2[0]; |
|
194 | + |
|
195 | + //check version |
|
196 | + if (is_numeric($expected_version)) { |
|
197 | + //a specific version is required |
|
198 | + if ($current_version !== $expected_version) { |
|
199 | + return false; |
|
200 | + } else { |
|
201 | + return true; |
|
202 | + } |
|
203 | + } else if ($expected_version === "*") { |
|
204 | + //every version is allowed |
|
205 | + return true; |
|
206 | + } else { |
|
207 | + //parse version string |
|
208 | + |
|
209 | + $operator_length = 0; |
|
210 | + |
|
211 | + for ($i = 0; $i < strlen($expected_version); $i++) { |
|
212 | + if (!is_numeric($expected_version[$i])) { |
|
213 | + $operator_length++; |
|
214 | + } else { |
|
215 | + break; |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + //get operator and version |
|
220 | + $operator = substr($expected_version, 0, $operator_length); |
|
221 | + $version = substr($expected_version, $operator_length); |
|
222 | + |
|
223 | + if (!empty($operator_length)) { |
|
224 | + return version_compare($current_version, $version, $operator) === TRUE; |
|
225 | + } else { |
|
226 | + return version_compare($current_version, $expected_version) === 0; |
|
227 | + } |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + public function install () : bool { |
|
232 | + //first, check compatibility |
|
233 | + if (!$this->checkRequirements()) { |
|
234 | + return false; |
|
235 | + } |
|
236 | + |
|
237 | + //check, if plugin is already installed |
|
238 | + if ($this->plugin->isInstalled()) { |
|
239 | + return false; |
|
240 | + } |
|
241 | + |
|
242 | + Logger::log(LogLevel::INFO, "install plugin '" . $this->plugin->getName() . "'"); |
|
243 | + |
|
244 | + //start transaction |
|
245 | + Database::getInstance()->beginTransaction(); |
|
246 | + |
|
247 | + try { |
|
248 | + //check, if install.json is used |
|
249 | + if ($this->plugin->hasInstallJson()) { |
|
250 | + //check, if install.json exists |
|
251 | + if (!file_exists($this->plugin->getPath() . "install.json")) { |
|
252 | + throw new IllegalStateException("plugin '" . $this->plugin->getName() . "' requires a install.json, but plugin directory doesnt contains a install.json file."); |
|
253 | + } |
|
254 | + |
|
255 | + //get content |
|
256 | + $install_json = json_decode(file_get_contents($this->plugin->getPath() . "install.json"), true); |
|
257 | + |
|
258 | + $installer_plugins = self::listInstallerPlugins(); |
|
259 | + |
|
260 | + foreach ($installer_plugins as $i_plugin) { |
|
261 | + //cast plugin |
|
262 | + $i_plugin = PluginInstaller_Plugin::cast($i_plugin); |
|
263 | + |
|
264 | + //execute installer plugin |
|
265 | + $i_plugin->install($this->plugin, $install_json); |
|
266 | + } |
|
267 | + |
|
268 | + if (isset($install_json['install_script'])) { |
|
269 | + $script_filename = $install_json['install_script']; |
|
270 | + $script_path = $this->plugin->getPath() . $script_filename; |
|
271 | + |
|
272 | + //execute script, if exists |
|
273 | + if (file_exists($script_path)) { |
|
274 | + require($script_path); |
|
275 | + } else { |
|
276 | + throw new IllegalStateException("a install script '" . $script_filename . "' is set for plugin '" . $this->plugin->getName() . "', but file doesnt exists (path: " . $script_path . ")."); |
|
277 | + } |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + //set plugin as installed |
|
282 | + $this->setInstalled(); |
|
283 | + } catch (Exception $e) { |
|
284 | + //rollback database on exception |
|
285 | + Database::getInstance()->rollback(); |
|
286 | + |
|
287 | + throw $e; |
|
288 | + } |
|
289 | + |
|
290 | + Database::getInstance()->commit(); |
|
291 | + |
|
292 | + //clear cache |
|
293 | + Cache::clear(); |
|
294 | + |
|
295 | + return true; |
|
296 | + } |
|
297 | + |
|
298 | + public function uninstall () : bool { |
|
299 | + Logger::log(LogLevel::INFO, "uninstall plugin '" . $this->plugin->getName() . "'"); |
|
300 | + |
|
301 | + //check, if install.json is used |
|
302 | + if ($this->plugin->hasInstallJson()) { |
|
303 | + //check, if install.json exists |
|
304 | + if (!file_exists($this->plugin->getPath() . "install.json")) { |
|
305 | + throw new IllegalStateException("plugin '" . $this->plugin->getName() . "' requires a install.json, but plugin directory doesnt contains a install.json file."); |
|
306 | + } |
|
307 | + |
|
308 | + //get content |
|
309 | + $install_json = json_decode(file_get_contents($this->plugin->getPath() . "install.json"), true); |
|
310 | + |
|
311 | + $installer_plugins = self::listInstallerPlugins(); |
|
312 | + |
|
313 | + foreach ($installer_plugins as $i_plugin) { |
|
314 | + //cast plugin |
|
315 | + $i_plugin = PluginInstaller_Plugin::cast($i_plugin); |
|
316 | + |
|
317 | + //execute installer plugin |
|
318 | + $i_plugin->uninstall($this->plugin, $install_json); |
|
319 | + } |
|
320 | + |
|
321 | + if (isset($install_json['uninstall_script'])) { |
|
322 | + $script_filename = $install_json['uninstall_script']; |
|
323 | + $script_path = $this->plugin->getPath() . $script_filename; |
|
324 | + |
|
325 | + //execute script, if exists |
|
326 | + if (file_exists($script_path)) { |
|
327 | + require($script_path); |
|
328 | + } else { |
|
329 | + throw new IllegalStateException("a uninstall script '" . $script_filename . "' is set for plugin '" . $this->plugin->getName() . "', but file doesnt exists (path: " . $script_path . ")."); |
|
330 | + } |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + //set plugin as uninstalled |
|
335 | + $this->setUnInstalled(); |
|
336 | 336 | |
337 | - //clear cache |
|
338 | - Cache::clear(); |
|
337 | + //clear cache |
|
338 | + Cache::clear(); |
|
339 | 339 | |
340 | - return true; |
|
341 | - } |
|
340 | + return true; |
|
341 | + } |
|
342 | 342 | |
343 | - public function upgrade () { |
|
344 | - throw new Exception("UnuspportedOperationException: method PluginInstaller::upgrade() isnt implemented yet."); |
|
345 | - } |
|
343 | + public function upgrade () { |
|
344 | + throw new Exception("UnuspportedOperationException: method PluginInstaller::upgrade() isnt implemented yet."); |
|
345 | + } |
|
346 | 346 | |
347 | - public function setInstalled () { |
|
348 | - Database::getInstance()->execute("INSERT INTO `{praefix}plugins` ( |
|
347 | + public function setInstalled () { |
|
348 | + Database::getInstance()->execute("INSERT INTO `{praefix}plugins` ( |
|
349 | 349 | `name`, `version`, `installed`, `activated` |
350 | 350 | ) VALUES ( |
351 | 351 | :name, :version, :installed, :activated |
352 | 352 | ) ON DUPLICATE KEY UPDATE `installed` = '1', `version` = :version; ", array( |
353 | - 'name' => $this->plugin->getName(), |
|
354 | - 'version' => $this->plugin->getVersion(), |
|
355 | - 'installed' => 1, |
|
356 | - 'activated' => 0 |
|
357 | - )); |
|
358 | - |
|
359 | - //clear cache |
|
360 | - Plugins::clearCache(); |
|
361 | - } |
|
362 | - |
|
363 | - public function setUnInstalled () { |
|
364 | - Database::getInstance()->execute("DELETE FROM `{praefix}plugins` WHERE `name` = :name; ", array( |
|
365 | - 'name' => $this->plugin->getName() |
|
366 | - )); |
|
367 | - |
|
368 | - //clear cache |
|
369 | - Plugins::clearCache(); |
|
370 | - } |
|
371 | - |
|
372 | - public static function listInstallerPlugins () : array { |
|
373 | - $rows = array(); |
|
374 | - |
|
375 | - if (Cache::contains("plugins", "installer_plugins")) { |
|
376 | - $rows = Cache::get("plugins", "installer_plugins"); |
|
377 | - } else { |
|
378 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugin_installer_plugins`; "); |
|
379 | - |
|
380 | - //put into cache |
|
381 | - Cache::put("plugins", "installer_plugins", $rows); |
|
382 | - } |
|
383 | - |
|
384 | - $plugins = array(); |
|
385 | - |
|
386 | - foreach ($rows as $row) { |
|
387 | - $class_name = $row['class_name']; |
|
388 | - $path = $row['path']; |
|
389 | - |
|
390 | - //first, include path |
|
391 | - require_once(ROOT_PATH . $path); |
|
392 | - |
|
393 | - //create object |
|
394 | - $obj = new $class_name(); |
|
395 | - |
|
396 | - //check, if object extends PluginInstaller_Plugin |
|
397 | - if ($obj instanceof PluginInstaller_Plugin) { |
|
398 | - //add plugin to list |
|
399 | - $plugins[] = $obj; |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - //sort list |
|
404 | - usort($plugins, function(PluginInstaller_Plugin $a, PluginInstaller_Plugin $b) { |
|
405 | - return $b->getPriority() <=> $a->getPriority(); |
|
406 | - }); |
|
407 | - |
|
408 | - return $plugins; |
|
409 | - } |
|
410 | - |
|
411 | - public static function addInstallerPluginIfAbsent (string $class_name, string $path) { |
|
412 | - Database::getInstance()->execute("INSERT INTO `{praefix}plugin_installer_plugins` ( |
|
353 | + 'name' => $this->plugin->getName(), |
|
354 | + 'version' => $this->plugin->getVersion(), |
|
355 | + 'installed' => 1, |
|
356 | + 'activated' => 0 |
|
357 | + )); |
|
358 | + |
|
359 | + //clear cache |
|
360 | + Plugins::clearCache(); |
|
361 | + } |
|
362 | + |
|
363 | + public function setUnInstalled () { |
|
364 | + Database::getInstance()->execute("DELETE FROM `{praefix}plugins` WHERE `name` = :name; ", array( |
|
365 | + 'name' => $this->plugin->getName() |
|
366 | + )); |
|
367 | + |
|
368 | + //clear cache |
|
369 | + Plugins::clearCache(); |
|
370 | + } |
|
371 | + |
|
372 | + public static function listInstallerPlugins () : array { |
|
373 | + $rows = array(); |
|
374 | + |
|
375 | + if (Cache::contains("plugins", "installer_plugins")) { |
|
376 | + $rows = Cache::get("plugins", "installer_plugins"); |
|
377 | + } else { |
|
378 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugin_installer_plugins`; "); |
|
379 | + |
|
380 | + //put into cache |
|
381 | + Cache::put("plugins", "installer_plugins", $rows); |
|
382 | + } |
|
383 | + |
|
384 | + $plugins = array(); |
|
385 | + |
|
386 | + foreach ($rows as $row) { |
|
387 | + $class_name = $row['class_name']; |
|
388 | + $path = $row['path']; |
|
389 | + |
|
390 | + //first, include path |
|
391 | + require_once(ROOT_PATH . $path); |
|
392 | + |
|
393 | + //create object |
|
394 | + $obj = new $class_name(); |
|
395 | + |
|
396 | + //check, if object extends PluginInstaller_Plugin |
|
397 | + if ($obj instanceof PluginInstaller_Plugin) { |
|
398 | + //add plugin to list |
|
399 | + $plugins[] = $obj; |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + //sort list |
|
404 | + usort($plugins, function(PluginInstaller_Plugin $a, PluginInstaller_Plugin $b) { |
|
405 | + return $b->getPriority() <=> $a->getPriority(); |
|
406 | + }); |
|
407 | + |
|
408 | + return $plugins; |
|
409 | + } |
|
410 | + |
|
411 | + public static function addInstallerPluginIfAbsent (string $class_name, string $path) { |
|
412 | + Database::getInstance()->execute("INSERT INTO `{praefix}plugin_installer_plugins` ( |
|
413 | 413 | `class_name`, `path` |
414 | 414 | ) VALUES ( |
415 | 415 | :class_name, :path |
416 | 416 | ) ON DUPLICATE KEY UPDATE `path` = :path; ", array( |
417 | - 'class_name' => $class_name, |
|
418 | - 'path' => $path |
|
419 | - )); |
|
420 | - |
|
421 | - //clear cache |
|
422 | - Cache::clear("plugins", "installer_plugins"); |
|
423 | - } |
|
424 | - |
|
425 | - public static function removeInstallerPlugin (string $class_name) { |
|
426 | - Database::getInstance()->execute("DELETE FROM `{praefix}plugin_installer_plugins` WHERE `class_name` = :class_name; ", array( |
|
427 | - 'class_name' => $class_name |
|
428 | - )); |
|
429 | - |
|
430 | - //clear cache |
|
431 | - Cache::clear("plugins", "installer_plugins"); |
|
432 | - } |
|
417 | + 'class_name' => $class_name, |
|
418 | + 'path' => $path |
|
419 | + )); |
|
420 | + |
|
421 | + //clear cache |
|
422 | + Cache::clear("plugins", "installer_plugins"); |
|
423 | + } |
|
424 | + |
|
425 | + public static function removeInstallerPlugin (string $class_name) { |
|
426 | + Database::getInstance()->execute("DELETE FROM `{praefix}plugin_installer_plugins` WHERE `class_name` = :class_name; ", array( |
|
427 | + 'class_name' => $class_name |
|
428 | + )); |
|
429 | + |
|
430 | + //clear cache |
|
431 | + Cache::clear("plugins", "installer_plugins"); |
|
432 | + } |
|
433 | 433 | |
434 | 434 | } |
435 | 435 |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * |
44 | 44 | * @return mixed true, if all required plugins are available, or an array with missing |
45 | 45 | */ |
46 | - public function checkRequirements (bool $dontCheckPlugins = false) { |
|
46 | + public function checkRequirements(bool $dontCheckPlugins = false) { |
|
47 | 47 | //get require |
48 | 48 | $require_array = $this->plugin->getRequiredPlugins(); |
49 | 49 | |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | } |
150 | 150 | } |
151 | 151 | |
152 | - public static function isPluginInstalled (string $plugin_name, array $installed_plugins = array()) : bool { |
|
152 | + public static function isPluginInstalled(string $plugin_name, array $installed_plugins = array()) : bool { |
|
153 | 153 | if (empty($installed_plugins)) { |
154 | 154 | $installed_plugins = Plugins::listInstalledPlugins(); |
155 | 155 | } |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | } |
164 | 164 | } |
165 | 165 | |
166 | - public static function getPluginVersion (string $plugin_name, array $installed_plugins = array()) { |
|
166 | + public static function getPluginVersion(string $plugin_name, array $installed_plugins = array()) { |
|
167 | 167 | if (empty($installed_plugins)) { |
168 | 168 | $installed_plugins = Plugins::listInstalledPlugins(); |
169 | 169 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | } |
179 | 179 | } |
180 | 180 | |
181 | - protected function checkVersion (string $expected_version, $current_version) : bool { |
|
181 | + protected function checkVersion(string $expected_version, $current_version) : bool { |
|
182 | 182 | //remove alpha and beta labels |
183 | 183 | /*$expected_version = str_replace("-alpha", "", $expected_version); |
184 | 184 | $expected_version = str_replace("-beta", "", $expected_version); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
231 | - public function install () : bool { |
|
231 | + public function install() : bool { |
|
232 | 232 | //first, check compatibility |
233 | 233 | if (!$this->checkRequirements()) { |
234 | 234 | return false; |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | return true; |
296 | 296 | } |
297 | 297 | |
298 | - public function uninstall () : bool { |
|
298 | + public function uninstall() : bool { |
|
299 | 299 | Logger::log(LogLevel::INFO, "uninstall plugin '" . $this->plugin->getName() . "'"); |
300 | 300 | |
301 | 301 | //check, if install.json is used |
@@ -340,11 +340,11 @@ discard block |
||
340 | 340 | return true; |
341 | 341 | } |
342 | 342 | |
343 | - public function upgrade () { |
|
343 | + public function upgrade() { |
|
344 | 344 | throw new Exception("UnuspportedOperationException: method PluginInstaller::upgrade() isnt implemented yet."); |
345 | 345 | } |
346 | 346 | |
347 | - public function setInstalled () { |
|
347 | + public function setInstalled() { |
|
348 | 348 | Database::getInstance()->execute("INSERT INTO `{praefix}plugins` ( |
349 | 349 | `name`, `version`, `installed`, `activated` |
350 | 350 | ) VALUES ( |
@@ -360,7 +360,7 @@ discard block |
||
360 | 360 | Plugins::clearCache(); |
361 | 361 | } |
362 | 362 | |
363 | - public function setUnInstalled () { |
|
363 | + public function setUnInstalled() { |
|
364 | 364 | Database::getInstance()->execute("DELETE FROM `{praefix}plugins` WHERE `name` = :name; ", array( |
365 | 365 | 'name' => $this->plugin->getName() |
366 | 366 | )); |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | Plugins::clearCache(); |
370 | 370 | } |
371 | 371 | |
372 | - public static function listInstallerPlugins () : array { |
|
372 | + public static function listInstallerPlugins() : array { |
|
373 | 373 | $rows = array(); |
374 | 374 | |
375 | 375 | if (Cache::contains("plugins", "installer_plugins")) { |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | return $plugins; |
409 | 409 | } |
410 | 410 | |
411 | - public static function addInstallerPluginIfAbsent (string $class_name, string $path) { |
|
411 | + public static function addInstallerPluginIfAbsent(string $class_name, string $path) { |
|
412 | 412 | Database::getInstance()->execute("INSERT INTO `{praefix}plugin_installer_plugins` ( |
413 | 413 | `class_name`, `path` |
414 | 414 | ) VALUES ( |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | Cache::clear("plugins", "installer_plugins"); |
423 | 423 | } |
424 | 424 | |
425 | - public static function removeInstallerPlugin (string $class_name) { |
|
425 | + public static function removeInstallerPlugin(string $class_name) { |
|
426 | 426 | Database::getInstance()->execute("DELETE FROM `{praefix}plugin_installer_plugins` WHERE `class_name` = :class_name; ", array( |
427 | 427 | 'class_name' => $class_name |
428 | 428 | )); |
@@ -44,8 +44,8 @@ |
||
44 | 44 | $logger->log($level, $message, array('user' => 'Bob')); |
45 | 45 | |
46 | 46 | $expected = array( |
47 | - $level.' message of level '.$level.' with context: Bob', |
|
48 | - $level.' message of level '.$level.' with context: Bob', |
|
47 | + $level . ' message of level ' . $level . ' with context: Bob', |
|
48 | + $level . ' message of level ' . $level . ' with context: Bob', |
|
49 | 49 | ); |
50 | 50 | $this->assertEquals($expected, $this->getLogs()); |
51 | 51 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * initialize logging provider |
54 | 54 | */ |
55 | - public function init () { |
|
55 | + public function init() { |
|
56 | 56 | $preferences = new Preferences("plugin_rollbar"); |
57 | 57 | $access_token = $preferences->get("access_token", "none"); |
58 | 58 | $environment = $preferences->get("environment", "development"); |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | /** |
81 | 81 | * log message |
82 | 82 | */ |
83 | - public function log (string $level, string $message, $args = array()) { |
|
83 | + public function log(string $level, string $message, $args = array()) { |
|
84 | 84 | $this->logs[] = array( |
85 | 85 | 'level' => $level, |
86 | 86 | 'message' => $message, |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | /** |
97 | 97 | * lazy logging - after generating page write logs to file or send them to server |
98 | 98 | */ |
99 | - public function send () { |
|
99 | + public function send() { |
|
100 | 100 | if (empty($this->logs)) { |
101 | 101 | //we dont have to send anything |
102 | 102 | return; |
@@ -119,9 +119,9 @@ discard block |
||
119 | 119 | $this->logs = array(); |
120 | 120 | } |
121 | 121 | |
122 | - public static function addRollbarClassloader (array $params) { |
|
122 | + public static function addRollbarClassloader(array $params) { |
|
123 | 123 | //add classloader for psr/log composer package |
124 | - \ClassLoader::addLoader("Psr", function (string $class_name) { |
|
124 | + \ClassLoader::addLoader("Psr", function(string $class_name) { |
|
125 | 125 | $path = PSR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
126 | 126 | |
127 | 127 | if (file_exists($path)) { |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | }); |
134 | 134 | |
135 | 135 | //add classloader for monolog/monolog composer package |
136 | - \ClassLoader::addLoader("Monolog", function (string $class_name) { |
|
136 | + \ClassLoader::addLoader("Monolog", function(string $class_name) { |
|
137 | 137 | $path = MONOLOG_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
138 | 138 | |
139 | 139 | if (file_exists($path)) { |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | }); |
146 | 146 | |
147 | 147 | //add classloader for rollbar sdk |
148 | - \ClassLoader::addLoader("Rollbar", function (string $class_name) { |
|
148 | + \ClassLoader::addLoader("Rollbar", function(string $class_name) { |
|
149 | 149 | $path = ROLLBAR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
150 | 150 | |
151 | 151 | if (file_exists($path)) { |
@@ -34,128 +34,128 @@ |
||
34 | 34 | use LogLevel; |
35 | 35 | |
36 | 36 | if (!defined('ROLLBAR_SDK_DIR')) { |
37 | - define('ROLLBAR_SDK_DIR', dirname(__FILE__) . "/../rollbar-php-1.6.2/"); |
|
37 | + define('ROLLBAR_SDK_DIR', dirname(__FILE__) . "/../rollbar-php-1.6.2/"); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | if (!defined('PSR_SDK_DIR')) { |
41 | - define('PSR_SDK_DIR', dirname(__FILE__) . "/../log-1.0.1/"); |
|
41 | + define('PSR_SDK_DIR', dirname(__FILE__) . "/../log-1.0.1/"); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | if (!defined('MONOLOG_SDK_DIR')) { |
45 | - define('MONOLOG_SDK_DIR', dirname(__FILE__) . "/../monolog-1.23.0/src/"); |
|
45 | + define('MONOLOG_SDK_DIR', dirname(__FILE__) . "/../monolog-1.23.0/src/"); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | class RollbarLoggingProvider implements LogProvider { |
49 | 49 | |
50 | - protected $logs = array(); |
|
51 | - |
|
52 | - /** |
|
53 | - * initialize logging provider |
|
54 | - */ |
|
55 | - public function init () { |
|
56 | - $preferences = new Preferences("plugin_rollbar"); |
|
57 | - $access_token = $preferences->get("access_token", "none"); |
|
58 | - $environment = $preferences->get("environment", "development"); |
|
59 | - |
|
60 | - if ($access_token === "none") { |
|
61 | - //access token wasnt set yet |
|
62 | - return; |
|
63 | - } |
|
64 | - |
|
65 | - Rollbar::init( |
|
66 | - array( |
|
67 | - 'access_token' => $access_token, |
|
68 | - 'environment' => $environment, |
|
69 | - |
|
70 | - // optional - path to directory your code is in. used for linking stack traces. |
|
71 | - //'root' => ROOT_PATH, |
|
72 | - 'included_errno' => E_ALL//Note: If you wish to log E_NOTICE errors make sure to pass 'included_errno' => E_ALL to Rollbar::init |
|
73 | - ), |
|
74 | - true, |
|
75 | - true, |
|
76 | - true |
|
77 | - ); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * log message |
|
82 | - */ |
|
83 | - public function log (string $level, string $message, $args = array()) { |
|
84 | - $this->logs[] = array( |
|
85 | - 'level' => $level, |
|
86 | - 'message' => $message, |
|
87 | - 'args' => $args |
|
88 | - ); |
|
89 | - |
|
90 | - if ($level === LogLevel::ERROR || $level === LogLevel::CRITICAL) { |
|
91 | - //send it directly to rollbar server |
|
92 | - $this->send(); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * lazy logging - after generating page write logs to file or send them to server |
|
98 | - */ |
|
99 | - public function send () { |
|
100 | - if (empty($this->logs)) { |
|
101 | - //we dont have to send anything |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - foreach ($this->logs as $entry) { |
|
106 | - //send log to server |
|
107 | - $response = Rollbar::log( |
|
108 | - $entry['level'], |
|
109 | - $entry['message'], |
|
110 | - $entry['args'] // key-value additional data |
|
111 | - ); |
|
112 | - |
|
113 | - if (!$response->wasSuccessful()) { |
|
114 | - throw new \IllegalStateException('logging with Rollbar failed'); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - //clear logs array |
|
119 | - $this->logs = array(); |
|
120 | - } |
|
121 | - |
|
122 | - public static function addRollbarClassloader (array $params) { |
|
123 | - //add classloader for psr/log composer package |
|
124 | - \ClassLoader::addLoader("Psr", function (string $class_name) { |
|
125 | - $path = PSR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
|
126 | - |
|
127 | - if (file_exists($path)) { |
|
128 | - require($path); |
|
129 | - } else { |
|
130 | - echo "Couldnt load psr class: " . $class_name . " (expected path: " . $path . ")!"; |
|
131 | - exit; |
|
132 | - } |
|
133 | - }); |
|
134 | - |
|
135 | - //add classloader for monolog/monolog composer package |
|
136 | - \ClassLoader::addLoader("Monolog", function (string $class_name) { |
|
137 | - $path = MONOLOG_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
|
138 | - |
|
139 | - if (file_exists($path)) { |
|
140 | - require($path); |
|
141 | - } else { |
|
142 | - echo "Couldnt load monolog class: " . $class_name . " (expected path: " . $path . ")!"; |
|
143 | - exit; |
|
144 | - } |
|
145 | - }); |
|
146 | - |
|
147 | - //add classloader for rollbar sdk |
|
148 | - \ClassLoader::addLoader("Rollbar", function (string $class_name) { |
|
149 | - $path = ROLLBAR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
|
150 | - |
|
151 | - if (file_exists($path)) { |
|
152 | - require($path); |
|
153 | - } else { |
|
154 | - echo "Couldnt load rollbar class: " . $class_name . " (expected path: " . $path . ")!"; |
|
155 | - exit; |
|
156 | - } |
|
157 | - }); |
|
158 | - } |
|
50 | + protected $logs = array(); |
|
51 | + |
|
52 | + /** |
|
53 | + * initialize logging provider |
|
54 | + */ |
|
55 | + public function init () { |
|
56 | + $preferences = new Preferences("plugin_rollbar"); |
|
57 | + $access_token = $preferences->get("access_token", "none"); |
|
58 | + $environment = $preferences->get("environment", "development"); |
|
59 | + |
|
60 | + if ($access_token === "none") { |
|
61 | + //access token wasnt set yet |
|
62 | + return; |
|
63 | + } |
|
64 | + |
|
65 | + Rollbar::init( |
|
66 | + array( |
|
67 | + 'access_token' => $access_token, |
|
68 | + 'environment' => $environment, |
|
69 | + |
|
70 | + // optional - path to directory your code is in. used for linking stack traces. |
|
71 | + //'root' => ROOT_PATH, |
|
72 | + 'included_errno' => E_ALL//Note: If you wish to log E_NOTICE errors make sure to pass 'included_errno' => E_ALL to Rollbar::init |
|
73 | + ), |
|
74 | + true, |
|
75 | + true, |
|
76 | + true |
|
77 | + ); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * log message |
|
82 | + */ |
|
83 | + public function log (string $level, string $message, $args = array()) { |
|
84 | + $this->logs[] = array( |
|
85 | + 'level' => $level, |
|
86 | + 'message' => $message, |
|
87 | + 'args' => $args |
|
88 | + ); |
|
89 | + |
|
90 | + if ($level === LogLevel::ERROR || $level === LogLevel::CRITICAL) { |
|
91 | + //send it directly to rollbar server |
|
92 | + $this->send(); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * lazy logging - after generating page write logs to file or send them to server |
|
98 | + */ |
|
99 | + public function send () { |
|
100 | + if (empty($this->logs)) { |
|
101 | + //we dont have to send anything |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + foreach ($this->logs as $entry) { |
|
106 | + //send log to server |
|
107 | + $response = Rollbar::log( |
|
108 | + $entry['level'], |
|
109 | + $entry['message'], |
|
110 | + $entry['args'] // key-value additional data |
|
111 | + ); |
|
112 | + |
|
113 | + if (!$response->wasSuccessful()) { |
|
114 | + throw new \IllegalStateException('logging with Rollbar failed'); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + //clear logs array |
|
119 | + $this->logs = array(); |
|
120 | + } |
|
121 | + |
|
122 | + public static function addRollbarClassloader (array $params) { |
|
123 | + //add classloader for psr/log composer package |
|
124 | + \ClassLoader::addLoader("Psr", function (string $class_name) { |
|
125 | + $path = PSR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
|
126 | + |
|
127 | + if (file_exists($path)) { |
|
128 | + require($path); |
|
129 | + } else { |
|
130 | + echo "Couldnt load psr class: " . $class_name . " (expected path: " . $path . ")!"; |
|
131 | + exit; |
|
132 | + } |
|
133 | + }); |
|
134 | + |
|
135 | + //add classloader for monolog/monolog composer package |
|
136 | + \ClassLoader::addLoader("Monolog", function (string $class_name) { |
|
137 | + $path = MONOLOG_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
|
138 | + |
|
139 | + if (file_exists($path)) { |
|
140 | + require($path); |
|
141 | + } else { |
|
142 | + echo "Couldnt load monolog class: " . $class_name . " (expected path: " . $path . ")!"; |
|
143 | + exit; |
|
144 | + } |
|
145 | + }); |
|
146 | + |
|
147 | + //add classloader for rollbar sdk |
|
148 | + \ClassLoader::addLoader("Rollbar", function (string $class_name) { |
|
149 | + $path = ROLLBAR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php"; |
|
150 | + |
|
151 | + if (file_exists($path)) { |
|
152 | + require($path); |
|
153 | + } else { |
|
154 | + echo "Couldnt load rollbar class: " . $class_name . " (expected path: " . $path . ")!"; |
|
155 | + exit; |
|
156 | + } |
|
157 | + }); |
|
158 | + } |
|
159 | 159 | |
160 | 160 | } |
161 | 161 |
@@ -53,9 +53,9 @@ |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | if ($bytes > 1024 * 1024) { |
56 | - return round($bytes / 1024 / 1024, 2).' MB'; |
|
56 | + return round($bytes / 1024 / 1024, 2) . ' MB'; |
|
57 | 57 | } elseif ($bytes > 1024) { |
58 | - return round($bytes / 1024, 2).' KB'; |
|
58 | + return round($bytes / 1024, 2) . ' KB'; |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | return $bytes . ' B'; |
@@ -33,11 +33,11 @@ |
||
33 | 33 | $replacements = array(); |
34 | 34 | foreach ($record['context'] as $key => $val) { |
35 | 35 | if (is_null($val) || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) { |
36 | - $replacements['{'.$key.'}'] = $val; |
|
36 | + $replacements['{' . $key . '}'] = $val; |
|
37 | 37 | } elseif (is_object($val)) { |
38 | - $replacements['{'.$key.'}'] = '[object '.get_class($val).']'; |
|
38 | + $replacements['{' . $key . '}'] = '[object ' . get_class($val) . ']'; |
|
39 | 39 | } else { |
40 | - $replacements['{'.$key.'}'] = '['.gettype($val).']'; |
|
40 | + $replacements['{' . $key . '}'] = '[' . gettype($val) . ']'; |
|
41 | 41 | } |
42 | 42 | } |
43 | 43 |
@@ -42,7 +42,7 @@ |
||
42 | 42 | protected function send($chunk) |
43 | 43 | { |
44 | 44 | if (!is_resource($this->socket)) { |
45 | - throw new \LogicException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore'); |
|
45 | + throw new \LogicException('The UdpSocket to ' . $this->ip . ':' . $this->port . ' has been closed and can not be written to anymore'); |
|
46 | 46 | } |
47 | 47 | socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port); |
48 | 48 | } |
@@ -48,7 +48,7 @@ |
||
48 | 48 | $this->factor = $factor; |
49 | 49 | |
50 | 50 | if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) { |
51 | - throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object"); |
|
51 | + throw new \RuntimeException("The given handler (" . json_encode($this->handler) . ") is not a callable nor a Monolog\Handler\HandlerInterface object"); |
|
52 | 52 | } |
53 | 53 | } |
54 | 54 |