forked from pablodip/PablodipModuleBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
524 lines (449 loc) · 12.8 KB
/
Copy pathModule.php
File metadata and controls
524 lines (449 loc) · 12.8 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
/*
* This file is part of the PablodipModuleBundle package.
*
* (c) Pablo Díez <pablodip@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pablodip\ModuleBundle\Module;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Pablodip\ModuleBundle\Action\ActionInterface;
use Pablodip\ModuleBundle\Action\RouteActionInterface;
use Pablodip\ModuleBundle\Action\ActionCollectionInterface;
use Pablodip\ModuleBundle\Extension\ExtensionInterface;
/**
* Module.
*
* @author Pablo Díez <pablodip@gmail.com>
*/
abstract class Module implements ModuleInterface
{
private $container;
private $extensions;
private $routeNamePrefix;
private $routePatternPrefix;
private $parametersToPropagate;
private $options;
private $actions;
private $controllerPreExecutes;
/**
* Constructor.
*
* @param ContainerInterface $container A container.
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->extensions = array();
$this->routeNamePrefix = '';
$this->routePatternPrefix = '';
$this->parametersToPropagate = array();
$this->options = array();
$this->actions = array();
$this->controllerPreExecutes = array();
// register extensions
foreach ($this->registerExtensions() as $extension) {
if (!$extension instanceof ExtensionInterface) {
throw new \LogicException('The extensions must be an instance of ExtensionInterface.');
}
$name = $extension->getName();
if (isset($this->extensions[$name])) {
throw new \LogicException(sprintf('Trying to register two extensions with the same name: "%s".', $name));
}
$extension->setModule($this);
$this->extensions[$name] = $extension;
}
// define configuration
foreach ($this->extensions as $extension) {
$extension->defineConfiguration();
}
$this->defineConfiguration();
// configure
foreach ($this->extensions as $extension) {
$extension->configure();
}
$this->configure();
// parse configuration
foreach ($this->extensions as $extension) {
$extension->parseConfiguration();
}
$this->parseConfiguration();
}
/**
* Here you should register the extensions.
*
* Be aware to continue registering the parent extensions.
*/
protected function registerExtensions()
{
return array();
}
/**
* Here is where you should define your configuration.
*
* Defining configuration is add options, actions, parameters to propagate.
* These things can be modified later in the configure method by other users
* in reusable modules.
*/
abstract protected function defineConfiguration();
/**
* Here is where users can configure reusable modules.
*/
protected function configure()
{
}
/**
* Here is where you should check and/or parse the configuration for reusable modules.
*
* This is mostly for checking and parsing option values.
*/
protected function parseConfiguration()
{
}
/**
* {@inheritdoc}
*/
public function getContainer()
{
return $this->container;
}
/**
* {@inheritdoc}
*/
public function getExtension($name)
{
if (!isset($this->extensions[$name])) {
throw new \InvalidArgumentException(sprintf('The extension "%s" does not exist.', $name));
}
return $this->extensions[$name];
}
/**
* {@inheritdoc}
*/
public function getExtensions()
{
return $this->extensions;
}
/**
* Sets the route name prefix.
*
* @param string $routeNamePrefix The route name prefix.
*
* @return Module The module (fluent interface).
*/
public function setRouteNamePrefix($routeNamePrefix)
{
$this->routeNamePrefix = $routeNamePrefix;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRouteNamePrefix()
{
return $this->routeNamePrefix;
}
/**
* Sets the route pattern prefix.
*
* @param string $routePatternPrefix The route pattern preffix.
*
* @return ModuleInterface The module (fluent interface).
*/
public function setRoutePatternPrefix($routePatternPrefix)
{
$this->routePatternPrefix = $routePatternPrefix;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRoutePatternPrefix()
{
return $this->routePatternPrefix;
}
/**
* Sets the route name and pattern prefixes.
*
* @param string $routeNamePrefix The route name prefix.
* @param string $routePatternPrefix The route pattern prefix.
*
* @return ModuleInterface The module (fluent interface).
*/
public function setRoutePrefixes($routeNamePrefix, $routePatternPrefix)
{
$this->setRouteNamePrefix($routeNamePrefix);
$this->setRoutePatternPrefix($routePatternPrefix);
return $this;
}
/**
* Adds a parameter to propagate.
*
* @param string $parameter The parameter.
*
* @return ModuleInterface The module (fluent interface).
*/
public function addParameterToPropagate($parameter)
{
$this->parametersToPropagate[] = $parameter;
return $this;
}
/**
* Adds parameters to propagate.
*
* @param array $parameters The parameters.
*
* @return ModuleInterface The module (fluent interface).
*/
public function addParametersToPropagate(array $parameters)
{
foreach ($parameters as $parameter) {
$this->addParameterToPropagate($parameter);
}
return $this;
}
/**
* Sets the parameters to propagate.
*
* @param array $parameters The parameters.
*
* @return ModuleInterface The module (fluent interface).
*/
public function setParametersToPropagate(array $parameters)
{
$this->parametersToPropagate = array();
$this->addParametersToPropagate($parameters);
return $this;
}
/**
* {@inheritdoc}
*/
public function getParametersToPropagate()
{
return $this->parametersToPropagate;
}
/**
* Adds an option.
*
* @param string $name The name.
* @param mixed $defaultValue The default value.
*
* @return ModuleInterface The module (fluent interface).
*
* @throws \LogicException If the option already exists.
*/
public function addOption($name, $defaultValue)
{
if ($this->hasOption($name)) {
throw new \LogicException(sprintf('The option "%s" already exists.', $name));
}
$this->options[$name] = $defaultValue;
return $this;
}
/**
* Adds options.
*
* @param array $options The options as an array (the name as the key and the default value as the value).
*
* @return ModuleInterface The module (fluent interface).
*/
public function addOptions(array $options)
{
foreach ($options as $name => $defaultValue) {
$this->addOption($name, $defaultValue);
}
return $this;
}
/**
* Sets an option.
*
* @param string $name The name.
* @param mixed $value The value.
*
* @return ModuleInterface The module (fluent interface).
*
* @throws \InvalidArgumentException If the option does not exist.
*/
public function setOption($name, $value)
{
if (!$this->hasOption($name)) {
throw new \InvalidArgumentException(sprintf('The option "%s" does not exist.', $name));
}
$this->options[$name] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function hasOption($name)
{
return array_key_exists($name, $this->options);
}
/**
* {@inheritdoc}
*/
public function getOption($name)
{
if (!$this->hasOption($name)) {
throw new \InvalidArgumentException(sprintf('The option "%s" does not exist.', $name));
}
return $this->options[$name];
}
/**
* {@inheritdoc}
*/
public function getOptions()
{
return $this->options;
}
/**
* Adds an action.
*
* @param ActionInterface $action An action.
*
* @return ModuleInterface The module (fluent interface).
*
* @throws \InvalidArgumentException If the action already exists.
*/
public function addAction(ActionInterface $action)
{
if (isset($this->actions[$action->getName()])) {
throw new \InvalidArgumentException(sprintf('The action "%s" already exists.', $action->getName()));
}
$action->setModule($this);
$this->actions[$action->getName()] = $action;
return $this;
}
/**
* Adds actions.
*
* @param array $actions An array of actions.
*
* @return ModuleInterface The module (fluent interface).
*/
public function addActions(array $actions)
{
foreach ($actions as $action) {
$this->addAction($action);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function hasAction($name)
{
return isset($this->actions[$name]);
}
/**
* {@inheritdoc}
*/
public function getAction($name)
{
if (!isset($this->actions[$name])) {
throw new \InvalidArgumentException(sprintf('The action "%s" does not exist.', $name));
}
return $this->actions[$name];
}
/**
* {@inheritdoc}
*/
public function deleteAction($name)
{
if (!isset($this->actions[$name])) {
throw new \InvalidArgumentException(sprintf('The action "%s" does not exist.', $name));
}
unset($this->actions[$name]);
}
/**
* {@inheritdoc}
*/
public function getActions()
{
return $this->actions;
}
/**
* {@inheritdoc}
*/
public function getRouteActions()
{
$routeActions = array();
foreach ($this->actions as $name => $action) {
if ($action instanceof RouteActionInterface) {
$routeActions[$name] = $action;
}
}
return $routeActions;
}
/**
* Shortcut for setting options to actions.
*
* @param string $actionName The action name.
* @param string $optionName The option name.
* @param mixed $optionValue The option value.
*
* @return ModuleInterface The module (fluent interface).
*/
public function setActionOption($actionName, $optionName, $optionValue)
{
$this->getAction($actionName)->setOption($optionName, $optionValue);
return $this;
}
/**
* Adds a controller pre execute.
*
* @param mixed $controllerPreExecute A callable.
*
* @return ModuleInterface The module (fluent interface).
*/
public function addControllerPreExecute($controllerPreExecute)
{
if (!is_callable($controllerPreExecute)) {
throw new \InvalidArgumentException('The controller pre execute is not callable.');
}
$this->controllerPreExecutes[] = $controllerPreExecute;
return $this;
}
/**
* {@inheritdoc}
*/
public function getControllerPreExecutes()
{
return $this->controllerPreExecutes;
}
/**
* {@inheritdoc}
*/
public function generateModuleUrl($actionName, array $parameters = array(), $absolute = false)
{
$routeName = $this->getRouteNamePrefix().$this->getAction($actionName)->getRouteName();
if ($this->parametersToPropagate) {
$request = $this->container->get('request');
foreach ($this->parametersToPropagate as $parameter) {
if (!isset($parameters[$parameter]) && $value = $request->get($parameter)) {
$parameters[$parameter] = $value;
}
}
}
return $this->container->get('router')->generate($routeName, $parameters, $absolute);
}
/**
* {@inheritdoc}
*/
public function forward($actionName, array $attributes = array(), array $query = array())
{
$controller = 'PablodipModuleBundle:Module:execute';
$attributes['_module.module'] = get_class($this);
$attributes['_module.action'] = $actionName;
return $this->container->get('http_kernel')->forward($controller, $attributes, $query);
}
/**
* {@inheritdoc}
*/
public function createView()
{
return new ModuleView($this);
}
}