|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: famoser |
|
5
|
|
|
* Date: 27.05.2016 |
|
6
|
|
|
* Time: 14:23 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Famoser\XKCDCache\Controllers\Base; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Famoser\XKCDCache\Framework\ContainerBase; |
|
13
|
|
|
use Famoser\XKCDCache\Models\Response\Base\BaseResponse; |
|
14
|
|
|
use Slim\Http\Request; |
|
15
|
|
|
use Slim\Http\Response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* the base controller which provides access to the environment |
|
19
|
|
|
* |
|
20
|
|
|
* Class BaseController |
|
21
|
|
|
* @package Famoser\XKCDCache\Controllers\Base |
|
22
|
|
|
*/ |
|
23
|
|
|
class BaseController extends ContainerBase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* redirects to the route specified in $slug |
|
27
|
|
|
* |
|
28
|
|
|
* @param Request $request |
|
29
|
|
|
* @param Response $response |
|
30
|
|
|
* @param string $slug |
|
31
|
|
|
* @return Response |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function redirect(Request $request, Response $response, $slug) |
|
34
|
|
|
{ |
|
35
|
|
|
$reqUri = $request->getUri()->withPath($this->getRouter()->pathFor($slug)); |
|
36
|
|
|
return $response->withRedirect($reqUri); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* returns model as json |
|
41
|
|
|
* |
|
42
|
|
|
* @param Response $response |
|
43
|
|
|
* @param BaseResponse $responseContent |
|
44
|
|
|
* @return Response |
|
45
|
|
|
*/ |
|
46
|
4 |
|
protected function returnJson(Response $response, BaseResponse $responseContent) |
|
47
|
|
|
{ |
|
48
|
4 |
|
return $this->returnRawJson($response, json_encode($responseContent)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* returns model as json |
|
53
|
|
|
* |
|
54
|
|
|
* @param Response $response |
|
55
|
|
|
* @param string $json |
|
56
|
|
|
* @return Response |
|
57
|
|
|
*/ |
|
58
|
4 |
|
protected function returnRawJson(Response $response, $json) |
|
59
|
|
|
{ |
|
60
|
4 |
|
$response->getBody()->write($json); |
|
61
|
4 |
|
return $response->withHeader('Content-Type', 'application/json'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* returns model as json |
|
66
|
|
|
* |
|
67
|
|
|
* @param Response $response |
|
68
|
|
|
* @param $type |
|
69
|
|
|
* @param $filename |
|
70
|
|
|
* @param $length |
|
71
|
|
|
* @param $content |
|
72
|
|
|
* @return Response |
|
73
|
|
|
* @internal param string $json |
|
74
|
|
|
*/ |
|
75
|
1 |
|
protected function returnRawFile(Response $response, $type, $filename, $length, $content) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$response->withHeader('Content-Type', $type); |
|
78
|
1 |
|
$response->withHeader('Pragma', "public"); |
|
79
|
1 |
|
$response->withHeader('Content-disposition:', 'attachment; filename=' . $filename); |
|
80
|
1 |
|
$response->withHeader('Content-Transfer-Encoding', 'binary'); |
|
81
|
1 |
|
$response->withHeader('Content-Length', $length); |
|
82
|
1 |
|
$response->getBody()->write($content); |
|
83
|
1 |
|
return $response->withHeader('Content-Type', 'application/json'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|