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\Str; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait MvcFeatures. Mvc features for Request networking |
12
|
|
|
* @package Ffcms\Core\Network\Request |
13
|
|
|
*/ |
14
|
|
|
trait MvcFeatures |
15
|
|
|
{ |
16
|
|
|
protected $controller; |
17
|
|
|
protected $action; |
18
|
|
|
protected $argumentId; |
19
|
|
|
protected $argumentAdd; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Check if current url in redirect map |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
private function searchRedirect(): void |
27
|
|
|
{ |
28
|
|
|
// calculated depend of language |
29
|
|
|
$pathway = $this->getPathInfo(); |
|
|
|
|
30
|
|
|
/** @var array $routing */ |
31
|
|
|
$routing = App::$Properties->getAll('Routing'); |
32
|
|
|
|
33
|
|
|
if (!Any::isArray($routing) || !isset($routing['Redirect']) || !Any::isArray($routing['Redirect'])) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// check if source uri is key in redirect target map |
38
|
|
|
if (array_key_exists($pathway, $routing['Redirect'])) { |
39
|
|
|
$target = $this->getSchemeAndHttpHost(); // . $this->getBasePath() . '/' . rtrim($routing['Redirect'][$pathway], '/'); |
|
|
|
|
40
|
|
|
if ($this->getBasePath() !== null && !Str::likeEmpty($this->getBasePath())) { |
|
|
|
|
41
|
|
|
$target .= '/' . $this->getBasePath(); |
42
|
|
|
} |
43
|
|
|
$target .= rtrim($routing['Redirect'][$pathway], '/'); |
44
|
|
|
$redirect = new RedirectResponse($target); |
45
|
|
|
$redirect->send(); |
46
|
|
|
exit(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Working with path array data |
52
|
|
|
* @param array|null $pathArray |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function setPathdata(?array $pathArray = null): void |
56
|
|
|
{ |
57
|
|
|
if (!Any::isArray($pathArray) || count($pathArray) < 1) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// check if array length is more then 4 basic elements and slice it recursive |
62
|
|
|
if (count($pathArray) > 4) { |
63
|
|
|
$this->setPathdata(array_slice($pathArray, 0, 4)); |
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Switch path array as reverse without break point! Caution: drugs inside! |
68
|
|
|
switch (count($pathArray)) { |
69
|
|
|
case 4: |
70
|
|
|
$this->argumentAdd = $pathArray[3]; |
71
|
|
|
// no break |
72
|
|
|
case 3: |
73
|
|
|
$this->argumentId = $pathArray[2]; |
74
|
|
|
// no break |
75
|
|
|
case 2: |
76
|
|
|
$this->action = ucfirst(Str::lowerCase($pathArray[1])); |
77
|
|
|
// no break |
78
|
|
|
case 1: |
79
|
|
|
$this->controller = ucfirst(Str::lowerCase($pathArray[0])); |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get current controller name |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getController(): ?string |
89
|
|
|
{ |
90
|
|
|
return $this->controller; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get current controller action() name |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getAction(): ?string |
98
|
|
|
{ |
99
|
|
|
return $this->action; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get current $id argument for controller action |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
|
|
public function getID(): ?string |
107
|
|
|
{ |
108
|
|
|
return urldecode($this->argumentId); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set current controller name |
113
|
|
|
* @param string $name |
114
|
|
|
*/ |
115
|
|
|
public function setController($name): void |
116
|
|
|
{ |
117
|
|
|
$this->controller = $name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set current action value |
122
|
|
|
* @param string $name |
123
|
|
|
*/ |
124
|
|
|
public function setAction($name): void |
125
|
|
|
{ |
126
|
|
|
$this->action = $name; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set current id argument value |
131
|
|
|
* @param mixed $name |
132
|
|
|
*/ |
133
|
|
|
public function setId($name): void |
134
|
|
|
{ |
135
|
|
|
$this->argumentId = $name; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set current add argument value |
140
|
|
|
* @param mixed $name |
141
|
|
|
*/ |
142
|
|
|
public function setAdd($name): void |
143
|
|
|
{ |
144
|
|
|
$this->argumentAdd = $name; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get current $add argument for controller action |
149
|
|
|
* @return string|null |
150
|
|
|
*/ |
151
|
|
|
public function getAdd(): ?string |
152
|
|
|
{ |
153
|
|
|
return urldecode($this->argumentAdd); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|