|
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 $args; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Check if current url in redirect map |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
private function searchRedirect(): void |
|
25
|
|
|
{ |
|
26
|
|
|
// calculated depend of language |
|
27
|
|
|
$pathway = $this->getPathInfo(); |
|
|
|
|
|
|
28
|
|
|
/** @var array $routing */ |
|
29
|
|
|
$routing = App::$Properties->getAll('Routing'); |
|
30
|
|
|
|
|
31
|
|
|
if (!Any::isArray($routing) || !isset($routing['Redirect']) || !Any::isArray($routing['Redirect'])) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// check if source uri is key in redirect target map |
|
36
|
|
|
if (array_key_exists($pathway, $routing['Redirect'])) { |
|
37
|
|
|
$target = $this->getSchemeAndHttpHost(); // . $this->getBasePath() . '/' . rtrim($routing['Redirect'][$pathway], '/'); |
|
|
|
|
|
|
38
|
|
|
if ($this->getBasePath() !== null && !Str::likeEmpty($this->getBasePath())) { |
|
|
|
|
|
|
39
|
|
|
$target .= '/' . $this->getBasePath(); |
|
40
|
|
|
} |
|
41
|
|
|
$target .= rtrim($routing['Redirect'][$pathway], '/'); |
|
42
|
|
|
$redirect = new RedirectResponse($target); |
|
43
|
|
|
$redirect->send(); |
|
44
|
|
|
exit(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Working with path array data |
|
50
|
|
|
* @param array|null $pathArray |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
private function setPathdata(?array $pathArray = null): void |
|
54
|
|
|
{ |
|
55
|
|
|
if (!Any::isArray($pathArray) || count($pathArray) < 1) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// extract controller info from full path array |
|
60
|
|
|
$this->controller = ucfirst(Str::lowerCase(array_shift($pathArray))); |
|
|
|
|
|
|
61
|
|
|
if (count($pathArray) > 0) { |
|
62
|
|
|
// extract action |
|
63
|
|
|
$this->action = ucfirst(Str::lowerCase(array_shift($pathArray))); |
|
64
|
|
|
|
|
65
|
|
|
// safe other parts to arguments if exist |
|
66
|
|
|
if (count($pathArray) > 0) { |
|
67
|
|
|
$this->args = $pathArray; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get current controller name |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getController(): ?string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->controller; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get current controller action() name |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getAction(): ?string |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->action; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set current controller name |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setController($name): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->controller = $name; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set current action value |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setAction($name): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->action = $name; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get arguments from pathway |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getArguments(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return (array)$this->args; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|