1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author PhileCMS |
4
|
|
|
* @link https://philecms.github.io/ |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @package Phile\Plugin |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Phile\Plugin; |
10
|
|
|
|
11
|
|
|
use Phile\Exception\PluginException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Represents a dire directory with plugin folders |
15
|
|
|
*/ |
16
|
|
|
class PluginDirectory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* File path to directory |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $path; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param string $path |
29
|
|
|
*/ |
30
|
41 |
|
public function __construct(string $path) |
31
|
|
|
{ |
32
|
41 |
|
$this->path = $path; |
33
|
41 |
|
spl_autoload_register([$this, 'autoload']); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Gets directory path |
38
|
|
|
* |
39
|
|
|
* @return string path |
40
|
|
|
*/ |
41
|
39 |
|
public function getPath(): string |
42
|
|
|
{ |
43
|
39 |
|
return $this->path; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Tries to create new a Plugin class from plugin in directory |
48
|
|
|
* |
49
|
|
|
* @param string $pluginKey |
50
|
|
|
* @return AbstractPlugin|null |
51
|
|
|
*/ |
52
|
37 |
|
public function newPluginInstance(string $pluginKey): ?AbstractPlugin |
53
|
|
|
{ |
54
|
37 |
|
list($vendor, $pluginName) = explode('\\', $pluginKey); |
55
|
|
|
// uppercase first letter convention |
56
|
37 |
|
$className = 'Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin'; |
57
|
37 |
|
if (!file_exists($this->filenameForClass($className))) { |
58
|
1 |
|
return null; |
59
|
|
|
}; |
60
|
|
|
|
61
|
36 |
|
$plugin = new $className; |
62
|
36 |
|
if (($plugin instanceof AbstractPlugin) === false) { |
63
|
1 |
|
throw new PluginException( |
64
|
1 |
|
"the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin", |
65
|
|
|
1398536526 |
66
|
|
|
); |
67
|
|
|
} |
68
|
35 |
|
return $plugin; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Class auto-loader plugin namespace |
73
|
|
|
* |
74
|
|
|
* @param string $className |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
37 |
|
public function autoload(string $className): void |
78
|
|
|
{ |
79
|
37 |
|
if (strpos($className, "Phile\\Plugin\\") !== 0) { |
80
|
35 |
|
return; |
81
|
|
|
} |
82
|
3 |
|
$filename = $this->filenameForClass($className); |
83
|
3 |
|
if (file_exists($filename)) { |
84
|
3 |
|
include $filename; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates file-path to class-file in plugin directory |
90
|
|
|
* |
91
|
|
|
* @param string $className |
92
|
|
|
* @return string the file path |
93
|
|
|
*/ |
94
|
38 |
|
protected function filenameForClass(string $className): string |
95
|
|
|
{ |
96
|
38 |
|
$className = str_replace('Phile\\Plugin\\', '', $className); |
97
|
38 |
|
$classNameParts = explode('\\', $className); |
98
|
38 |
|
$pluginVendor = lcfirst(array_shift($classNameParts)); |
99
|
38 |
|
$pluginName = lcfirst(array_shift($classNameParts)); |
100
|
38 |
|
$classPath = array_merge( |
101
|
38 |
|
[$pluginVendor, $pluginName, 'Classes'], |
102
|
|
|
$classNameParts |
103
|
|
|
); |
104
|
|
|
|
105
|
38 |
|
return $this->path . implode('/', $classPath) . '.php'; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|