|
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
|
|
|
* get load errors |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function getLoadErrors() |
|
34
|
|
|
{ |
|
35
|
4 |
|
return $this->loadErrors; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* loads all activated plugins from $settings |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $settings plugin-settings |
|
42
|
|
|
* @return array of AbstractPlugin |
|
43
|
|
|
* @throws PluginException |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public function loadAll($settings) |
|
46
|
|
|
{ |
|
47
|
4 |
|
$this->reset(); |
|
48
|
4 |
|
foreach ($settings as $pluginKey => $config) { |
|
49
|
4 |
|
if (!isset($config['active']) || !$config['active']) { |
|
50
|
1 |
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
try { |
|
53
|
4 |
|
$this->plugins[$pluginKey] = $this->load($pluginKey); |
|
54
|
1 |
|
} catch (PluginException $e) { |
|
55
|
1 |
|
$this->loadErrors[] = [ |
|
56
|
1 |
|
'message' => $e->getMessage(), |
|
57
|
4 |
|
'code' => $e->getCode() |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
4 |
|
return $this->plugins; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* load and return single plugin |
|
66
|
|
|
* |
|
67
|
|
|
* @param $pluginKey |
|
68
|
|
|
* @return AbstractPlugin |
|
69
|
|
|
* @throws PluginException |
|
70
|
|
|
*/ |
|
71
|
4 |
|
protected function load($pluginKey) |
|
72
|
|
|
{ |
|
73
|
4 |
|
list($vendor, $pluginName) = explode('\\', $pluginKey); |
|
74
|
|
|
// uppercase first letter convention |
|
75
|
4 |
|
$pluginClassName = '\\Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin'; |
|
76
|
|
|
|
|
77
|
4 |
|
if (!class_exists($pluginClassName)) { |
|
78
|
1 |
|
throw new PluginException( |
|
79
|
1 |
|
"the plugin '{$pluginKey}' could not be loaded!", |
|
80
|
1 |
|
1398536479 |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @var \Phile\Plugin\AbstractPlugin $plugin |
|
86
|
|
|
*/ |
|
87
|
3 |
|
$plugin = new $pluginClassName; |
|
88
|
3 |
|
if (($plugin instanceof AbstractPlugin) === false) { |
|
89
|
|
|
throw new PluginException( |
|
90
|
|
|
"the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin", |
|
91
|
|
|
1398536526 |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
$plugin->initializePlugin($pluginKey); |
|
|
|
|
|
|
96
|
3 |
|
return $plugin; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* clear out repository |
|
101
|
|
|
*/ |
|
102
|
4 |
|
protected function reset() |
|
103
|
|
|
{ |
|
104
|
4 |
|
$this->loadErrors = []; |
|
105
|
4 |
|
$this->plugins = []; |
|
106
|
4 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* auto-loader plugin namespace |
|
110
|
|
|
* |
|
111
|
|
|
* @param $className |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public static function autoload($className) |
|
114
|
|
|
{ |
|
115
|
3 |
|
if (strpos($className, "Phile\\Plugin\\") !== 0) { |
|
|
|
|
|
|
116
|
1 |
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
$className = substr($className, 13); |
|
120
|
2 |
|
$classNameParts = explode('\\', $className); |
|
121
|
2 |
|
$pluginVendor = lcfirst(array_shift($classNameParts)); |
|
122
|
2 |
|
$pluginName = lcfirst(array_shift($classNameParts)); |
|
123
|
2 |
|
$classPath = array_merge( |
|
124
|
2 |
|
[$pluginVendor, $pluginName, 'Classes'], |
|
125
|
2 |
|
$classNameParts |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
2 |
|
$fileName = PLUGINS_DIR . implode(DIRECTORY_SEPARATOR, $classPath) . '.php'; |
|
129
|
2 |
|
if (file_exists($fileName)) { |
|
130
|
1 |
|
include_once $fileName; |
|
131
|
|
|
} |
|
132
|
2 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.