Skip to content

Commit

Permalink
Keep parsed expressions at least during the current request
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Jun 26, 2024
1 parent 2c0eaf4 commit b89a8e7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/JMS/ObjectRouting/ObjectRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Metadata\MetadataFactory;
use Metadata\MetadataFactoryInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ParsedExpression;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class ObjectRouter
Expand All @@ -33,22 +34,23 @@ class ObjectRouter
private $accessor;
private $expressionLanguage;

public static function create(RouterInterface $router)
public static function create(RouterInterface $router, ?ExpressionLanguage $expressionLanguage = null)
{
return new self(
$router,
new MetadataFactory(new DriverChain([
new AttributeDriver(),
]))
])),
$expressionLanguage
);
}

public function __construct(RouterInterface $router, MetadataFactoryInterface $metadataFactory)
public function __construct(RouterInterface $router, MetadataFactoryInterface $metadataFactory, ?ExpressionLanguage $expressionLanguage = null)
{
$this->router = $router;
$this->metadataFactory = $metadataFactory;
$this->accessor = new PropertyAccessor();
$this->expressionLanguage = new ExpressionLanguage();
$this->expressionLanguage = $expressionLanguage ?? new ExpressionLanguage();
}

/**
Expand Down Expand Up @@ -90,6 +92,10 @@ public function generate($type, $object, $absolute = false, array $extraParams =
}

foreach ($route['paramExpressions'] as $k => $expression) {
if (!$expression instanceof ParsedExpression) {
$expression = $this->expressionLanguage->parse($expression, ['this']);
$metadata->routes[$type]['paramExpressions'][$k] = $expression;
}
$evaluated = $this->expressionLanguage->evaluate($expression, ['this' => $object]);
if ($k[0] === '?') {
if ($evaluated === null) {
Expand Down

0 comments on commit b89a8e7

Please sign in to comment.