|
23 | 23 | use Metadata\Driver\DriverChain; |
24 | 24 | use Metadata\MetadataFactory; |
25 | 25 | use Metadata\MetadataFactoryInterface; |
| 26 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 27 | +use Symfony\Component\ExpressionLanguage\ParsedExpression; |
26 | 28 | use Symfony\Component\PropertyAccess\PropertyAccessor; |
27 | 29 |
|
28 | 30 | class ObjectRouter |
29 | 31 | { |
30 | 32 | private $router; |
31 | 33 | private $metadataFactory; |
32 | 34 | private $accessor; |
| 35 | + private $expressionLanguage; |
33 | 36 |
|
34 | | - public static function create(RouterInterface $router) |
| 37 | + public static function create(RouterInterface $router, ?ExpressionLanguage $expressionLanguage = null) |
35 | 38 | { |
36 | 39 | return new self( |
37 | 40 | $router, |
38 | 41 | new MetadataFactory(new DriverChain([ |
39 | 42 | new AttributeDriver(), |
40 | | - ])) |
| 43 | + ])), |
| 44 | + $expressionLanguage |
41 | 45 | ); |
42 | 46 | } |
43 | 47 |
|
44 | | - public function __construct(RouterInterface $router, MetadataFactoryInterface $metadataFactory) |
| 48 | + public function __construct(RouterInterface $router, MetadataFactoryInterface $metadataFactory, ?ExpressionLanguage $expressionLanguage = null) |
45 | 49 | { |
46 | 50 | $this->router = $router; |
47 | 51 | $this->metadataFactory = $metadataFactory; |
48 | 52 | $this->accessor = new PropertyAccessor(); |
| 53 | + $this->expressionLanguage = $expressionLanguage ?? new ExpressionLanguage(); |
49 | 54 | } |
50 | 55 |
|
51 | 56 | /** |
@@ -80,6 +85,22 @@ public function generate($type, $object, $absolute = false, array $extraParams = |
80 | 85 | $params[$k] = $this->accessor->getValue($object, $path); |
81 | 86 | } |
82 | 87 |
|
| 88 | + foreach ($route['paramExpressions'] as $k => $expression) { |
| 89 | + if (!$expression instanceof ParsedExpression) { |
| 90 | + $expression = $this->expressionLanguage->parse($expression, ['this', 'params']); |
| 91 | + $metadata->routes[$type]['paramExpressions'][$k] = $expression; |
| 92 | + } |
| 93 | + $evaluated = $this->expressionLanguage->evaluate($expression, ['this' => $object, 'params' => $params]); |
| 94 | + if ('?' === $k[0]) { |
| 95 | + if (null === $evaluated) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + $params[substr($k, 1)] = $evaluated; |
| 99 | + } else { |
| 100 | + $params[$k] = $evaluated; |
| 101 | + } |
| 102 | + } |
| 103 | + |
83 | 104 | return $this->router->generate($route['name'], $params, $absolute); |
84 | 105 | } |
85 | 106 |
|
|
0 commit comments