1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Mvc; |
12
|
|
|
|
13
|
|
|
use Zemit\Bootstrap\Config; |
14
|
|
|
use Zemit\Mvc\Router\ModuleRoute; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Router |
18
|
|
|
* {@inheritDoc} |
19
|
|
|
* |
20
|
|
|
* @author Julien Turbide <[email protected]> |
21
|
|
|
* @copyright Zemit Team <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* @version 1.0 |
25
|
|
|
* |
26
|
|
|
* @package Zemit\Mvc |
27
|
|
|
*/ |
28
|
|
|
class Router extends \Phalcon\Mvc\Router |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Config |
32
|
|
|
*/ |
33
|
|
|
public $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Router constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct($defaultRoutes = true, $application = null) |
39
|
|
|
{ |
40
|
|
|
parent::__construct(false); |
41
|
|
|
if (isset($application)) { |
42
|
|
|
$this->config = $application->getDI()->get('config'); |
43
|
|
|
} |
44
|
|
|
if ($defaultRoutes) { |
45
|
|
|
$this->defaultRoutes(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Default routes |
51
|
|
|
* - Default namespace |
52
|
|
|
* - Default controller |
53
|
|
|
* - Default action |
54
|
|
|
* - Default notFound |
55
|
|
|
*/ |
56
|
|
|
public function defaultRoutes() |
57
|
|
|
{ |
58
|
|
|
$this->removeExtraSlashes(true); |
59
|
|
|
$this->setDefaults($this->config->router->defaults->toArray() ?: $this->defaults); |
|
|
|
|
60
|
|
|
$this->notFound($this->config->router->notFound->toArray() ?: $this->notFound); |
61
|
|
|
$this->mount(new ModuleRoute($this->getDefaults(), true)); |
62
|
|
|
$this->mount(new ModuleRoute($this->getDefaults(), true, true)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array|null $hostnames |
67
|
|
|
* @param array|null $defaults |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function hostnamesRoutes(array $hostnames = null, array $defaults = null) |
71
|
|
|
{ |
72
|
|
|
$defaults ??= $this->getDefaults(); |
73
|
|
|
$hostnames ??= $this->config->router->hostnames->toArray() ?: []; |
|
|
|
|
74
|
|
|
foreach ($hostnames as $hostname => $hostnameRoute) { |
75
|
|
|
if (!isset($hostnameRoute['module']) || !is_string($hostnameRoute['module'])) { |
76
|
|
|
throw new \InvalidArgumentException('Router hostname config parameter "module" must be a string under "' . $hostname . '"'); |
77
|
|
|
} |
78
|
|
|
$this->mount((new ModuleRoute(array_merge($defaults, $hostnameRoute), true))->setHostname($hostname)); |
79
|
|
|
$this->mount((new ModuleRoute(array_merge($defaults, $hostnameRoute), true, true))->setHostname($hostname)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Defines our frontend routes |
85
|
|
|
* /controller/action/params |
86
|
|
|
*/ |
87
|
|
|
public function modulesRoutes($application) |
88
|
|
|
{ |
89
|
|
|
$defaults = $this->getDefaults(); |
90
|
|
|
foreach ($application->getModules() as $key => $module) { |
91
|
|
|
if (!isset($module['className'])) { |
92
|
|
|
throw new \InvalidArgumentException('Module parameter "className" must be a string under "' . $key . '"'); |
93
|
|
|
} |
94
|
|
|
$namespace = rtrim($module['className'], 'Module') . 'Controllers'; |
95
|
|
|
$this->mount(new ModuleRoute(array_merge($defaults, ['namespace' => $namespace, 'module' => $key]))); |
96
|
|
|
$this->mount(new ModuleRoute(array_merge($defaults, ['namespace' => $namespace, 'module' => $key]), false, true)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Router toArray |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function toArray() |
105
|
|
|
{ |
106
|
|
|
$mathedRoute = $this->getMatchedRoute(); |
107
|
|
|
return [ |
108
|
|
|
'namespace' => $this->getNamespaceName(), |
109
|
|
|
'module' => $this->getModuleName(), |
110
|
|
|
'controller' => $this->getControllerName(), |
111
|
|
|
'action' => $this->getActionName(), |
112
|
|
|
'params' => $this->getParams(), |
113
|
|
|
'defaults' => $this->getDefaults(), |
114
|
|
|
'matches' => $this->getMatches(), |
115
|
|
|
'matched' => $mathedRoute ? [ |
|
|
|
|
116
|
|
|
'id' => $mathedRoute->getRouteId(), |
117
|
|
|
'name' => $mathedRoute->getName(), |
118
|
|
|
'hostname' => $mathedRoute->getHostname(), |
119
|
|
|
'paths' => $mathedRoute->getPaths(), |
120
|
|
|
'pattern' => $mathedRoute->getPattern(), |
121
|
|
|
'httpMethod' => $mathedRoute->getHttpMethods(), |
122
|
|
|
'reversedPaths' => $mathedRoute->getReversedPaths(), |
123
|
|
|
] : null, |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|