1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/boot project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Boot\Middleware; |
10
|
|
|
|
11
|
|
|
use Daikon\Config\ConfigProviderInterface; |
12
|
|
|
use Middlewares\ContentEncoding; |
13
|
|
|
use Middlewares\ContentLanguage; |
14
|
|
|
use Middlewares\ContentType; |
15
|
|
|
use Middlewares\Cors; |
16
|
|
|
use Middlewares\JsonPayload; |
17
|
|
|
use Middlewares\RequestHandler; |
18
|
|
|
use Middlewares\UrlEncodePayload; |
19
|
|
|
use Middlewares\Whoops; |
20
|
|
|
use Psr\Container\ContainerInterface; |
21
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
22
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
23
|
|
|
use Relay\Relay; |
24
|
|
|
|
25
|
|
|
final class DefaultPipeline implements PipelineBuilderInterface |
26
|
|
|
{ |
27
|
|
|
private const DEFAULT_PIPELINE = [ |
28
|
|
|
ContentType::class, |
29
|
|
|
ContentLanguage::class, |
30
|
|
|
ContentEncoding::class, |
31
|
|
|
RoutingHandler::class, |
32
|
|
|
JsonPayload::class, |
33
|
|
|
UrlEncodePayload::class, |
34
|
|
|
ActionHandler::class, |
35
|
|
|
RequestHandler::class |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
private ContainerInterface $container; |
39
|
|
|
|
40
|
|
|
private ConfigProviderInterface $configProvider; |
41
|
|
|
|
42
|
|
|
private array $settings; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
ContainerInterface $container, |
46
|
|
|
ConfigProviderInterface $configProvider, |
47
|
|
|
array $settings = [] |
48
|
|
|
) { |
49
|
|
|
$this->container = $container; |
50
|
|
|
$this->configProvider = $configProvider; |
51
|
|
|
$this->settings = $settings; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function __invoke(): RequestHandlerInterface |
55
|
|
|
{ |
56
|
|
|
$middlewares = []; |
57
|
|
|
$this->addDebug($middlewares, $this->container->get(Whoops::class)); |
58
|
|
|
|
59
|
|
|
if ($this->configProvider->get('project.cors.enabled', false)) { |
60
|
|
|
$this->add($middlewares, $this->container->get(Cors::class)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->add($middlewares, ...array_map( |
64
|
|
|
[$this->container, 'get'], |
65
|
|
|
$this->settings['pipeline'] ?? self::DEFAULT_PIPELINE |
66
|
|
|
)); |
67
|
|
|
|
68
|
|
|
return new Relay($middlewares); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function addDebug(array &$middlewares, MiddlewareInterface ...$middleware): void |
72
|
|
|
{ |
73
|
|
|
if ($this->configProvider->get('app.debug') === true) { |
74
|
|
|
$this->add($middlewares, ...$middleware); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function add(array &$middlewares, MiddlewareInterface ...$middleware): void |
79
|
|
|
{ |
80
|
|
|
array_push($middlewares, ...$middleware); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|