1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plugin class |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Plugin; |
6
|
|
|
|
7
|
|
|
use Phile\Core\Container; |
8
|
|
|
use Phile\Core\Utility; |
9
|
|
|
use Phile\Gateway\EventObserverInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* the AbstractPlugin class for implementing a plugin for PhileCMS |
13
|
|
|
* |
14
|
|
|
* @author PhileCMS |
15
|
|
|
* @link https://philecms.com |
16
|
|
|
* @license http://opensource.org/licenses/MIT |
17
|
|
|
* @package Phile\Plugin |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractPlugin implements EventObserverInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string plugin attributes |
24
|
|
|
*/ |
25
|
|
|
private $plugin = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array subscribed Phile events ['eventName' => 'classMethodToCall'] |
29
|
|
|
*/ |
30
|
|
|
protected $events = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array the plugin settings |
34
|
|
|
*/ |
35
|
|
|
protected $settings = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* initialize plugin |
39
|
|
|
* |
40
|
|
|
* try to keep all initialization in one method to have a clean class |
41
|
|
|
* for the plugin-user |
42
|
|
|
* |
43
|
|
|
* @param string $pluginKey |
44
|
|
|
* @deprecated since 1.5.1 will be declared 'final' |
45
|
|
|
*/ |
46
|
31 |
|
public function initializePlugin($pluginKey) |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* init $plugin property |
50
|
|
|
*/ |
51
|
31 |
|
$this->plugin['key'] = $pluginKey; |
52
|
31 |
|
list($vendor, $name) = explode('\\', $this->plugin['key']); |
53
|
31 |
|
$DS = DIRECTORY_SEPARATOR; |
54
|
31 |
|
$this->plugin['dir'] = PLUGINS_DIR . $vendor . $DS . $name . $DS; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* init events |
58
|
|
|
*/ |
59
|
31 |
|
foreach ($this->events as $event => $method) { |
60
|
31 |
|
Container::getInstance()->get('Phile_EventBus')->register($event, $this); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* init plugin settings |
65
|
|
|
*/ |
66
|
31 |
|
$defaults = Utility::load($this->getPluginPath('config.php')); |
67
|
31 |
|
if (empty($defaults) || !is_array($defaults)) { |
68
|
26 |
|
$defaults = []; |
69
|
|
|
} |
70
|
|
|
|
71
|
31 |
|
$globals = Container::getInstance()->get('Phile_Config')->toArray(); |
72
|
31 |
|
if (!isset($globals['plugins'][$pluginKey])) { |
73
|
|
|
$globals['plugins'][$pluginKey] = []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// settings precedence: global > default > class |
77
|
31 |
|
$this->settings = array_replace_recursive( |
78
|
31 |
|
$this->settings, |
79
|
31 |
|
$defaults, |
80
|
31 |
|
$globals['plugins'][$pluginKey] |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
// backwards compatibility to Phile 1.4 |
84
|
31 |
|
$this->injectSettings($this->settings); |
|
|
|
|
85
|
|
|
|
86
|
31 |
|
$globals['plugins'][$pluginKey]['settings'] = $this->settings; |
87
|
31 |
|
Container::getInstance()->get('Phile_Config')->set($globals); |
88
|
31 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* inject settings |
92
|
|
|
* |
93
|
|
|
* backwards compatibility to Phile 1.4 |
94
|
|
|
* |
95
|
|
|
* @param array $settings |
96
|
|
|
* @deprecated since 1.5.1 will be removed |
97
|
|
|
*/ |
98
|
31 |
|
public function injectSettings(array $settings = null) |
|
|
|
|
99
|
|
|
{ |
100
|
31 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* implements EventObserverInterface |
104
|
|
|
* |
105
|
|
|
* @param string $eventKey |
106
|
|
|
* @param null $data |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
28 |
|
public function on($eventKey, $data = null) |
110
|
|
|
{ |
111
|
28 |
|
if (!isset($this->events[$eventKey])) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
28 |
|
$method = $this->events[$eventKey]; |
115
|
28 |
|
if (!is_callable([$this, $method])) { |
116
|
1 |
|
$class = get_class($this); |
117
|
1 |
|
throw new \RuntimeException( |
118
|
1 |
|
"Event $eventKey can't invoke $class::$method(). Not callable.", |
119
|
1 |
|
1428564865 |
120
|
|
|
); |
121
|
|
|
} |
122
|
27 |
|
$this->{$this->events[$eventKey]}($data); |
123
|
27 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* get file path to plugin root (trailing slash) or to a sub-item |
127
|
|
|
* |
128
|
|
|
* @param string $subPath |
129
|
|
|
* @return null|string null if item does not exist |
130
|
|
|
*/ |
131
|
31 |
|
protected function getPluginPath($subPath = '') |
132
|
|
|
{ |
133
|
31 |
|
return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.