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