1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Network\Request; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Helper\Type\Any; |
7
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait RouteMapFeatures. Process alias and callback route address bindings |
13
|
|
|
* @package Ffcms\Core\Network\Request |
14
|
|
|
*/ |
15
|
|
|
trait RouteMapFeatures |
16
|
|
|
{ |
17
|
|
|
// special variable for route aliasing |
18
|
|
|
protected $aliasPathTarget; |
19
|
|
|
// special variable for route callback binding |
20
|
|
|
protected $callbackClass; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Build static and dynamic path aliases for working set |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
private function runRouteBinding(): void |
27
|
|
|
{ |
28
|
|
|
// calculated depend of language |
29
|
|
|
$pathway = $this->getPathInfo(); |
|
|
|
|
30
|
|
|
/** @var array $routing */ |
31
|
|
|
$routing = App::$Properties->getAll('Routing'); |
32
|
|
|
|
33
|
|
|
// try to work with static aliases |
34
|
|
|
if (Any::isArray($routing) && isset($routing['Alias'], $routing['Alias'][env_name])) { |
|
|
|
|
35
|
|
|
$pathway = $this->findStaticAliases($routing['Alias'][env_name], $pathway); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->setPathdata(explode('/', trim($pathway, '/'))); |
|
|
|
|
39
|
|
|
|
40
|
|
|
// set default controller and action for undefined data |
41
|
|
|
if (!$this->action) { |
42
|
|
|
$this->action = 'Index'; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// empty or contains backslashes? set to main |
46
|
|
|
if (!$this->controller || Str::contains('\\', $this->controller)) { |
47
|
|
|
$this->controller = 'Main'; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// find callback injection in routing configs (calculated in App::run()) |
51
|
|
|
if (Any::isArray($routing) && isset($routing['Callback'], $routing['Callback'][env_name])) { |
52
|
|
|
$this->findDynamicCallbacks($routing['Callback'][env_name], $this->controller); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prepare static pathway aliasing for routing |
58
|
|
|
* @param array|null $map |
59
|
|
|
* @param string|null $pathway |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
private function findStaticAliases(?array $map = null, ?string $pathway = null): ?string |
63
|
|
|
{ |
64
|
|
|
if (!$map) { |
65
|
|
|
return $pathway; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// current pathway is found as "old path" (or alias target). Make redirect to new pathway. |
69
|
|
|
if (Arr::in($pathway, $map)) { |
70
|
|
|
// find "new path" as binding uri slug |
71
|
|
|
$binding = array_search($pathway, $map, true); |
72
|
|
|
// build url to redirection |
73
|
|
|
$url = $this->getSchemeAndHttpHost() . $this->getBasePath() . '/'; |
|
|
|
|
74
|
|
|
if (App::$Properties->get('multiLanguage')) { |
75
|
|
|
$url .= $this->language . '/'; |
76
|
|
|
} |
77
|
|
|
$url .= ltrim($binding, '/'); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$redirect = new RedirectResponse($url); |
80
|
|
|
$redirect->send(); |
81
|
|
|
exit(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// current pathway request is equal to path alias. Set alias to property. |
85
|
|
|
if (array_key_exists($pathway, $map)) { |
86
|
|
|
$pathway = $map[$pathway]; |
87
|
|
|
$this->aliasPathTarget = $pathway; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $pathway; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Prepare dynamic callback data for routing |
95
|
|
|
* @param array|null $map |
96
|
|
|
* @param string|null $controller |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
private function findDynamicCallbacks(array $map = null, ?string $controller = null): void |
100
|
|
|
{ |
101
|
|
|
if (!$map) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// try to find global callback for this controller slug |
106
|
|
|
if (array_key_exists($controller, $map)) { |
107
|
|
|
$class = (string)$map[$controller]; |
108
|
|
|
if (!Str::likeEmpty($class)) { |
109
|
|
|
$this->callbackClass = $class; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get callback class alias if exist |
117
|
|
|
* @return null|string |
118
|
|
|
*/ |
119
|
|
|
public function getCallbackAlias(): ?string |
120
|
|
|
{ |
121
|
|
|
return $this->callbackClass; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get static alias of pathway if exist |
126
|
|
|
* @return null|string |
127
|
|
|
*/ |
128
|
|
|
public function getStaticAlias(): ?string |
129
|
|
|
{ |
130
|
|
|
return $this->aliasPathTarget; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check if current request is aliased by dynamic or static rule |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function isPathInjected(): bool |
138
|
|
|
{ |
139
|
|
|
return $this->callbackClass !== null || $this->aliasPathTarget !== null; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|