1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
10
|
|
|
use Selami\Router\Route; |
11
|
|
|
use Selami\Router\Router; |
12
|
|
|
use Selami\View\ViewInterface; |
13
|
|
|
use Zend\Diactoros\Response; |
14
|
|
|
use Zend\Config\Config; |
15
|
|
|
|
16
|
|
|
class Application implements RequestHandlerInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ContainerInterface |
21
|
|
|
*/ |
22
|
|
|
private $container; |
23
|
|
|
/** |
24
|
|
|
* @var Config |
25
|
|
|
*/ |
26
|
|
|
private $config; |
27
|
|
|
/** |
28
|
|
|
* @var Router |
29
|
|
|
*/ |
30
|
|
|
private $router; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $response; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ServerRequestInterface |
39
|
|
|
*/ |
40
|
|
|
private $request; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
ContainerInterface $container, |
44
|
|
|
Router $router, |
45
|
|
|
Config $config |
46
|
|
|
) { |
47
|
|
|
$this->config = $config; |
48
|
|
|
$this->router = $router; |
49
|
|
|
$this->container = $container; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function createWithContainer(ContainerInterface $container) : self |
53
|
|
|
{ |
54
|
|
|
return new self( |
55
|
|
|
$container, |
56
|
|
|
$container->get(Router::class), |
57
|
|
|
$container->get(Config::class) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
62
|
|
|
{ |
63
|
|
|
$this->request = $request; |
64
|
|
|
$this->run(); |
65
|
|
|
return $this->response->returnResponse(new Response()); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) |
69
|
|
|
: ResponseInterface |
70
|
|
|
{ |
71
|
|
|
$this->request = $request; |
72
|
|
|
$this->run(); |
73
|
|
|
return $this->response->returnResponse($response); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function run() : void |
77
|
|
|
{ |
78
|
|
|
$route = $this->getRoute(); |
79
|
|
|
$statusCode = $route->getStatusCode(); |
80
|
|
|
switch ($statusCode) { |
81
|
|
|
case 405: |
82
|
|
|
$this->notFound(405, 'Method Not Allowed'); |
83
|
|
|
break; |
84
|
|
|
case 200: |
85
|
|
|
$this->runRoute( |
86
|
|
|
$route->getController(), |
87
|
|
|
$route->getReturnType(), |
88
|
|
|
$route->getUriParameters() |
89
|
|
|
); |
90
|
|
|
break; |
91
|
|
|
case 404: |
92
|
|
|
default: |
93
|
|
|
$this->notFound($statusCode, 'Not Found'); |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
private function notFound($status, $message) : void |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$errorHandlerClass = $this->container->get('http-error-handler'); |
101
|
|
|
$errorHandler = new $errorHandlerClass($status, $message); |
102
|
|
|
$this->response = new ApplicationResponse( |
|
|
|
|
103
|
|
|
$errorHandlerClass, |
104
|
|
|
$errorHandler(), |
105
|
|
|
$this->config, |
106
|
|
|
$this->container->get(ViewInterface::class) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function getRoute() : Route |
111
|
|
|
{ |
112
|
|
|
$this->router = $this->router |
113
|
|
|
->withDefaultReturnType($this->config->app->get('default_return_type', Router::HTML)) |
114
|
|
|
->withSubFolder($this->config->app->get('app_sub_folder', '')); |
115
|
|
|
$cacheFile = $this->config->app->get('route_cache_file', null); |
116
|
|
|
if ($cacheFile !== null) { |
117
|
|
|
$this->router = $this->router |
118
|
|
|
->withCacheFile($cacheFile); |
119
|
|
|
} |
120
|
|
|
$this->getRouter($this->config->routes); |
121
|
|
|
return $this->router->getRoute(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function getRouter($routes) : void |
125
|
|
|
{ |
126
|
|
|
foreach ($routes as $route) { |
127
|
|
|
$this->router->add($route[0], $route[1], $route[2], $route[3], $route[4] ?? ''); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
private function runRoute($controllerClass, int $returnType, array $args) : void |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$controller = new ApplicationController($this->container, $controllerClass, $returnType, $args); |
134
|
|
|
$this->response = new ApplicationResponse( |
|
|
|
|
135
|
|
|
$controllerClass, |
136
|
|
|
$controller->getControllerResponse(), |
137
|
|
|
$this->config, |
138
|
|
|
$this->container->get(ViewInterface::class) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.