for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Symfony\Component\Routing\RouterInterface;
/**
* {@inheritdoc}
* @author Kévin Dunglas <[email protected]>
final class RouteNameResolver implements RouteNameResolverInterface
{
private $router;
public function __construct(RouterInterface $router)
$this->router = $router;
}
public function getRouteName(string $resourceClass, bool $collection): string
$operationType = $collection ? 'collection' : 'item';
foreach ($this->router->getRouteCollection()->all() as $routeName => $route) {
$currentResourceClass = $route->getDefault('_api_resource_class');
$operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType));
$methods = $route->getMethods();
if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || in_array('GET', $methods, true))) {
return $routeName;
throw new InvalidArgumentException(sprintf('No %s route associated with the type "%s".', $operationType, $resourceClass));