-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
117 lines (99 loc) · 3.04 KB
/
Controller.php
File metadata and controls
117 lines (99 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Cradle;
use DI\Container;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Database\Capsule\Manager;
use Slim\Exception\HttpInternalServerErrorException;
/**
* The base class for all controllers in the system.
* All valid controllers must either extend this class or extend one of its subclasses.
*/
abstract class Controller
{
/** @var Container $container A container instance. */
protected $container;
/** @var ResponseInterface $response The current response being handled by the controller. */
protected $response;
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Parse the response from a controller
*
* @param ResponseInterface $response
* @param mixed $body
* @return ResponseInterface
*/
protected function parseResponseBody(ResponseInterface $response, $body): ResponseInterface
{
switch (gettype($body)) {
case 'string': // Plain text
$response->getBody()->write($body);
$contentType = 'text/plain';
break;
case 'array': // An array
$response->getBody()->write(json_encode($body));
$contentType = 'application/json';
break;
case 'object': // If a view file is returned
if (get_class($body) != View::class & !is_subclass_of($body, View::class)) {
break;
}
$viewCompiler = $this->container->get('view');
$viewCompiler->clearViews();
$viewCompiler->addView($body);
$response->getBody()->write($viewCompiler->compileViews());
$contentType = 'text/html';
break;
default: // If now response from the controller
$contentType = null;
break;
}
if (!is_null($contentType) && count($response->getHeader('Content-Type')) == 0) { // If a content type was not set and one was detected.
$response = $response->withHeader('Content-Type', $contentType);
}
return $response;
}
/**
* Set a header
*
* @param string $name
* @param string $value
* @return array The array of header values for that header
*/
protected function setHeader(string $name, string $value): array
{
$this->response = $this->response->withHeader($name, $value);
return $this->getHeader($name);
}
/**
* Get an array of header values for a particular header
*
* @param string $name
* @return array
*/
protected function getHeader(string $name): array
{
return $this->response->getHeader($name);
}
/**
* When a controller method is called
*
* @param string $name
* @param array $arguments
* @return void
*/
public function __call(string $name, array $arguments): ResponseInterface
{
if (!method_exists($this, $name)) { // Method doesn't exist
throw new HttpInternalServerErrorException($arguments[0]);
}
$request = $arguments[0];
$params = (object) $arguments[2];
$this->response = $arguments[1];
$body = call_user_func_array([$this, $name], [$request, $params]);
$this->response = $this->parseResponseBody($this->response, $body);
return $this->response;
}
}