Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mapping request handlers #17

Merged
merged 3 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}