vendor/api-platform/core/src/Core/Bridge/Symfony/Routing/RouterOperationPathResolver.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\Routing;
  12. use ApiPlatform\Core\Api\OperationType;
  13. use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
  14. use ApiPlatform\Exception\InvalidArgumentException;
  15. use ApiPlatform\PathResolver\OperationPathResolverInterface;
  16. use Symfony\Component\Routing\RouterInterface;
  17. /**
  18. * Resolves the operations path using a Symfony route.
  19. * TODO: remove this in 3.0.
  20. *
  21. * @author Guilhem N. <egetick@gmail.com>
  22. */
  23. final class RouterOperationPathResolver implements OperationPathResolverInterface
  24. {
  25. private $router;
  26. private $deferred;
  27. public function __construct(RouterInterface $router, OperationPathResolverInterface $deferred)
  28. {
  29. $this->router = $router;
  30. $this->deferred = $deferred;
  31. }
  32. /**
  33. * @throws InvalidArgumentException
  34. */
  35. public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/* , string $operationName = null */): string
  36. {
  37. if (\func_num_args() >= 4) {
  38. $operationName = (string) func_get_arg(3);
  39. } else {
  40. @trigger_error(sprintf('Method %s() will have a 4th `string $operationName` argument in version 3.0. Not defining it is deprecated since 2.1.', __METHOD__), \E_USER_DEPRECATED);
  41. $operationName = null;
  42. }
  43. if (isset($operation['route_name'])) {
  44. $routeName = $operation['route_name'];
  45. } elseif (OperationType::SUBRESOURCE === $operationType) {
  46. throw new InvalidArgumentException('Subresource operations are not supported by the RouterOperationPathResolver without a route name.');
  47. } elseif (null === $operationName) {
  48. return $this->deferred->resolveOperationPath($resourceShortName, $operation, OperationTypeDeprecationHelper::getOperationType($operationType), $operationName);
  49. } elseif (isset($operation['uri_template'])) {
  50. return $operation['uri_template'];
  51. } else {
  52. $routeName = RouteNameGenerator::generate($operationName, $resourceShortName, $operationType);
  53. }
  54. if (!$route = $this->router->getRouteCollection()->get($routeName)) {
  55. throw new InvalidArgumentException(sprintf('The route "%s" of the resource "%s" was not found.', $routeName, $resourceShortName));
  56. }
  57. return $route->getPath();
  58. }
  59. }