1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phile\Plugin; |
4
|
|
|
|
5
|
|
|
use Phile\Exception\PluginException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PluginRepository manages plugin loading |
9
|
|
|
* |
10
|
|
|
* @author PhileCMS |
11
|
|
|
* @link https://philecms.com |
12
|
|
|
* @license http://opensource.org/licenses/MIT |
13
|
|
|
* @package Phile\Core |
14
|
|
|
*/ |
15
|
|
|
class PluginRepository |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array of AbstractPlugin |
20
|
|
|
*/ |
21
|
|
|
protected $plugins = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array errors during load; keys: 'message' and 'code' |
25
|
|
|
*/ |
26
|
|
|
protected $loadErrors = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var directory plug-in root directory |
30
|
|
|
*/ |
31
|
|
|
protected $directory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
* |
36
|
|
|
* @param string $directory root directory for plug-in folder |
37
|
|
|
*/ |
38
|
28 |
|
public function __construct($directory) |
39
|
|
|
{ |
40
|
28 |
|
$this->directory = $directory; |
|
|
|
|
41
|
28 |
|
spl_autoload_register([$this, 'autoload']); |
42
|
28 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* get load errors |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
28 |
|
public function getLoadErrors() |
50
|
|
|
{ |
51
|
28 |
|
return $this->loadErrors; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* loads all activated plugins from $settings |
56
|
|
|
* |
57
|
|
|
* @param array $settings plugin-settings |
58
|
|
|
* @return array of AbstractPlugin |
59
|
|
|
* @throws PluginException |
60
|
|
|
*/ |
61
|
28 |
|
public function loadAll($settings) |
62
|
|
|
{ |
63
|
28 |
|
$this->reset(); |
64
|
28 |
|
foreach ($settings as $pluginKey => $config) { |
65
|
28 |
|
if (!isset($config['active']) || !$config['active']) { |
66
|
1 |
|
continue; |
67
|
|
|
} |
68
|
|
|
try { |
69
|
28 |
|
$this->plugins[$pluginKey] = $this->load($pluginKey); |
70
|
1 |
|
} catch (PluginException $e) { |
71
|
1 |
|
$this->loadErrors[] = [ |
72
|
1 |
|
'message' => $e->getMessage(), |
73
|
28 |
|
'code' => $e->getCode() |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
28 |
|
return $this->plugins; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* load and return single plugin |
82
|
|
|
* |
83
|
|
|
* @param $pluginKey |
84
|
|
|
* @return AbstractPlugin |
85
|
|
|
* @throws PluginException |
86
|
|
|
*/ |
87
|
28 |
|
protected function load($pluginKey) |
88
|
|
|
{ |
89
|
28 |
|
list($vendor, $pluginName) = explode('\\', $pluginKey); |
90
|
|
|
// uppercase first letter convention |
91
|
28 |
|
$pluginClassName = '\\Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin'; |
92
|
|
|
|
93
|
28 |
|
if (!class_exists($pluginClassName)) { |
94
|
1 |
|
throw new PluginException( |
95
|
1 |
|
"the plugin '{$pluginKey}' could not be loaded!", |
96
|
1 |
|
1398536479 |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var \Phile\Plugin\AbstractPlugin $plugin |
102
|
|
|
*/ |
103
|
27 |
|
$plugin = new $pluginClassName; |
104
|
27 |
|
if (($plugin instanceof AbstractPlugin) === false) { |
105
|
|
|
throw new PluginException( |
106
|
|
|
"the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin", |
107
|
|
|
1398536526 |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
27 |
|
$plugin->initializePlugin($pluginKey); |
|
|
|
|
112
|
27 |
|
return $plugin; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* clear out repository |
117
|
|
|
*/ |
118
|
28 |
|
protected function reset() |
119
|
|
|
{ |
120
|
28 |
|
$this->loadErrors = []; |
121
|
28 |
|
$this->plugins = []; |
122
|
28 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* auto-loader plugin namespace |
126
|
|
|
* |
127
|
|
|
* @param $className |
128
|
|
|
*/ |
129
|
5 |
|
public function autoload($className) |
130
|
|
|
{ |
131
|
5 |
|
if (strpos($className, "Phile\\Plugin\\") !== 0) { |
|
|
|
|
132
|
2 |
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
$className = substr($className, 13); |
136
|
4 |
|
$classNameParts = explode('\\', $className); |
137
|
4 |
|
$pluginVendor = lcfirst(array_shift($classNameParts)); |
138
|
4 |
|
$pluginName = lcfirst(array_shift($classNameParts)); |
139
|
4 |
|
$classPath = array_merge( |
140
|
4 |
|
[$pluginVendor, $pluginName, 'Classes'], |
141
|
4 |
|
$classNameParts |
142
|
|
|
); |
143
|
|
|
|
144
|
4 |
|
$fileName = $this->directory . implode(DS, $classPath) . '.php'; |
145
|
4 |
|
if (file_exists($fileName)) { |
146
|
3 |
|
include_once $fileName; |
147
|
|
|
} |
148
|
4 |
|
} |
149
|
|
|
} |
150
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..