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

IBX-6504: Gracefully handled URL generation in RoutingExtension #389

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ services:
class: eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\RoutingExtension
arguments: ["@ezpublish.route_reference.generator", "@router"]
tags:
- {name: twig.extension}
- { name: twig.extension }
- { name: 'monolog.logger', channel: 'ibexa.core' }

eZ\Publish\Core\MVC\Symfony\Templating\Twig\ResourceProvider:
arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@

namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface;
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference;
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Throwable;
use Twig\Extension\AbstractExtension;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Node;
use Twig\TwigFunction;

class RoutingExtension extends AbstractExtension
class RoutingExtension extends AbstractExtension implements LoggerAwareInterface
{
use LoggerAwareTrait;

/** @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface */
private $routeReferenceGenerator;

Expand All @@ -32,10 +40,12 @@ class RoutingExtension extends AbstractExtension

public function __construct(
RouteReferenceGeneratorInterface $routeReferenceGenerator,
UrlGeneratorInterface $urlGenerator
UrlGeneratorInterface $urlGenerator,
?LoggerInterface $logger = null
) {
$this->routeReferenceGenerator = $routeReferenceGenerator;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger ?? new NullLogger();
}

public function getFunctions(): array
Expand Down Expand Up @@ -78,14 +88,30 @@ public function getPath(object $name, array $parameters = [], bool $relative = f
{
$referenceType = $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH;

return $this->generateUrlForObject($name, $parameters, $referenceType);
return $this->tryGeneratingUrlForObject($name, $parameters, $referenceType);
}

public function getUrl(object $name, array $parameters = [], bool $schemeRelative = false): string
{
$referenceType = $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL;

return $this->generateUrlForObject($name, $parameters, $referenceType);
return $this->tryGeneratingUrlForObject($name, $parameters, $referenceType);
}

private function tryGeneratingUrlForObject(object $object, array $parameters, int $referenceType): string
{
try {
return $this->generateUrlForObject($object, $parameters, $referenceType);
} catch (NotFoundException $e) {
return '';
} catch (Throwable $e) {
$this->logger->warning(
'Url could not be generated.',
['exception' => $e]
);

return '';
alongosz marked this conversation as resolved.
Show resolved Hide resolved
}
}

private function generateUrlForObject(object $object, array $parameters, int $referenceType): string
Expand Down