|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Aura\Session\Segment; |
|
6
|
|
|
use Aura\Session\Session; |
|
7
|
|
|
use Interop\Container\Exception\ContainerException; |
|
8
|
|
|
use Monolog\Logger; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Slim\Container; |
|
11
|
|
|
use Slim\Http\Request; |
|
12
|
|
|
use Slim\Http\Response; |
|
13
|
|
|
use Slim\Router; |
|
14
|
|
|
use Slim\Views\Twig; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class AppController. |
|
18
|
|
|
*/ |
|
19
|
|
|
class AppController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Request |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Response |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $response; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Segment |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $session; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Router |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $router; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Logger |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $logger; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Twig |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $twig; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var Session |
|
53
|
|
|
*/ |
|
54
|
|
|
private $sessionHandler; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* AppController constructor. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Container $container |
|
60
|
|
|
* |
|
61
|
|
|
* @throws ContainerException |
|
62
|
|
|
*/ |
|
63
|
5 |
|
public function __construct(Container $container) |
|
64
|
|
|
{ |
|
65
|
5 |
|
$this->request = $container->get('request'); |
|
66
|
5 |
|
$this->response = $container->get('response'); |
|
67
|
5 |
|
$this->sessionHandler = $container->get(Session::class); |
|
68
|
5 |
|
$this->session = $this->sessionHandler->getSegment('app'); |
|
69
|
5 |
|
$this->router = $container->get('router'); |
|
70
|
5 |
|
$this->logger = $container->get(Logger::class); |
|
71
|
5 |
|
$this->twig = $container->get(Twig::class); |
|
72
|
5 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Render HTML. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Response $response |
|
78
|
|
|
* @param Request $request |
|
79
|
|
|
* @param string $file |
|
80
|
|
|
* @param array $viewData |
|
81
|
|
|
* |
|
82
|
|
|
* @return ResponseInterface |
|
83
|
|
|
*/ |
|
84
|
3 |
|
protected function render( |
|
85
|
|
|
Response $response, |
|
86
|
|
|
Request $request, |
|
87
|
|
|
string $file, |
|
88
|
|
|
array $viewData = [] |
|
89
|
|
|
): ResponseInterface { |
|
90
|
|
|
$extend = [ |
|
91
|
3 |
|
'language' => $request->getAttribute('language'), |
|
92
|
3 |
|
'page' => 'Slim Application', |
|
93
|
3 |
|
'is_logged_in' => $this->session->get('logged_in') ?: false, |
|
94
|
|
|
]; |
|
95
|
3 |
|
$viewData = array_replace_recursive($extend, $viewData); |
|
96
|
|
|
|
|
97
|
3 |
|
return $this->twig->render($response, $file, $viewData); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Return JSON Response. |
|
102
|
|
|
* |
|
103
|
|
|
* @param Response $response |
|
104
|
|
|
* @param array $data |
|
105
|
|
|
* @param int $status |
|
106
|
|
|
* |
|
107
|
|
|
* @return ResponseInterface |
|
108
|
|
|
*/ |
|
109
|
1 |
|
protected function json(Response $response, $data, int $status = 200): ResponseInterface |
|
110
|
|
|
{ |
|
111
|
1 |
|
return $response->withJson($data, $status); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Return redirect. |
|
116
|
|
|
* |
|
117
|
|
|
* @param Response $response |
|
118
|
|
|
* @param string $url |
|
119
|
|
|
* @param int $status |
|
120
|
|
|
* |
|
121
|
|
|
* @return ResponseInterface |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function redirect(Response $response, string $url, int $status = 301): ResponseInterface |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $response->withRedirect($url, $status); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|