Skip to content

Commit 233413a

Browse files
authored
Allow usage without twig (#461)
* allow usage of route defaults validator without twig
1 parent adf5944 commit 233413a

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"require": {
1818
"php": "^7.2 || ^8.0",
1919
"symfony-cmf/routing": "^2.3.0",
20-
"symfony/framework-bundle": "^4.4 || ^5.0",
21-
"twig/twig": "^2.4.4 || ^3.0"
20+
"symfony/framework-bundle": "^4.4 || ^5.0"
2221
},
2322
"require-dev": {
2423
"jackalope/jackalope-doctrine-dbal": "^1.3",
@@ -37,7 +36,8 @@
3736
"symfony/twig-bundle": "^4.4 || ^5.0",
3837
"symfony/monolog-bundle": "^3.5",
3938
"doctrine/phpcr-bundle": "^2.1",
40-
"symfony/serializer": "^4.4 || ^5.0"
39+
"symfony/serializer": "^4.4 || ^5.0",
40+
"twig/twig": "^2.4.4 || ^3.0"
4141
},
4242
"suggest": {
4343
"doctrine/phpcr-odm": "To enable support for the PHPCR ODM documents (^1.4)",

src/Resources/config/validators.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class="Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTwigValidator"
1111
>
1212
<argument id="controller_resolver" type="service"/>
13-
<argument id="twig.loader" type="service"/>
13+
<argument id="twig.loader" type="service" on-invalid="null"/>
1414
<tag name="validator.constraint_validator" alias="cmf_routing.validator.route_defaults" />
1515
</service>
1616
</services>

src/Validator/Constraints/RouteDefaultsTwigValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class RouteDefaultsTwigValidator extends ConstraintValidator
2525
private $controllerResolver;
2626

2727
/**
28-
* @var LoaderInterface
28+
* @var LoaderInterface|null
2929
*/
3030
private $twig;
3131

32-
public function __construct(ControllerResolverInterface $controllerResolver, LoaderInterface $twig)
32+
public function __construct(ControllerResolverInterface $controllerResolver, ?LoaderInterface $twig)
3333
{
3434
$this->controllerResolver = $controllerResolver;
3535
$this->twig = $twig;
@@ -49,7 +49,7 @@ public function validate($defaults, Constraint $constraint)
4949
}
5050
}
5151

52-
if (isset($defaults['_template']) && null !== $defaults['_template']) {
52+
if (null !== $this->twig && isset($defaults['_template']) && null !== $defaults['_template']) {
5353
$template = $defaults['_template'];
5454

5555
if (false === $this->twig->exists($template)) {

tests/Unit/Validator/Constraints/RouteDefaultsTwigValidatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Validator\Constraints;
1313

14+
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaults;
1415
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTwigValidator;
1516
use Twig\Loader\LoaderInterface;
1617

@@ -25,4 +26,15 @@ protected function createValidator()
2526
{
2627
return new RouteDefaultsTwigValidator($this->controllerResolver, $this->engine);
2728
}
29+
30+
public function testNoTemplateViolationWithoutTwig()
31+
{
32+
$this->validator = new RouteDefaultsTwigValidator($this->controllerResolver, null);
33+
$this->validator->validate(
34+
['_template' => 'NotExistingBundle:Foo:bar.html.twig'],
35+
new RouteDefaults(['message' => 'my message'])
36+
);
37+
38+
$this->assertNoViolation();
39+
}
2840
}

0 commit comments

Comments
 (0)