Skip to content

Commit

Permalink
Merge pull request #17 from chimeraphp/allow-mapping-request-handlers
Browse files Browse the repository at this point in the history
Allow mapping request handlers
  • Loading branch information
lcobucci authored Feb 27, 2019
2 parents defac06 + a474aaf commit fda29ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Mapping/ExpandTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class ExpandTags implements CompilerPassInterface
Mapping\Routing\ExecuteEndpoint::class => 'execute',
Mapping\Routing\ExecuteAndFetchEndpoint::class => 'execute_fetch',
Mapping\Routing\FetchEndpoint::class => 'fetch',
Mapping\Routing\SimpleEndpoint::class => 'none',
];

private const SERVICE_TAGS = [
Expand All @@ -38,6 +39,7 @@ final class ExpandTags implements CompilerPassInterface
Mapping\Routing\ExecuteEndpoint::class => Tags::HTTP_ROUTE,
Mapping\Routing\ExecuteAndFetchEndpoint::class => Tags::HTTP_ROUTE,
Mapping\Routing\FetchEndpoint::class => Tags::HTTP_ROUTE,
Mapping\Routing\SimpleEndpoint::class => Tags::HTTP_ROUTE,
Mapping\Routing\Middleware::class => Tags::HTTP_MIDDLEWARE,
];

Expand Down
20 changes: 16 additions & 4 deletions src/Routing/Expressive/RegisterServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

final class RegisterServices implements CompilerPassInterface
{
private const MESSAGE_INVALID_ROUTE = 'You must specify the "route_name", "path", and "type" arguments in '
private const MESSAGE_INVALID_ROUTE = 'You must specify the "route_name", "path", and "behavior" arguments in '
. '"%s" (tag "%s").';

private const BEHAVIORS = [
Expand All @@ -64,6 +64,7 @@ final class RegisterServices implements CompilerPassInterface
'create_fetch' => ['methods' => ['POST'], 'callback' => 'createAndFetch'],
'execute' => ['methods' => ['PATCH', 'PUT', 'DELETE'], 'callback' => 'executeOnly'],
'execute_fetch' => ['methods' => ['PATCH', 'PUT'], 'callback' => 'executeAndFetch'],
'none' => ['methods' => ['GET'], 'callback' => 'noBehavior'],
];

/**
Expand Down Expand Up @@ -114,7 +115,7 @@ private function extractRoutes(ContainerBuilder $container): array

foreach ($container->findTaggedServiceIds(Tags::HTTP_ROUTE) as $serviceId => $tags) {
foreach ($tags as $tag) {
if (! isset($tag['route_name'], $tag['path'], $tag['type'])) {
if (! isset($tag['route_name'], $tag['path'], $tag['behavior'])) {
throw new InvalidArgumentException(
sprintf(self::MESSAGE_INVALID_ROUTE, $serviceId, Tags::HTTP_ROUTE)
);
Expand All @@ -124,8 +125,9 @@ private function extractRoutes(ContainerBuilder $container): array
$tag['methods'] = explode(',', $tag['methods']);
}

$tag['app'] = $tag['app'] ?? $this->applicationName;
$tag['async'] = (bool) ($tag['async'] ?? false);
$tag['app'] = $tag['app'] ?? $this->applicationName;
$tag['async'] = (bool) ($tag['async'] ?? false);
$tag['serviceId'] = $serviceId;

$routes[$tag['app']] = $routes[$tag['app']] ?? [];
$routes[$tag['app']][] = $tag;
Expand Down Expand Up @@ -556,4 +558,14 @@ public function executeAndFetch(string $routeServiceId, array $route, ContainerB

return $this->wrapHandler($routeServiceId, $container);
}

/**
* @param mixed[] $route
*/
public function noBehavior(string $routeServiceId, array $route, ContainerBuilder $container): string
{
$container->setAlias($routeServiceId . '.handler', $route['serviceId']);

return $this->wrapHandler($routeServiceId, $container);
}
}

0 comments on commit fda29ae

Please sign in to comment.