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
|
14 |
|
$routePrefix = $hostname ? '' : '/' . $module; |
36
|
14 |
|
$namePrefix = $hostname ? Slug::generate($hostname) : $module; |
37
|
|
|
|
38
|
14 |
|
$this->add($routePrefix ?: '/', [ |
39
|
14 |
|
])->setName($namePrefix); |
40
|
|
|
|
41
|
14 |
|
$this->add($routePrefix . '/:controller', [ |
42
|
14 |
|
'controller' => 1, |
43
|
14 |
|
])->setName($namePrefix . '-controller'); |
44
|
|
|
|
45
|
14 |
|
$this->add($routePrefix . '/:controller/:action/:params', [ |
46
|
14 |
|
'controller' => 1, |
47
|
14 |
|
'action' => 2, |
48
|
14 |
|
'params' => 3, |
49
|
14 |
|
])->setName($namePrefix . '-controller-action'); |
50
|
|
|
|
51
|
14 |
|
if (!empty($this->locale)) { |
52
|
14 |
|
$localeRegex = '{locale:(' . implode('|', $this->locale) . ')}'; |
53
|
|
|
|
54
|
14 |
|
$routePrefix = '/' . $localeRegex . '/' . $module; |
55
|
14 |
|
$namePrefix = 'locale-' . $module; |
56
|
|
|
|
57
|
14 |
|
$this->add($routePrefix, [ |
58
|
14 |
|
'locale' => 1, |
59
|
14 |
|
])->setName($namePrefix); |
60
|
|
|
|
61
|
14 |
|
$this->add($routePrefix . '/:controller', [ |
62
|
14 |
|
'locale' => 1, |
63
|
14 |
|
'controller' => 2, |
64
|
14 |
|
])->setName($namePrefix . '-controller'); |
65
|
|
|
|
66
|
14 |
|
$this->add($routePrefix . '/:controller/:action/:params', [ |
67
|
14 |
|
'locale' => 1, |
68
|
14 |
|
'controller' => 2, |
69
|
14 |
|
'action' => 3, |
70
|
14 |
|
'params' => 4, |
71
|
14 |
|
])->setName($namePrefix . '-controller-action'); |
72
|
|
|
|
73
|
14 |
|
foreach ($this->locale as $locale) { |
74
|
14 |
|
$localeRegex = $locale; |
75
|
|
|
|
76
|
14 |
|
$routePrefix = '/' . $localeRegex . '/' . $module; |
77
|
14 |
|
$namePrefix = $locale . '-' . $module; |
78
|
|
|
|
79
|
14 |
|
$this->add($routePrefix, [ |
80
|
14 |
|
'locale' => $locale, |
81
|
14 |
|
])->setName($namePrefix); |
82
|
|
|
|
83
|
14 |
|
$this->add($routePrefix . '/:controller', [ |
84
|
14 |
|
'locale' => $locale, |
85
|
14 |
|
'controller' => 1, |
86
|
14 |
|
])->setName($namePrefix . '-controller'); |
87
|
|
|
|
88
|
14 |
|
$this->add($routePrefix . '/:controller/:action/:params', [ |
89
|
14 |
|
'locale' => $locale, |
90
|
14 |
|
'controller' => 1, |
91
|
14 |
|
'action' => 2, |
92
|
14 |
|
'params' => 3, |
93
|
14 |
|
])->setName($namePrefix . '-controller-action'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|