Test Failed
Push — master ( 1132d1...894171 )
by Justin
43:27 queued 39:25
created

Plugins::isPluginInArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 07.04.2018
25
 * Time: 19:12
26
 */
27
28
class Plugins {
29
30
	public static function listAvailablePluginNames () : array {
31
		$names = array();
32
33
		//use directory iterator
34
		$dir = new DirectoryIterator(PLUGIN_PATH);
35
36
		foreach ($dir as $fileinfo) {
37
			if ($fileinfo->isDir() && !$fileinfo->isDot()) {
38
				$names[] = $fileinfo->getFilename();
39
			}
40
		}
41
42
		return $names;
43
	}
44
45
	public static function listInstalledPluginNames () : array {
46
		if (Cache::contains("plugins", "installed_plugin_names")) {
47
			return Cache::get("plugins", "installed_plugin_names");
48
		} else {
49
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugins` WHERE `installed` = '1'; ");
50
51
			$array = array();
52
53
			foreach ($rows as $row) {
54
				$array[] = $row['name'];
55
			}
56
57
			//cache rows
58
			Cache::put("plugins", "installed_plugin_names", $array);
59
60
			return $array;
61
		}
62
	}
63
64
	public static function listInstalledPlugins () : array {
65
		if (Cache::contains("plugins", "installed_plugins")) {
66
			return Cache::get("plugins", "installed_plugins");
67
		} else {
68
			//read installed plugins from database
69
			$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}plugins` WHERE `installed` = '1'; ");
70
71
			$plugins = array();
72
73
			foreach ($rows as $row) {
74
				//create new plugin instance
75
				$plugin = new Plugin($row['name'], $row);
76
77
				//load plugin
78
				$plugin->load();
79
80
				$plugins[$plugin->getName()] = $plugin;
81
			}
82
83
			//cache plugins
84
			Cache::put("plugins", "installed_plugins", $plugins);
85
86
			return $plugins;
87
		}
88
	}
89
90
	public static function listUninstalledPlugins () : array {
91
		$installed_plugin_names = self::listInstalledPluginNames();
92
93
		//create new empty list
94
		$list = array();
95
96
		$dir = new DirectoryIterator(PLUGIN_PATH);
97
98
		foreach ($dir as $fileInfo) {
99
			if ($fileInfo->isDot()) {
100
				//dont parse directory "."
101
				continue;
102
			}
103
104
			if (!$fileInfo->isDir()) {
105
				//we only search for directories
106
				continue;
107
			}
108
109
			//get directory name
110
			$name = $fileInfo->getFilename();
111
112
			//check, if plugin is already installed
113
			if (in_array($name, $installed_plugin_names)) {
114
				continue;
115
			}
116
117
			//create and load new plugin
118
			$plugin = new Plugin($name);
119
			$plugin->load();
120
121
			//add plugin to list
122
			$list[] = $plugin;
123
		}
124
125
		return $list;
126
	}
127
128
	public static function clearCache () {
129
		Cache::clear("plugins");
130
	}
131
132
}
133
134
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
135