1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PPI Framework. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2011-2013 Paul Dragoonis <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* |
8
|
|
|
* @link http://www.ppi.io |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PPI\Framework\Module\Controller; |
12
|
|
|
|
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver; |
15
|
|
|
use Zend\ServiceManager\ServiceLocatorAwareInterface; |
16
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ControllerResolver. |
20
|
|
|
* |
21
|
|
|
* @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver |
22
|
|
|
* |
23
|
|
|
* @author Fabien Potencier <[email protected]> |
24
|
|
|
* @author Vítor Brandão <[email protected]> |
25
|
|
|
* @author Paul Dragoonis <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ControllerResolver extends BaseControllerResolver |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param ServiceLocatorInterface $serviceManager A ServiceLocatorInterface instance |
36
|
|
|
* @param ControllerNameParser $parser A ControllerNameParser instance |
37
|
|
|
* @param LoggerInterface $logger A LoggerInterface instance |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ServiceLocatorInterface $serviceManager, ControllerNameParser $parser, LoggerInterface $logger = null) |
40
|
|
|
{ |
41
|
|
|
$this->serviceManager = $serviceManager; |
|
|
|
|
42
|
|
|
$this->parser = $parser; |
|
|
|
|
43
|
|
|
parent::__construct($logger); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns a callable for the given controller. |
48
|
|
|
* |
49
|
|
|
* @param string $controller A Controller string |
50
|
|
|
* |
51
|
|
|
* @throws \LogicException When the name could not be parsed |
52
|
|
|
* @throws \InvalidArgumentException When the controller class does not exist |
53
|
|
|
* |
54
|
|
|
* @return mixed A PHP callable |
55
|
|
|
*/ |
56
|
|
|
protected function createController($controller) |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
if (false === strpos($controller, '::')) { |
60
|
|
|
$count = substr_count($controller, ':'); |
61
|
|
|
if (2 == $count) { |
62
|
|
|
// controller in the a:b:c notation then |
63
|
|
|
$controller = $this->parser->parse($controller); |
64
|
|
|
} elseif (1 == $count) { |
65
|
|
|
// controller in the service:method notation |
66
|
|
|
list($service, $method) = explode(':', $controller, 2); |
67
|
|
|
|
68
|
|
|
return array($this->container->get($service), $method); |
|
|
|
|
69
|
|
|
} elseif ($this->container->has($controller) && method_exists($service = $this->container->get($controller), '__invoke')) { |
70
|
|
|
return $service; |
71
|
|
|
} else { |
72
|
|
|
throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return parent::createController($controller); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $class |
81
|
|
|
* @return object |
82
|
|
|
*/ |
83
|
|
|
protected function instantiateController($class) |
84
|
|
|
{ |
85
|
|
|
$controller = parent::instantiateController($class); |
86
|
|
|
|
87
|
|
|
if ($controller instanceof ServiceLocatorAwareInterface) { |
88
|
|
|
$controller->setServiceLocator($this->serviceManager); |
89
|
|
|
} |
90
|
|
|
return $controller; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: