Completed
Push — master ( 22aab3...557f5d )
by Dawid
10s
created

HttpApplication::getMiddlewareAggregator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Application;
4
5
use Igni\Application\Exception\ApplicationException;
6
use Igni\Application\Exception\ControllerException;
7
use Igni\Application\Http\Controller;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Igni\Application\Controller. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Igni\Application\Http\MiddlewareAggregator;
9
use Igni\Application\Http\GenericRouter;
10
use Igni\Network\Http\Middleware\CallableMiddleware;
11
use Igni\Network\Http\Middleware\ErrorMiddleware;
12
use Igni\Network\Http\Middleware\MiddlewarePipe;
13
use Igni\Network\Http\Response;
14
use Igni\Network\Http\Route;
15
use Igni\Network\Http\Router;
16
use Igni\Network\Http\ServerRequest;
17
use Igni\Network\Server\Client;
18
use Igni\Network\Server\HttpServer;
19
use Igni\Network\Server\OnRequestListener;
20
use Psr\Container\ContainerInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\MiddlewareInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
use Throwable;
26
use Zend\HttpHandlerRunner\Emitter\EmitterInterface;
27
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;
28
29
/**
30
 * @package Igni\Application
31
 */
32
class HttpApplication extends Application implements
33
    ControllerAggregator,
34
    MiddlewareAggregator,
35
    MiddlewareInterface,
36
    RequestHandlerInterface,
37
    OnRequestListener
