-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAbstractPlugin.php
More file actions
340 lines (231 loc) · 8.36 KB
/
AbstractPlugin.php
File metadata and controls
340 lines (231 loc) · 8.36 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Created by Asier Marqués <asiermarques@gmail.com>
* Date: 15/5/16
* Time: 18:10
*/
namespace Simettric\Sense;
use Collections\Collection;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Simettric\Sense\Annotations\AdminRoute;
use Simettric\Sense\Router\RouteInterface;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Finder\Finder;
abstract class AbstractPlugin
{
private $base_namespace = null;
private $has_routes = false;
private $rootDir = false;
/**
* @var ContainerInterface|null
*/
private $container = null;
public function setRootDir($dir)
{
$this->rootDir = $dir;
}
public function getConfigLocations()
{
return [ $this->rootDir . "/Config"];
}
public function getAdminControllerLocations()
{
return [ $this->rootDir . "/AdminController" ];
}
public function getControllerLocations()
{
return [ $this->rootDir . "/Controller" ];
}
public function getTemplateLocations()
{
return [ get_stylesheet_directory() . "/" . $this->getName(), $this->rootDir . "/View"];
}
public function getAdminTemplateLocations()
{
return [ $this->rootDir . "/AdminView"];
}
public function get($key)
{
if(!$this->container)
{
throw new \Exception("Service container not set yet");
}
return $this->container->get($key);
}
public function getParameter($key)
{
if(!$this->container)
{
throw new \Exception("Service container not set yet");
}
return $this->container->getParameter($key);
}
public abstract function getName();
public function getBaseNamespace()
{
if(!$this->base_namespace){
$ref = new \ReflectionObject($this);
$this->base_namespace = $ref->getNamespaceName();
}
return $this->base_namespace;
}
public function registerAdminRoutes(Collection $routeContainer)
{
if(!count($this->getAdminControllerLocations())) return;
$enabled_locations = array();
foreach ($this->getAdminControllerLocations() as $location)
{
if(is_dir($location))
$enabled_locations[] = $location;
}
if(!count($enabled_locations)) return;
AnnotationRegistry::registerFile(__DIR__ . "/Annotations/AdminRoute.php");
$finder = new Finder();
$finder->files()->in($enabled_locations);
$files = $finder->files()->name('*Controller.php');
$this->has_routes = false;
foreach($files as $file){
$reader = new AnnotationReader();
$class = $this->getClassInFile($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename());
$reflClass = new \ReflectionClass($class);
foreach($reflClass->getMethods() as $method) {
$classAnnotations = $reader->getMethodAnnotations($method);
/**
* @var $route AdminRoute
*/
foreach ($classAnnotations as $route) {
$route->setActionMethod($method->getName());
$route->setControllerClassName($reflClass->getName());
$route->setPlugin($this);
$route->configure();
$routeContainer->add($route);
if(!$this->has_routes){
$this->has_routes = true;
}
}
}
}
}
public function registerRoutes(Collection $routeContainer)
{
if(!count($this->getControllerLocations())) return;
$enabled_locations = array();
foreach ($this->getControllerLocations() as $location)
{
if(is_dir($location))
$enabled_locations[] = $location;
}
if(!count($enabled_locations)) return;
AnnotationRegistry::registerFile(__DIR__ . "/Annotations/Route.php");
$finder = new Finder();
$finder->files()->in($enabled_locations);
$files = $finder->files()->name('*Controller.php');
$this->has_routes = false;
foreach($files as $file){
$reader = new AnnotationReader();
$class = $this->getClassInFile($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename());
$reflClass = new \ReflectionClass($class);
foreach($reflClass->getMethods() as $method) {
$classAnnotations = $reader->getMethodAnnotations($method);
/**
* @var $routeInterface RouteInterface
*/
foreach ($classAnnotations as $routeInterface) {
$routeInterface->setActionMethod($method->getName());
$routeInterface->setControllerClassName($reflClass->getName());
$routeInterface->setPlugin($this);
$routeInterface->configure();
$routeContainer->add($routeInterface);
if(!$this->has_routes){
$this->has_routes = true;
}
}
}
}
}
public function registerServices(ContainerBuilder $container)
{
$this->container = $container;
if(count($this->getConfigLocations())){
try{
$loader = new YamlFileLoader($container, new FileLocator($this->getConfigLocations()));
$loader->load('services.yml');
}catch (FileLocatorFileNotFoundException $e)
{
}
}
}
public function getClassInFile($file)
{
//Grab the contents of the file
$contents = file_get_contents($file);
//Start with a blank namespace and class
$namespace = $class = "";
//Set helper values to know that we have found the namespace/class token and need to collect the string values after them
$getting_namespace = $getting_class = false;
//Go through each token and evaluate it as necessary
foreach (token_get_all($contents) as $token) {
//If this token is the namespace declaring, then flag that the next tokens will be the namespace name
if (is_array($token) && $token[0] == T_NAMESPACE) {
$getting_namespace = true;
}
//If this token is the class declaring, then flag that the next tokens will be the class name
if (is_array($token) && $token[0] == T_CLASS) {
$getting_class = true;
}
//While we're grabbing the namespace name...
if ($getting_namespace === true) {
//If the token is a string or the namespace separator...
if(is_array($token) && in_array($token[0], [T_STRING, T_NS_SEPARATOR])) {
//Append the token's value to the name of the namespace
$namespace .= $token[1];
}
else if ($token === ';') {
//If the token is the semicolon, then we're done with the namespace declaration
$getting_namespace = false;
}
}
//While we're grabbing the class name...
if ($getting_class === true) {
//If the token is a string, it's the name of the class
if(is_array($token) && $token[0] == T_STRING) {
//Store the token's value as the class name
$class = $token[1];
//Got what we need, stope here
break;
}
}
}
//Build the fully-qualified class name and return it
return $namespace ? $namespace . '\\' . $class : $class;
}
/**
* You can overwrite this function if you have implemented any pluggable functions
* https://codex.wordpress.org/Pluggable_Functions
*
* Note: Themes can´t implement pluggable functions
*/
public function registerPluggableFunctions()
{
if($this->isTheme()) return;
}
public function isTheme()
{
return false;
}
public function hasRoutes()
{
return $this->has_routes;
}
public function onActivate()
{
if($this->hasRoutes()){
flush_rewrite_rules();
}
}
}