Skip to content

Commit

Permalink
feat: make the condition resolver available in a static routes (#1820…
Browse files Browse the repository at this point in the history
…) (#1823)

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Doc PR?       | no
| Backport      | 0.10, 0.11, 0.12
| License       | MIT

* Make the condition resolver available in a static routes over the request context

(cherry picked from commit 2f3f955e136fc66e47a9b99f3bc56baa897b65dd)

Co-authored-by: Gerhard Seidel <[email protected]>
  • Loading branch information
mergify[bot] and gseidel authored Aug 14, 2023
1 parent 390fdfc commit be1dbd7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 4 additions & 2 deletions DependencyInjection/Compiler/ConditionResolverPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class ConditionResolverPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$conditionResolver = $container->getParameter('enhavo_routing.condition_resolver');
if($conditionResolver) {
if ($conditionResolver) {
$definition = $container->getDefinition('cmf_routing.final_matcher');
$definition->setClass(ConditionUrlMatcher::class);
$definition->addArgument(new Reference($conditionResolver));

$container->setAlias('enhavo_routing.condition_resolver', $conditionResolver);
}
}
}
}
30 changes: 30 additions & 0 deletions Request/RequestContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Enhavo\Bundle\RoutingBundle\Request;

use Enhavo\Bundle\RoutingBundle\Condition\ConditionResolverInterface;

/**
* We want to use the resolver in static routes. Unlike in cmf routing, the static routes getting compiled to php
* for faster execution. So it is hard to inject further variables to the condition language expression. Thus, we abuse
* the request context to inject our condition resolver to make it available in any static routing condition expression.
*/
class RequestContext extends \Symfony\Component\Routing\RequestContext
{
private ?ConditionResolverInterface $resolver;

public function resolve()
{
return $this->resolver->resolve();
}

public function getResolver(): ?ConditionResolverInterface
{
return $this->resolver;
}

public function setResolver(?ConditionResolverInterface $resolver): void
{
$this->resolver = $resolver;
}
}
6 changes: 6 additions & 0 deletions Resources/config/services/general.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ services:
Enhavo\Bundle\RoutingBundle\Twig\SlugifyExtension:
tags:
- { name: twig.extension }

Enhavo\Bundle\RoutingBundle\Request\RequestContext:
decorates: router.request_context
class: Enhavo\Bundle\RoutingBundle\Request\RequestContext
calls:
- ['setResolver', ['@enhavo_routing.condition_resolver']]

0 comments on commit be1dbd7

Please sign in to comment.