blob: a5dbb77694f9298bc52fac5844cc4c142fecd02f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<?php
namespace ipl\Stdlib;
use ipl\Stdlib\Contract\PluginLoader;
use ipl\Stdlib\Loader\AutoloadingPluginLoader;
trait Plugins
{
/** @var array Registered plugin loaders by type */
protected $pluginLoaders = [];
/**
* Factory for plugin loaders
*
* @param PluginLoader|string $loaderOrNamespace
* @param string $postfix
*
* @return PluginLoader
*/
public static function wantPluginLoader($loaderOrNamespace, $postfix = '')
{
if ($loaderOrNamespace instanceof PluginLoader) {
$loader = $loaderOrNamespace;
} else {
$loader = new AutoloadingPluginLoader($loaderOrNamespace, $postfix);
}
return $loader;
}
/**
* Get whether a plugin loader for the given type exists
*
* @param string $type
*
* @return bool
*/
public function hasPluginLoader($type)
{
return isset($this->pluginLoaders[$type]);
}
/**
* Add a plugin loader for the given type
*
* @param string $type
* @param PluginLoader|string $loaderOrNamespace
* @param string $postfix
*
* @return $this
*/
public function addPluginLoader($type, $loaderOrNamespace, $postfix = '')
{
$loader = static::wantPluginLoader($loaderOrNamespace, $postfix);
if (! isset($this->pluginLoaders[$type])) {
$this->pluginLoaders[$type] = [];
}
array_unshift($this->pluginLoaders[$type], $loader);
return $this;
}
/**
* Load the class file of the given plugin
*
* @param string $type
* @param string $name
*
* @return string|false
*/
public function loadPlugin($type, $name)
{
if ($this->hasPluginLoader($type)) {
/** @var PluginLoader $loader */
foreach ($this->pluginLoaders[$type] as $loader) {
$class = $loader->load($name);
if ($class) {
return $class;
}
}
}
return false;
}
protected function addDefaultPluginLoader($type, $loaderOrNamespace, $postfix)
{
$this->pluginLoaders[$type][] = static::wantPluginLoader($loaderOrNamespace, $postfix);
return $this;
}
}
|