-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionDispatcher.php
206 lines (185 loc) · 5.3 KB
/
ActionDispatcher.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php declare( strict_types = 1 );
namespace CodeKandis\Tiphy\Actions;
use CodeKandis\RegularExpressions\RegularExpression;
use CodeKandis\Tiphy\Actions\PreDispatchment\PreDispatcherInterface;
use CodeKandis\Tiphy\Actions\PreDispatchment\PreDispatchmentState;
use CodeKandis\Tiphy\Configurations\RoutesConfigurationInterface;
use CodeKandis\Tiphy\Entities\NotFoundEntity;
use CodeKandis\Tiphy\Entities\NotFoundEntityInterface;
use CodeKandis\Tiphy\Http\Requests\JsonBody;
use CodeKandis\Tiphy\Throwables\Handlers\ThrowableHandlerInterface;
use Throwable;
use function is_string;
use function sprintf;
use function strpos;
use function substr;
use function urldecode;
/**
* Represents an action dispatcher.
* @package codekandis/tiphy
* @author Christian Ramelow <info@codekandis.net>
*/
class ActionDispatcher implements ActionDispatcherInterface
{
/**
* Stores the routes configuration.
* @var RoutesConfigurationInterface
*/
private RoutesConfigurationInterface $routesConfiguration;
/**
* Stores the pre-dispatcher of the dispatcher;
* @var ?PreDispatcherInterface
*/
private ?PreDispatcherInterface $preDispatcher;
/**
* Stores the throwable handler of the dispatcher.
* @var ?ThrowableHandlerInterface
*/
private ?ThrowableHandlerInterface $throwableHandler;
/**
* Stores the requested HTTP method.
* @var string
*/
private string $requestedMethod;
/**
* Stores the requested URI.
* @var string
*/
private string $requestedUri;
/**
* Stores the requested route.
* @var string
*/
private string $requestedRoute;
/**
* Constructor method.
* @param RoutesConfigurationInterface $routesConfiguration The routes configuration of the action dispatcher.
* @param ?PreDispatcherInterface $preDispatcher The pre-dispatcher of the action dispatcher.
* @param ?ThrowableHandlerInterface $throwableHandler The throwable handler of the action dispatcher.
*/
public function __construct( RoutesConfigurationInterface $routesConfiguration, ?PreDispatcherInterface $preDispatcher = null, ?ThrowableHandlerInterface $throwableHandler = null )
{
$this->routesConfiguration = $routesConfiguration;
$this->preDispatcher = $preDispatcher;
$this->throwableHandler = $throwableHandler;
$this->requestedMethod = $_SERVER[ 'REQUEST_METHOD' ];
$this->requestedUri = $_SERVER[ 'REQUEST_URI' ];
$this->requestedRoute = $this->getParsedRequestRoute();
}
/**
* Gets the parsed request route.
* @return string The parsed request route.
*/
private function getParsedRequestRoute(): string
{
$queryArgumentsDelimiterPosition = strpos( $this->requestedUri, '?' );
return false === $queryArgumentsDelimiterPosition
? $this->requestedUri
: substr( $this->requestedUri, 0, $queryArgumentsDelimiterPosition );
}
/**
* Filters the arguments from the requested URI.
* @param array $matches The argument matches of the requested URI.
* @return array The filtered arguments.
*/
private function filterArguments( array $matches ): array
{
$filteredArguments = [];
foreach ( $matches as $matchIndex => $matchValue )
{
$isString = is_string( $matchIndex );
if ( true === $isString )
{
$filteredArguments[ $matchIndex ] = urldecode( $matchValue );
}
}
return $filteredArguments;
}
/**
* {@inheritDoc}
*/
public function dispatch(): void
{
try
{
if ( null !== $this->preDispatcher )
{
$preDispatchmentState = new PreDispatchmentState();
$this->preDispatcher->preDispatch( $this->requestedUri, $preDispatchmentState );
if ( true === $preDispatchmentState->getPreventDispatchment() )
{
return;
}
}
$routesPresets = $this->routesConfiguration->getPresets();
$actionClass = null;
$requestBody = '';
$actionArguments = [];
foreach ( $routesPresets as $routesPreset )
{
$baseRoute = $routesPreset->getBaseRoute();
$routeFound = false;
foreach ( $routesPreset->getRoutes() as $configuredRoute => $configuredMethods )
{
$matches = ( new RegularExpression(
sprintf(
'~^%s%s$~',
$baseRoute,
$configuredRoute
)
) )
->match( $this->requestedRoute, false );
if ( null !== $matches )
{
$routeFound = true;
$actionClass = MethodNotAllowedAction::class;
foreach ( $configuredMethods as $configuredMethod => $configuredAction )
{
if ( $configuredMethod === $this->requestedMethod )
{
$actionClass = $configuredAction;
$requestBody = new JsonBody();
$actionArguments = $this->filterArguments( $matches );
break;
}
}
break;
}
}
if ( true === $routeFound )
{
break;
}
}
/**
* @var ActionInterface $action
*/
if ( null === $actionClass )
{
/**
* @var NotFoundEntityInterface $notFoundEntity
*/
$notFoundEntity = NotFoundEntity::fromArray(
[
'method' => $this->requestedMethod,
'uri' => $this->requestedRoute
]
);
$action = new NotFoundAction( $notFoundEntity );
}
else
{
$action = new $actionClass( $this->requestedRoute, $requestBody, $actionArguments );
}
$action->execute();
}
catch ( Throwable $throwable )
{
if ( null === $this->throwableHandler )
{
throw $throwable;
}
$this->throwableHandler->execute( $throwable );
}
}
}