-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathController.php
771 lines (667 loc) · 21.5 KB
/
Controller.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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
<?php
/*
* This file has its roots as part of the Mojavi package which was
* Copyright (c) 2003 Sean Kerr. It has been incorporated into this
* derivative work under the terms of the LGPL V2.1.
* (http://www.gnu.org/licenses/lgpl-2.1.html)
*/
namespace Xmf\Xadr;
use Xmf\Xadr\Exceptions\InvalidConfigurationException;
/**
* The Controller dispatches requests to the proper action and controls
* application flow.
*
* @category Xmf\Xadr\Controller
* @package Xmf
* @author Richard Griffith <[email protected]>
* @author Sean Kerr <[email protected]>
* @copyright 2013-2015 XOOPS Project (http://xoops.org)
* @copyright 2003 Sean Kerr
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @link http://xoops.org
*/
class Controller
{
/**
* @var AuthorizationHandler instance
*/
protected $authorizationHandler;
/**
* @var Config a Config instance
*/
protected $config;
/**
* @var string currently processing action.
*/
protected $currentAction;
/**
* @var string currently processing unit.
*/
protected $currentUnit;
/**
* @var ExecutionChain instance
*/
protected $execChain;
/**
* @var DomainManager instance
*/
protected $domainManager;
/**
* @var array an associative array of template-ready data.
*/
protected $mojavi;
/**
* @var string PHP Namespace of Xadr application
*/
protected $nameSpace;
/**
* Determines how a Responder should be rendered.
*
* Possible render modes:
* - Xadr::RENDER_CLIENT - render to the client
* - Xadr::RENDER_VARIABLE - render to variable
*
* @var integer
*/
protected $renderMode;
/**
* @var Request instance
*/
protected $request;
/**
* @var Response instance
*/
protected $response;
/**
* @var string originally requested action
*/
protected $requestAction;
/**
* @var string originally requested unit
*/
protected $requestUnit;
/**
* @var User instance
*/
protected $user;
/**
* Create a new Controller instance.
*
* _This should never be called manually._
* Use static getNew() method.
*
* @param Request|null $request Request object, or null for default request
* @param Response|null $response Response object, or null for default response
*/
protected function __construct($request = null, $response = null)
{
$this->currentAction = null;
$this->currentUnit = null;
$this->execChain = new ExecutionChain;
$this->renderMode = Xadr::RENDER_CLIENT;
$this->requestAction = null;
$this->requestUnit = null;
// init Controller objects
$this->authorizationHandler = null;
$this->request = is_object($request) ? $request : new Request();
$this->response = is_object($response) ? $response : new Response();
$this->mojavi = array();
$this->user = null;
$this->domainManager = new DomainManager($this);
$this->config = new Config;
}
/**
* Retrieve an new instance of the Controller.
*
* @param Request|null $request Request object, or null for default request
* @param Response|null $response Response object, or null for default response
*
* @return Controller A Controller instance.
*/
public static function getNew($request = null, $response = null)
{
$controllerClass = get_called_class(); // not available PHP<5.3
$instance = new $controllerClass($request, $response);
return $instance;
}
/**
* getComponentName - build class name of action, responder, etc.
*
* @param string $compType type (action, responder, etc.)
* @param string $unitName Unit name
* @param string $actionName Action name
* @param string $actResponse Responder suffix (success, error, input, etc.)
*
* @return string|null file name or null on error
*/
protected function getComponentName($compType, $unitName, $actionName, $actResponse)
{
$actResponse = ucfirst(strtolower($actResponse));
$cTypes=array(
'action' => array('dir'=>'Actions', 'suffix'=>'Action'),
'filter' => array('dir'=>'Filters', 'suffix'=>'Filter'),
'filterlist' => array('dir'=>'Filters', 'suffix'=>''),
'responder' => array('dir'=>'Responders', 'suffix'=>"Responder{$actResponse}"),
'domain' => array('dir'=>'Domain', 'suffix'=>''),
);
$class=null;
if (isset($cTypes[$compType])) {
$c=$cTypes[$compType];
$class = $this->nameSpace . '\\' . $unitName . '\\' . $c['dir'] . '\\' . $actionName . $c['suffix'];
}
return $class;
}
/**
* Set a variable if it is currently empty
*
* @param string|null $variable variable to set
* @param string|null $value value
*
* @return void
*/
protected function setIfEmpty(&$variable, $value)
{
if (empty($variable)) {
$variable = $value;
}
}
/**
* Normalize unit and action
*
* @param string|null $unitName A unit name.
* @param string|null $actionName An action name.
*
* @return void ($unitName and $actionName are references)
*/
protected function normalizeUnitAction(&$unitName, &$actionName)
{
$unitParameter = $this->config->get('UNIT_ACCESSOR', 'unit');
$actionParameter = $this->config->get('ACTION_ACCESSOR', 'action');
$unitDefault = $this->config->get('DEFAULT_UNIT', 'App');
$actionDefault = $this->config->get('DEFAULT_ACTION', 'Index');
// use default unit and action only if both have not been specified
if (empty($unitName) && empty($actionName)
&& !$this->request->hasParameter($unitParameter)
&& !$this->request->hasParameter($actionParameter)
) {
$unitName = $unitDefault;
$actionName = $actionDefault;
} else {
// has a unit been specified via dispatch()?
$this->setIfEmpty($unitName, $this->request->getParameter($unitParameter));
$this->setIfEmpty($unitName, $unitDefault);
// has an action been specified via dispatch()?
$this->setIfEmpty($actionName, $this->request->getParameter($actionParameter));
$this->setIfEmpty($actionName, $actionDefault);
}
}
/**
* Dispatch a request.
*
* _Optional parameters for unit and action exist if you wish to
* use a page controller pattern._
*
* @param string|null $unitName A unit name.
* @param string|null $actionName An action name.
*
* @return void
*/
public function dispatch($unitName = null, $actionName = null)
{
if ($this->user === null) {
// no user type has been set, use the default no privilege user
$this->user = new User($this);
}
// unit and action are by reference
$this->normalizeUnitAction($unitName, $actionName);
// set request unit and action
$this->requestAction = $actionName;
$this->requestUnit = $unitName;
$this->mojavi['request_action'] = $actionName;
$this->mojavi['request_unit'] = $unitName;
// paths
$this->mojavi['controller_path'] = $this->getControllerPath();
$this->mojavi['current_action_path']
= $this->getControllerPath($unitName, $actionName);
$this->mojavi['current_unit_path'] = $this->getControllerPath($unitName);
$this->mojavi['request_action_path']
= $this->getControllerPath($unitName, $actionName);
$this->mojavi['request_unit_path'] = $this->getControllerPath($unitName);
// process the originally requested action
$this->forward($unitName, $actionName);
// shutdown DomainManager
$this->domainManager->shutdown();
}
/**
* Forward the request to an action.
*
* @param string $unitName A unit name.
* @param string $actionName An action name.
*
* @return void
*
* @throws InvalidConfigurationException
*/
public function forward($unitName, $actionName)
{
$action = $this->getAction($unitName, $actionName);
// track old unit/action
$oldAction = $this->currentAction;
$oldUnit = $this->currentUnit;
// add unit and action to execution chain, and update current vars
$this->execChain->addRequest($unitName, $actionName, $action);
$this->updateCurrentVars($unitName, $actionName);
if ($action === null) {
// requested action doesn't exist, get configured recovery action
$actionName = $this->config->get('ERROR_404_ACTION', 'PageNotFound');
$unitName = $this->config->get('ERROR_404_UNIT', 'App');
// add another request since the action is non-existent
$action = $this->getAction($unitName, $actionName);
if ($action === null) {
// cannot find load error 404 unit/action
$error = 'Invalid recovery action for not found: ' .
$this->nameSpace . ' - ' .
'ERROR_404_UNIT (' . $unitName . '), ' .
'ERROR_404_ACTION (' . $actionName . ')';
throw new InvalidConfigurationException($error);
}
$this->execChain->addRequest($unitName, $actionName, $action);
$this->updateCurrentVars($unitName, $actionName);
}
$filterChain = new FilterChain;
// map filters
$this->mapGlobalFilters($filterChain);
$this->mapUnitFilters($filterChain, $unitName);
// and last but not least, the main execution filter
$filterChain->register(new ExecutionFilter($this));
// execute filters
$filterChain->execute();
// update current vars
$this->updateCurrentVars($oldUnit, $oldAction);
}
/**
* Retrieve an action implementation instance.
*
* @param string $unitName A unit name.
* @param string $actionName An action name.
*
* @return Action|null An Action instance, if the action exists, otherwise null
*/
public function getAction($unitName, $actionName)
{
$classname = $this->getComponentName('action', $unitName, $actionName, '');
return $this->newContextAwareObject($classname, '\Xmf\Xadr\Action');
}
/**
* Retrieve the developer supplied authorization handler.
*
* @return AuthorizationHandler An AuthorizationHandler instance, if an
* authorization handler has been set,
* otherwise NULL.
*/
public function getAuthorizationHandler()
{
return $this->authorizationHandler;
}
/**
* Retrieve the Config object
*
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* Retrieve an absolute web path to the public controller file.
*
* @param string|null $unitName A unit name.
* @param string|null $actionName An action name.
*
* @return string An absolute web path representing the controller file,
* which also includes unit and action names.
*/
public function getControllerPath($unitName = null, $actionName = null)
{
$path = $this->config->get('SCRIPT_PATH');
$varsep = '?';
if (!(empty($unitName)
|| $unitName==$this->config->get('DEFAULT_UNIT', 'App'))
) {
$path .= $varsep.$this->config->get('UNIT_ACCESSOR', 'unit')."=$unitName";
$varsep = '&';
}
if (!empty($actionName)) {
$path .= $varsep.$this->config->get('ACTION_ACCESSOR', 'action')."=$actionName";
}
return $path;
}
/**
* Generate a URL for a given unit, action and parameters
*
* @param string $unitName a unit name
* @param string $actionName an action name
* @param array $params an associative array of additional URL parameters
*
* @return string A URL to a Xadr resource.
*/
public function getControllerPathWithParams($unitName, $actionName, $params)
{
$url=$this->getControllerPath($unitName, $actionName);
$divider = (strpos($url, '?')===false) ? '?' : '&';
foreach ($params as $k => $v) {
$url .= $divider . urlencode($k) . '=' . urlencode($v);
$divider = '&'; // from here on we append
}
return $url;
}
/**
* Generate a formatted Xadr URL.
*
* @param array $params An associative array of URL parameters.
*
* @return string A URL
*/
public function genURL($params)
{
$url = $this->config->get('SCRIPT_PATH');
$divider = '&';
$equals = '=';
$url .= '?';
$separator = '';
foreach ($params as $key => $value) {
$url .= $separator . urlencode($key) . $equals . urlencode($value);
$separator = $divider;
}
return $url;
}
/**
* Retrieve the name of the currently processing action.
*
* If the request has not been forwarded, this will return the
* originally requested action.
*
* @return string
*/
public function getCurrentAction()
{
return $this->currentAction;
}
/**
* Retrieve the name of the currently processing unit.
*
* If the request has not been forwarded, this will return the
* originally requested unit
*
* @return string current unit
*/
public function getCurrentUnit()
{
return $this->currentUnit;
}
/**
* Retrieve the execution chain.
*
* @return ExecutionChain An ExecutionChain instance.
*/
public function getExecutionChain()
{
return $this->execChain;
}
/**
* Retrieve the Mojavi associative array.
*
* @return array An associative array of template-ready data.
*/
public function & getMojavi()
{
return $this->mojavi;
}
/**
* Retrieve the global render mode.
*
* @return int One of two possible render modes:
* - Xadr::RENDER_CLIENT - render to the client
* - Xadr::RENDER_VARIABLE - render to variable
*/
public function getRenderMode()
{
return $this->renderMode;
}
/**
* Retrieve the request instance.
*
* @return Request A Request instance.
*/
public function getRequest()
{
return $this->request;
}
/**
* Retrieve the response instance.
*
* @return Response A Response instance.
*/
public function getResponse()
{
return $this->response;
}
/**
* Retrieve the name of the originally requested action.
*
* @return string An action name.
*/
public function getRequestAction()
{
return $this->requestAction;
}
/**
* Retrieve the name of the originally requested unit.
*
* @return string A unit name.
*/
public function getRequestUnit()
{
return $this->requestUnit;
}
/**
* Retrieve the currently requesting user.
*
* @return User a User instance.
*/
public function getUser()
{
return $this->user;
}
/**
* Retrieve a Responder implementation instance.
*
* @param ResponseSelector $responseSelected object describing the appropriate responder.
*
* @return Responder|null Responder instance, or null if responder does not exist
*/
public function getResponder($responseSelected)
{
$classname = $this->getComponentName(
'responder',
$responseSelected->getResponseUnit(),
$responseSelected->getResponseAction(),
$responseSelected->getResponseCode()
);
return $this->newContextAwareObject($classname, '\Xmf\Xadr\Responder');
}
/**
* Map a filter
*
* @param FilterChain $filterChain A FilterChain instance.
* @param string $className Class name of a FilterList
*
* @return void
*/
protected function mapFilter(FilterChain $filterChain, $className)
{
/* @var FilterList[] */
static $cache = array();
if (!isset($cache[$className])) {
$cache[$className] = null;
if (class_exists($className)) {
/* @var FilterList */
$object = new $className($this);
if ($object instanceof FilterList) {
$cache[$className] = $object;
$object->registerFilters($filterChain);
}
}
} else {
if ($cache[$className]) {
$cache[$className]->registerFilters($filterChain);
}
}
}
/**
* Map global filters.
*
* @param FilterChain $filterChain A FilterChain instance.
*
* @return void
*/
public function mapGlobalFilters($filterChain)
{
$className = $this->nameSpace . "\\GlobalFilterList";
$this->mapFilter($filterChain, $className);
}
/**
* Map all filters for a given unit.
*
* @param FilterChain $filterChain A FilterChain instance.
* @param string $unitName A unit name.
*
* @return void
*/
public function mapUnitFilters($filterChain, $unitName)
{
$listName = $unitName . 'FilterList';
$className = $this->getComponentName(
'filterlist',
$unitName,
$listName,
''
);
$this->mapFilter($filterChain, $className);
}
/**
* Set the developer supplied authorization handler.
*
* @param Authorizationhandler $handler An AuthorizationHandler instance.
*
* @return void
*/
public function setAuthorizationHandler($handler)
{
$this->authorizationHandler = $handler;
}
/**
* Set the global render mode.
*
* @param int $mode Global render mode, which is one of the following two:
* - Xadr::RENDER_CLIENT - render to the client
* - Xadr::RENDER_VARIABLE - render to variable
*
* @return void
*/
public function setRenderMode($mode)
{
$this->renderMode = $mode;
}
/**
* Set the user type.
*
* @param User $user A User instance.
*
* @return void
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Update current unit and action data.
*
* @param string $unitName A unit name.
* @param string $actionName An action name.
*
* @return void
*/
protected function updateCurrentVars($unitName, $actionName)
{
// alias objects for easy access
$mojavi =& $this->mojavi;
$this->currentUnit = $unitName;
$this->currentAction = $actionName;
// names
$mojavi['current_action'] = $actionName;
$mojavi['current_unit'] = $unitName;
// paths
$mojavi['current_action_path']
= $this->getControllerPath($unitName, $actionName);
$mojavi['current_unit_path'] = $this->getControllerPath($unitName);
}
/**
* Retrieve a filter implementation instance.
*
* @param string $name - A filter name.
* @param string $unitName - A unit name, defaults to current unit
*
* @return Filter|null instance, or null if it could not be instantiated or was not a Filter
*/
public function getFilter($name, $unitName = '')
{
if (empty($unitName)) {
$unitName = $this->currentUnit;
}
$classname = $this->getComponentName('filter', $unitName, $name, '');
return $this->newContextAwareObject($classname, '\Xmf\Xadr\Filter');
}
/**
* Retrieve the DomainManager instance.
*
* @return DomainManager the domain manager object
*/
public function getDomainManager()
{
return $this->domainManager;
}
/**
* Retrieve a domain implementation instance.
*
* @param string $name - A domain name.
* @param string $unitName - A unit name
*
* @return object|null
*/
public function getDomainComponent($name, $unitName)
{
$classname = $this->getComponentName('domain', $unitName, $name, '');
return $this->newContextAwareObject($classname, '\Xmf\Xadr\Domain');
}
/**
* Given a class name and a base class, try to instantiate the classname, and
* verify that it is an instance of baseClass. The specified classname is expected
* to extend ContextAware.
*
* @param string $classname fully qualified class name to instantiate
* @param string $baseClass fully qualified class or interface to verify as instanceof
*
* @return object|null an instantiated $classname object, or null if it does not exist
* or if it is not an instance of $baseClass
*/
protected function newContextAwareObject($classname, $baseClass)
{
$object = null;
if (class_exists($classname)) {
$object = new $classname($this);
$object = ($object instanceof $baseClass) ? $object : null;
}
return $object;
}
}