38
{
39
    /**
40
     * @var Router
41
     */
42
    private $router;
43
44
    /**
45
     * @var string[]|MiddlewareInterface[]
46
     */
47
    private $middleware = [];
48
49
    /**
50
     * @var MiddlewarePipe
51
     */
52
    private $pipeline;
53
54
    /**
55
     * @var EmitterInterface
56
     */
57
    private $emitter;
58
59
    /**
60
     * Application constructor.
61
     *
62
     * @param ContainerInterface|null $container
63
     */
64 19
    public function __construct(ContainerInterface $container = null)
65
    {
66 19
        parent::__construct($container);
67
68 19
        if ($this->getContainer()->has(Router::class)) {
69 3
            $this->router = $this->getContainer()->get(Router::class);
70
        } else {
71 16
            $this->router = new GenericRouter();
72
        }
73
74 19
        if ($this->getContainer()->has(EmitterInterface::class)) {
75
            $this->emitter = $this->getContainer()->get(EmitterInterface::class);
76
        } else {
77 19
            $this->emitter = new SapiEmitter();
78
        }
79 19
    }
80
81
    /**
82
     * While testing call this method before handle method.
83
     */
84 8
    public function startup(): void
85
    {
86 8
        $this->handleOnBootListeners();
87 8
        $this->initialize();
88 8
        $this->handleOnRunListeners();
89 8
    }
90
91
    /**
92
     * While testing, call this method after handle method.
93
     */
94 1
    public function shutdown(): void
95
    {
96 1
        $this->handleOnShutDownListeners();
97 1
    }
98
99
    /**
100
     * Startups and run application with/or without dedicated server.
101
     * Once application is run it will listen to incoming http requests,
102
     * and takes care of the entire request flow process.
103
     *
104
     * @param HttpServer|null $server
105
     */
106
    public function run(HttpServer $server = null): void
107
    {
108
        $this->startup();
109
        if ($server) {
110
            $server->addListener($this);
111
            $server->start();
112
        } else {
113
            $response = $this->handle(ServerRequest::fromGlobals());
114
            $this->emitter->emit($response);
115
            if ($response instanceof Response) {
116
                $response->end();
117
            }
118
        }
119
120
        $this->shutdown();
121
    }
122
123
    /**
124
     * Registers PSR-15 compatible middelware.
125
     * Middleware can be either callable object which accepts PSR-7 server request interface and returns
126
     * response interface, or just class name that implements psr-15 middleware or its instance.
127
     *
128
     * @param MiddlewareInterface|callable $middleware
129
     */
130 2
    public function use($middleware): void
131
    {
132 2
        if (!is_subclass_of($middleware, MiddlewareInterface::class)) {
133 2
            if (!is_callable($middleware)) {
134
                throw new ApplicationException(sprintf(
135
                    'Middleware must be either class or object that implements `%s`',
136
                    MiddlewareInterface::class
137
                ));
138
            }
139
140 2
            $middleware = new CallableMiddleware($middleware);
0 ignored issues
show
Bug introduced by
It seems like $middleware can also be of type Psr\Http\Server\MiddlewareInterface; however, parameter $middleware of Igni\Network\Http\Middle...ddleware::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
            $middleware = new CallableMiddleware(/** @scrutinizer ignore-type */ $middleware);
Loading history...
141
        }
142
143 2
        $this->middleware[] = $middleware;
144 2
    }
145
146 14
    public function register($controller, Route $route = null): void
147
    {
148 14
        if (is_callable($controller) && $route !== null) {
149 11
            $route = $route->withController($controller);
150 11
            $this->router->add($route);
151 11
            return;
152
        }
153
154 3
        if ($controller instanceof Controller) {
155
            /** @var Route $route */
156 1
            $route = $controller::getRoute();
157 1
            $route = $route->withController($controller);
158 1
            $this->router->add($route);
159 1
            return;
160
        }
161
162 2
        if (is_string($controller) && is_subclass_of($controller, Controller::class)) {
163
            /** @var Route $route */
164 1
            $route = $controller::getRoute();
165 1
            $route = $route->withController($controller);
166 1
            $this->router->add($route);
167 1
            return;
168
        }
169
170 1
        throw ApplicationException::forInvalidController($controller);
171
    }
172
173
    /**
174
     * Handles request flow process.
175
     *
176
     * @see MiddlewareInterface::process()
177
     *
178
     * @param ServerRequestInterface $request
179
     * @param RequestHandlerInterface $next
180
     * @return ResponseInterface
181
     */
182 13
    public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
183
    {
184
        /** @var Route $route */
185 13
        $route = $this->router->find(
186 13
            $request->getMethod(),
187 13
            $request->getUri()->getPath()
188
        );
189
190 10
        $controller = $route->getController();
191
192 10
        if ($request instanceof ServerRequest) {
193 10
            $request = $request->withAttributes($route->getAttributes());
194
        }
195
196 10
        if (is_string($controller) &&
197 10
            class_exists($controller) &&
198 10
            is_subclass_of($controller, Controller::class)
199
        ) {
200
            /** @var Controller $instance */
201
            $instance = $this->resolver->resolve($controller);
202
            return $instance($request);
203
        }
204
205 10
        if (is_callable($controller)) {
206 10
            $response = $controller($request);
207 10
            if (!$response instanceof ResponseInterface) {
208
                throw ControllerException::forInvalidReturnValue();
209
            }
210
211 10
            return $response;
212
        }
213
214
        throw ControllerException::forMissingController($route->getPath());
215
    }
216
217
    /**
218
     * Runs application listeners and handles request flow process.
219
     *
220
     * @param ServerRequestInterface $request
221
     * @return ResponseInterface
222
     */
223 13
    public function handle(ServerRequestInterface $request): ResponseInterface
224
    {
225 13
        $response = $this->getMiddlewarePipe()->handle($request);
226
227 13
        return $response;
228
    }
229
230
    /**
231
     * Decorator for handle method, used by server instance.
232
     * @see Application::handle()
233
     * @see Server::addListener()
234
     *
235
     * @param ResponseInterface $response
236
     * @param Client $client
237
     * @param ServerRequestInterface $request
238
     * @return ResponseInterface
239
     */
240
    public function onRequest(Client $client, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
241
    {
242
        return $this->handle($request);
243
    }
244
245
    /**
246
     * Registers new controller that accepts get request
247
     * when request uri matches passed route pattern.
248
     *
249
     * @param string $route
250
     * @param callable $controller
251
     */
252 4
    public function get(string $route, callable $controller): void
253
    {
254 4
        $this->register($controller, Route::get($route));
255 4
    }
256
257
    /**
258
     * Registers new controller that accepts post request
259
     * when request uri matches passed route pattern.
260
     *
261
     * @param string $route
262
     * @param callable $controller
263
     */
264 1
    public function post(string $route, callable $controller): void
265
    {
266 1
        $this->register($controller, Route::post($route));
267 1
    }
268
269
    /**
270
     * Registers new controller that accepts put request
271
     * when request uri matches passed route pattern.
272
     *
273
     * @param string $route
274
     * @param callable $controller
275
     */
276 1
    public function put(string $route, callable $controller): void
277
    {
278 1
        $this->register($controller, Route::put($route));
279 1
    }
280
281
    /**
282
     * Registers new controller that accepts patch request
283
     * when request uri matches passed route pattern.
284
     *
285
     * @param string $route
286
     * @param callable $controller
287
     */
288 1
    public function patch(string $route, callable $controller): void
289
    {
290 1
        $this->register($controller, Route::patch($route));
291 1
    }
292
293
    /**
294
     * Registers new controller that accepts delete request
295
     * when request uri matches passed route pattern.
296
     *
297
     * @param string $route
298
     * @param callable $controller
299
     */
300 1
    public function delete(string $route, callable $controller): void
301
    {
302 1
        $this->register($controller, Route::delete($route));
303 1
    }
304
305
    /**
306
     * Registers new controller that accepts options request
307
     * when request uri matches passed route pattern.
308
     *
309
     * @param string $route
310
     * @param callable $controller
311
     */
312 1
    public function options(string $route, callable $controller): void
313
    {
314 1
        $this->register($controller, Route::options($route));
315 1
    }
316
317
    /**
318
     * Registers new controller that accepts head request
319
     * when request uri matches passed route pattern.
320
     *
321
     * @param string $route
322
     * @param callable $controller
323
     */
324 1
    public function head(string $route, callable $controller): void
325
    {
326 1
        $this->register($controller, Route::head($route));
327 1
    }
328
329
    /**
330
     * Registers new controller that listens on the passed route.
331
     *
332
     * @param Route $route
333
     * @param callable $controller
334
     */
335
    public function on(Route $route, callable $controller): void
336
    {
337
        $this->register($controller, $route);
338
    }
339
340
    /**
341
     * Returns application's controller aggregate.
342
     *
343
     * @return ControllerAggregator
344
     */
345 1
    public function getControllerAggregator(): ControllerAggregator
346
    {
347 1
        return $this;
348
    }
349
350
    /**
351
     * Middleware aggregator is used to register application's middlewares.
352
     *
353
     * @return MiddlewareAggregator
354
     */
355
    public function getMiddlewareAggregator(): MiddlewareAggregator
356
    {
357
        return $this;
358
    }
359
360 13
    protected function getMiddlewarePipe(): MiddlewarePipe
361
    {
362 13
        if ($this->pipeline) {
363
            return $this->pipeline;
364
        }
365
366 13
        return $this->pipeline = $this->composeMiddlewarePipe();
367
    }
368
369 13
    private function composeMiddlewarePipe(): MiddlewarePipe
370
    {
371 13
        $pipe = new MiddlewarePipe();
372
        $pipe->add(new ErrorMiddleware(function(Throwable $exception) {
373 3
            return $this->handleOnErrorListeners($exception);
374 13
        }));
375 13
        foreach ($this->middleware as $middleware) {
376 2
            if (is_string($middleware)) {
377
                $middleware = $this->resolver->resolve($middleware);
378
            }
379 2
            $pipe->add($middleware);
380
        }
381 13
        $pipe->add($this);
382
383 13
        return $pipe;
384
    }
385
}
386