1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PPI Framework. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2011-2016 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 Zend\ModuleManager\ModuleManagerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ControllerNameParser converts controller from the short notation a:b:c |
17
|
|
|
* (BlogModule:Post:index) to a fully-qualified class::method string |
18
|
|
|
* (Module\BlogModule\Controller\PostController::indexAction). |
19
|
|
|
* |
20
|
|
|
* @author Fabien Potencier <[email protected]> |
21
|
|
|
* @author Vítor Brandão <[email protected]> |
22
|
|
|
* @author Paul Dragoonis <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ControllerNameParser |
25
|
|
|
{ |
26
|
|
|
protected $moduleManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param ModuleManagerInterface $moduleManager A ModuleManagerInterface instance |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ModuleManagerInterface $moduleManager) |
34
|
|
|
{ |
35
|
|
|
$this->moduleManager = $moduleManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Converts a short notation a:b:c to a class::method. |
40
|
|
|
* |
41
|
|
|
* @param string $controller A short notation controller (a:b:c) |
42
|
|
|
* |
43
|
|
|
* @throws \InvalidArgumentException when the specified module is not enabled |
44
|
|
|
* or the controller cannot be found |
45
|
|
|
* |
46
|
|
|
* @return string A string with class::method |
47
|
|
|
*/ |
48
|
|
|
public function parse($controller) |
49
|
|
|
{ |
50
|
|
|
list($module, $moduleName, $controller, $action) = $this->getPartsFromControllerName($controller); |
51
|
|
|
|
52
|
|
|
if (null === $module) { |
53
|
|
|
// this throws an exception if there is no such module |
54
|
|
|
$msg = sprintf('Unable to find controller "%s:%s" - module alias "%s" does not exist.', $moduleName, $controller, $moduleAlias); |
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
$class = $module->getNamespace() . '\\Controller\\' . $controller; |
57
|
|
|
if (class_exists($class)) { |
58
|
|
|
return $class . '::' . $action . 'Action'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$msg = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $moduleName, $controller, $class); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new \InvalidArgumentException($msg); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function build($controller) |
68
|
|
|
{ |
69
|
|
|
list($module, $moduleName, $controllerName, $actionName) = $this->getPartsFromControllerName($controller); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return sprintf('%s:%s:%s', $moduleName, $controllerName, $actionName); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $controller |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function getPartsFromControllerName($controller) |
80
|
|
|
{ |
81
|
|
|
if (3 != count($parts = explode(':', $controller))) { |
82
|
|
|
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
list($moduleName, $controller, $action) = $parts; |
86
|
|
|
$controller = str_replace('/', '\\', $controller); |
87
|
|
|
$module = $this->moduleManager->getModule($moduleName); |
|
|
|
|
88
|
|
|
|
89
|
|
|
if (!is_object($module)) { |
90
|
|
|
throw new \RuntimeException(sprintf( |
91
|
|
|
'Unable to locate module: %s, when parsing controller: %s', |
92
|
|
|
$moduleName, $controller |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$moduleName = $module->getName(); |
97
|
|
|
|
98
|
|
|
return [$module, $moduleName, $controller, $action]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.