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\Router; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Router\Group as RouterGroup; |
15
|
|
|
use Zemit\Utils\Slug; |
16
|
|
|
|
17
|
|
|
class ModuleRoute extends RouterGroup |
18
|
|
|
{ |
19
|
|
|
public array $locale; |
20
|
|
|
|
21
|
14 |
|
public function __construct($paths = null, array $locale = [], ?string $hostname = null) |
22
|
|
|
{ |
23
|
14 |
|
$this->locale = $locale; |
24
|
14 |
|
if (isset($hostname)) { |
25
|
|
|
$this->setHostname($hostname); |
26
|
|
|
} |
27
|
14 |
|
parent::__construct($paths); |
28
|
|
|
} |
29
|
|
|
|
30
|
14 |
|
public function initialize(): void |
31
|
|
|
{ |
32
|
14 |
|
$hostname = $this->getHostname(); |
33
|
14 |
|
$path = $this->getPaths(); |
34
|
14 |
|
$module = $path['module']; |
35
|
|
|
|
36
|
14 |
|
$mainRoutePrefix = $hostname ? '' : '/' . $module; |
37
|
14 |
|
$mainNamePrefix = $hostname ? Slug::generate($hostname) : $module; |
38
|
|
|
|
39
|
14 |
|
$routePrefix = $mainRoutePrefix; |
40
|
14 |
|
$namePrefix = $mainNamePrefix; |
41
|
|
|
|
42
|
14 |
|
$this->add($routePrefix ?: '/', [ |
43
|
14 |
|
])->setName($namePrefix); |
44
|
|
|
|
45
|
14 |
|
$this->add($routePrefix . '/:controller[/]{0,1}', [ |
46
|
14 |
|
'controller' => 1, |
47
|
14 |
|
])->setName($namePrefix . '-controller'); |
48
|
|
|
|
49
|
14 |
|
$this->add($routePrefix . '/:controller/:action/:params', [ |
50
|
14 |
|
'controller' => 1, |
51
|
14 |
|
'action' => 2, |
52
|
14 |
|
'params' => 3, |
53
|
14 |
|
])->setName($namePrefix . '-controller-action'); |
54
|
|
|
|
55
|
14 |
|
if (!empty($this->locale)) { |
56
|
14 |
|
$localeRegex = '{locale:(' . implode('|', array_unique($this->locale)) . ')}'; |
57
|
|
|
|
58
|
14 |
|
$routePrefix = '/' . $localeRegex . $mainRoutePrefix; |
59
|
14 |
|
$namePrefix = 'locale-' . $mainNamePrefix; |
60
|
|
|
|
61
|
14 |
|
$this->add($routePrefix . '[/]{0,1}', [ |
62
|
14 |
|
'locale' => 1, |
63
|
14 |
|
])->setName($namePrefix); |
64
|
|
|
|
65
|
14 |
|
$this->add($routePrefix . '/:controller[/]{0,1}', [ |
66
|
14 |
|
'locale' => 1, |
67
|
14 |
|
'controller' => 2, |
68
|
14 |
|
])->setName($namePrefix . '-controller'); |
69
|
|
|
|
70
|
14 |
|
$this->add($routePrefix . '/:controller/:action/:params', [ |
71
|
14 |
|
'locale' => 1, |
72
|
14 |
|
'controller' => 2, |
73
|
14 |
|
'action' => 3, |
74
|
14 |
|
'params' => 4, |
75
|
14 |
|
])->setName($namePrefix . '-controller-action'); |
76
|
|
|
|
77
|
14 |
|
foreach ($this->locale as $locale) { |
78
|
14 |
|
$routePrefix = '/' . $locale . $mainRoutePrefix; |
79
|
14 |
|
$namePrefix = $locale . '-' . $mainNamePrefix; |
80
|
|
|
|
81
|
14 |
|
$this->add($routePrefix . '[/]{0,1}', [ |
82
|
14 |
|
'locale' => $locale, |
83
|
14 |
|
])->setName($namePrefix); |
84
|
|
|
|
85
|
14 |
|
$this->add($routePrefix . '/:controller[/]{0,1}', [ |
86
|
14 |
|
'locale' => $locale, |
87
|
14 |
|
'controller' => 1, |
88
|
14 |
|
])->setName($namePrefix . '-controller'); |
89
|
|
|
|
90
|
14 |
|
$this->add($routePrefix . '/:controller/:action/:params', [ |
91
|
14 |
|
'locale' => $locale, |
92
|
14 |
|
'controller' => 1, |
93
|
14 |
|
'action' => 2, |
94
|
14 |
|
'params' => 3, |
95
|
14 |
|
])->setName($namePrefix . '-controller-action'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|