1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @license MIT |
4
|
|
|
*/ |
5
|
|
|
namespace Pivasic\Core; |
6
|
|
|
|
7
|
|
|
use Pivasic\Bundle\Debug\Wrap; |
8
|
|
|
use Pivasic\Bundle\Template\Native; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base functions to service request. |
12
|
|
|
* |
13
|
|
|
* Class BaseController |
14
|
|
|
* @package Pivasic\Core |
15
|
|
|
*/ |
16
|
|
|
class BaseController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param string $packageRoot |
20
|
|
|
* @param Request $request |
21
|
|
|
*/ |
22
|
|
|
public function __construct(string &$packageRoot, Request &$request) |
23
|
|
|
{ |
24
|
|
|
$this->packageRoot =& $packageRoot; |
25
|
|
|
$this->request =& $request; |
26
|
|
|
$this->response = new Response(); |
27
|
|
|
$this->data['request'] =& $this->request; |
28
|
|
|
$this->deleteJsonKeys = true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $handler |
33
|
|
|
*/ |
34
|
|
|
public function invoke(string $handler) |
35
|
|
|
{ |
36
|
|
|
$this->$handler(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Response |
41
|
|
|
*/ |
42
|
|
|
public function getResponse(): Response |
43
|
|
|
{ |
44
|
|
|
return $this->response; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param bool $delete |
49
|
|
|
*/ |
50
|
|
|
protected function deleteJsonKeys(bool $delete) |
51
|
|
|
{ |
52
|
|
|
$this->deleteJsonKeys = $delete; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Response $response |
57
|
|
|
*/ |
58
|
|
|
protected function setResponse(Response &$response) |
59
|
|
|
{ |
60
|
|
|
$this->response =& $response; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add items to the template data array. |
65
|
|
|
* |
66
|
|
|
* @param array $data |
67
|
|
|
*/ |
68
|
|
|
protected function addData(array $data) |
69
|
|
|
{ |
70
|
|
|
$this->data = array_replace($this->data, $data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create Response from template. |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @param array $data |
78
|
|
|
*/ |
79
|
|
|
protected function setView(string $name = '', array $data = []) |
80
|
|
|
{ |
81
|
|
|
if (!empty($data)) { |
82
|
|
|
$this->data = array_replace($this->data, $data); |
83
|
|
|
} |
84
|
|
|
$content = (new Native($this->packageRoot, $this->request->language(), !Wrap::isEnabled()))->getContent($name, $this->data); |
85
|
|
|
$this->response->setContent($content); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create Response from content. |
90
|
|
|
* |
91
|
|
|
* @param string $content |
92
|
|
|
*/ |
93
|
|
|
protected function setContent(string $content) |
94
|
|
|
{ |
95
|
|
|
$this->response->setContent($content); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create Response from content. |
100
|
|
|
* |
101
|
|
|
* @param array $content |
102
|
|
|
*/ |
103
|
|
|
protected function setJSONContent(array $content) |
104
|
|
|
{ |
105
|
|
|
if ($this->deleteJsonKeys) { |
106
|
|
|
$content = $this->deleteArrayKeys($content); |
107
|
|
|
} |
108
|
|
|
$content = json_encode($content, JSON_UNESCAPED_UNICODE); |
109
|
|
|
$this->response->setContentTypeJson(); |
110
|
|
|
$this->response->setContent($content); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Redirect to the URL with statusCode. |
115
|
|
|
* |
116
|
|
|
* @param string $url |
117
|
|
|
* @param int $statusCode |
118
|
|
|
*/ |
119
|
|
|
protected function setRedirect(string $url = '', int $statusCode = 303) |
120
|
|
|
{ |
121
|
|
|
$this->response->redirect($url, $statusCode); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get route digit's. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function getNumList(): array |
130
|
|
|
{ |
131
|
|
|
preg_match_all('/\/\d+/u', $this->request->route(), $numList); |
132
|
|
|
$numList = $numList[0]; |
133
|
|
|
$numList = array_map(function($val) { |
134
|
|
|
return intval(ltrim($val, '/')); |
135
|
|
|
}, $numList); |
136
|
|
|
return $numList; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $arr |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
private function deleteArrayKeys(&$arr): array |
144
|
|
|
{ |
145
|
|
|
$lst = []; |
146
|
|
|
foreach($arr as $k => $v){ |
147
|
|
|
if (is_array($v)) { |
148
|
|
|
$lst[] = $this->deleteArrayKeys($v); |
149
|
|
|
} else { |
150
|
|
|
$lst[] = $v; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
return $lst; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected $request; |
157
|
|
|
protected $response; |
158
|
|
|
protected $data; |
159
|
|
|
protected $deleteJsonKeys; |
160
|
|
|
|
161
|
|
|
private $packageRoot; |
162
|
|
|
} |