1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\mvc\controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use WebComplete\mvc\view\ViewInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractController |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var ViewInterface |
15
|
|
|
*/ |
16
|
|
|
protected $view; |
17
|
|
|
/** |
18
|
|
|
* @var Request |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
/** |
22
|
|
|
* @var Response |
23
|
|
|
*/ |
24
|
|
|
protected $response; |
25
|
|
|
protected $layout; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Request $request |
29
|
|
|
* @param Response $response |
30
|
|
|
* @param ViewInterface $view |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
Request $request, |
34
|
|
|
Response $response, |
35
|
|
|
ViewInterface $view |
36
|
|
|
) { |
37
|
|
|
$view->setController($this); |
38
|
|
|
$this->view = $view; |
39
|
|
|
$this->request = $request; |
40
|
|
|
$this->response = $response; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function beforeAction(): bool |
47
|
|
|
{ |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $result |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function afterAction($result) |
57
|
|
|
{ |
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $templatePath |
63
|
|
|
* @param array $vars |
64
|
|
|
* |
65
|
|
|
* @return Response |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
protected function responseHtml($templatePath, array $vars = []): Response |
69
|
|
|
{ |
70
|
|
|
$html = $this->view->layout($this->layout)->render($templatePath, $vars); |
71
|
|
|
$this->updateResponse(200, 'text/html', $html); |
72
|
|
|
return $this->response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $templatePath |
77
|
|
|
* @param array $vars |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
protected function responseHtmlPartial($templatePath, array $vars = []): Response |
83
|
|
|
{ |
84
|
|
|
$html = $this->view->layout()->render($templatePath, $vars); |
85
|
|
|
$this->updateResponse(200, 'text/html', $html); |
86
|
|
|
return $this->response; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $data |
91
|
|
|
* |
92
|
|
|
* @return Response |
93
|
|
|
* @throws \Exception |
94
|
|
|
*/ |
95
|
|
|
protected function responseJson(array $data): Response |
96
|
|
|
{ |
97
|
|
|
$this->updateResponse(200, 'application/json', \json_encode($data)); |
98
|
|
|
return $this->response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $url |
103
|
|
|
* @param int $code |
104
|
|
|
* @param array $headers |
105
|
|
|
* |
106
|
|
|
* @return RedirectResponse |
107
|
|
|
* @throws \InvalidArgumentException |
108
|
|
|
*/ |
109
|
|
|
protected function responseRedirect(string $url, int $code = 302, array $headers = []): RedirectResponse |
110
|
|
|
{ |
111
|
|
|
return new RedirectResponse($url, $code, $headers); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return Response |
116
|
|
|
* @throws \InvalidArgumentException |
117
|
|
|
*/ |
118
|
|
|
protected function responseNotFound(): Response |
119
|
|
|
{ |
120
|
|
|
$this->response->setStatusCode(404); |
121
|
|
|
return $this->response; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Response |
126
|
|
|
* @throws \InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
protected function responseAccessDenied(): Response |
129
|
|
|
{ |
130
|
|
|
$this->response->setStatusCode(403); |
131
|
|
|
return $this->response; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $statusCode |
136
|
|
|
* @param $contentType |
137
|
|
|
* @param $content |
138
|
|
|
* |
139
|
|
|
* @throws \InvalidArgumentException |
140
|
|
|
* @throws \UnexpectedValueException |
141
|
|
|
*/ |
142
|
|
|
private function updateResponse($statusCode, $contentType, $content) |
143
|
|
|
{ |
144
|
|
|
$this->response->setStatusCode($statusCode); |
145
|
|
|
$this->response->headers->set('content-type', $contentType); |
146
|
|
|
$this->response->setContent($content); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|