-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseRouter.php
135 lines (120 loc) · 3.33 KB
/
BaseRouter.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Web\Component\Router;
use Throwable;
use Ulrack\Web\Common\Router\RouterInterface;
use Ulrack\Web\Common\Endpoint\InputInterface;
use Ulrack\Web\Common\Endpoint\OutputInterface;
use Ulrack\Web\Common\Endpoint\EndpointInterface;
use Ulrack\Web\Common\Error\ErrorHandlerInterface;
use Ulrack\Web\Common\Output\OutputHandlerInterface;
use GrizzIt\Services\Common\Factory\ServiceFactoryInterface;
class BaseRouter implements RouterInterface
{
/**
* Contains the router.
*
* @var RouterInterface
*/
private RouterInterface $router;
/**
* Determines whether a router has been found.
*
* @var bool
*/
private bool $found = false;
/**
* Contains the output handler.
*
* @var OutputHandlerInterface
*/
private OutputHandlerInterface $outputHandler;
/**
* Contains the error handler.
*
* @var ErrorHandlerInterface
*/
private ErrorHandlerInterface $errorHandler;
/**
* Contains the service factory.
*
* @var ServiceFactoryInterface
*/
private ServiceFactoryInterface $serviceFactory;
/**
* Constructor.
*
* @param RouterInterface $router
* @param OutputHandlerInterface $outputHandler
* @param ErrorHandlerInterface $errorHandler
* @param ServiceFactoryInterface $serviceFactory
*/
public function __construct(
RouterInterface $router,
OutputHandlerInterface $outputHandler,
ErrorHandlerInterface $errorHandler,
ServiceFactoryInterface $serviceFactory
) {
$this->router = $router;
$this->outputHandler = $outputHandler;
$this->errorHandler = $errorHandler;
$this->serviceFactory = $serviceFactory;
}
/**
* Determines whether the router accepts the request.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return bool
*/
public function accepts(
InputInterface $input,
OutputInterface $output
): bool {
$this->found = $this->router->accepts($input, $output);
return $this->found;
}
/**
* Resolves the request to an endpoint, executes it and renders the response.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
public function __invoke(
InputInterface $input,
OutputInterface $output
): void {
if (!$this->found) {
$this->errorHandler->outputByCode(404);
return;
}
try {
$this->router->__invoke($input, $output);
$endpoint = $this->getEndpoint($input);
$endpoint($input, $output);
$this->outputHandler->__invoke($input, $output);
} catch (Throwable $exception) {
$this->errorHandler->outputByException($exception);
}
}
/**
* Constructs and retrieves the endpoint.
*
* @param InputInterface $input
*
* @return EndpointInterface
*/
private function getEndpoint(InputInterface $input): EndpointInterface
{
return $this->serviceFactory->create(
$input->getParameter('endpoint'),
$input->getParameters()
);
}
}