From d9a263f4ff06b282943b9df3eb18f0fa28a4d9fa Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 26 Jun 2024 09:37:18 +0200 Subject: [PATCH] Support accessing params from the expression as well --- src/JMS/ObjectRouting/ObjectRouter.php | 4 ++-- .../Tests/ObjectRouting/ObjectRouterTest.php | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/JMS/ObjectRouting/ObjectRouter.php b/src/JMS/ObjectRouting/ObjectRouter.php index 74a330e..f28f959 100644 --- a/src/JMS/ObjectRouting/ObjectRouter.php +++ b/src/JMS/ObjectRouting/ObjectRouter.php @@ -93,10 +93,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']); + $expression = $this->expressionLanguage->parse($expression, ['this', 'params']); $metadata->routes[$type]['paramExpressions'][$k] = $expression; } - $evaluated = $this->expressionLanguage->evaluate($expression, ['this' => $object]); + $evaluated = $this->expressionLanguage->evaluate($expression, ['this' => $object, 'params' => $params]); if ($k[0] === '?') { if ($evaluated === null) { continue; diff --git a/tests/JMS/Tests/ObjectRouting/ObjectRouterTest.php b/tests/JMS/Tests/ObjectRouting/ObjectRouterTest.php index 90d0ece..722097d 100644 --- a/tests/JMS/Tests/ObjectRouting/ObjectRouterTest.php +++ b/tests/JMS/Tests/ObjectRouting/ObjectRouterTest.php @@ -73,6 +73,26 @@ public function testGenerateWithParamExpression() $this->assertEquals('/foobar', $this->router->generate('view', $object)); } + public function testGenerateWithParamExpressionThatRefersToParam() + { + $metadata = new ClassMetadata('stdClass'); + $metadata->addRoute('view', 'view_name', ['foo' => 'bar'], ['concat' => 'params["foo"] ~ this.bar']); + + $object = new \stdClass; + $object->bar = 'baz'; + + $this->factory->expects($this->once()) + ->method('getMetadataForClass') + ->will($this->returnValue($metadata)); + + $this->adapter->expects($this->once()) + ->method('generate') + ->with('view_name', array('foo' => 'baz', 'concat' => 'bazbaz'), false) + ->will($this->returnValue('/foobar')); + + $this->assertEquals('/foobar', $this->router->generate('view', $object)); + } + public function testGenerateWithNullableParamExpression() { $metadata = new ClassMetadata('stdClass');