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