-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugins.php
executable file
·167 lines (130 loc) · 3.43 KB
/
plugins.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
Class:
<Plugins>
Loads all plugins specified in the <Current::$config> and calls them when the appropriate hooks are called in client code. <Plugins> is a singleton.
*/
class Plugins
{
// Property: <Plugins::$plugins>
// The plugin instances.
private $plugins = array();
/*
Constructor:
<Plugins::__construct>
Calling the constructor loads and instansiates every plugin specified in plugins.load
*/
public function __construct()
{
$this->loadPlugins();
}
/*
Method:
<Plugns::loadPlugins>
Load plugins
*/
private function loadPlugins()
{
$plugins = Current::$config->get('plugins.load');
foreach ( $plugins as $plugin )
{
$path = Current::$config->get('plugins.' . $plugin . '.path');
require($path);
$name = Plugins::makeName($path);
$this->plugins[$plugin] = new $name();
}
}
public function get($plugin)
{
if ( ! isset($this->plugins[$plugin]) )
return false;
return $this->plugins[$plugin];
}
/*
Method:
<Plugins::addInstance>
Adds an instance to the <Plugins::$plugins>-array.
Paramaters:
Plugin $plugin - The plugin instance to append.
*/
public function addInstance(Plugin $instance)
{
$this->plugins[] = $instance;
}
/*
Method:
<Plugins::removeInstance>
Removes an instance from <Plugins::$plugins>.
Parameters:
Plugin $plugin - The instance to remove.
*/
public function removeInstance(Plugin $instance)
{
foreach ( $this->plugins as $key => $plugin )
{
if ( $plugin === $instance )
{
unset($this->plugins[$key]);
}
}
}
/*
Method:
<Plugins::hook>
Call the plugins' hooks.
Parameters:
$method - The "name" of the hook.
$arg1 - Argument to be passed to the hook-method.
$argN - ...
*/
public function hook($method)
{
$args = func_get_args();
$args = array_slice($args, 1);
foreach ( $this->plugins as $plugin )
{
call_user_func_array(array($plugin, $method), $args);
}
}
/*
Method:
<Plugins::makeName>
Makes a plugin name from a corresponding filename. Following these conventions:
1. Replace _ with a space ( )
2. Uppercase the first letter in every word
3. Remove spaces
Parameters:
$filename - The filename of the plugin
Returns:
The Pluginname
*/
public static function makeName($filename)
{
$name = preg_replace('/plugin\.(.*?)\.php/', '$1', array_last(explode('/', $filename)));
return str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
}
}
/*
Class:
<Plugin>
Abstract base class for all plugins.
*/
abstract class Plugin
{
// FrontController-related hooks
public function prePathParse(Controller $controller, StaticServer $server) {}
public function postPathParse($args) {}
public function postRun() {}
public function preStaticServe(StaticServer $server) {}
public function postStaticServe(StaticServer $server) {}
// Command-related hooks
public function commandRun(Command $command, $method, $args) {}
public function postCommandRun(Command $command, $method, $args) {}
// ORM-related hooks
public function dbPopulate(DataMapper $mapper, DomainObject $object) {}
public function dbFind(DataMapper $mapper, $args) {}
public function dbInsert(DataMapper $mapper, DomainObject $object) {}
public function dbUpdate(DataMapper $mapper, DomainObject $object) {}
public function dbRemove(DataMapper $mapper, $id) {}
public function postDBQuery(DataMapper $mapper, $query, DBDriver $db) {}
}