Skip to content

Commit 36ea55c

Browse files
committed
use twig loader to validate template's existence
we do not need to define a twig path at all remove use statement really remove use statement Apply fixes from StyleCI [ci skip] [skip ci] use two services and use templating one, when FrameworkBundle's templating exists use the correct class add compiler pass to container add deprecation notice use argument index instead of parameters name get it running in sandbox use replace method remove debug messages
1 parent 47be1b0 commit 36ea55c

File tree

12 files changed

+197
-26
lines changed

12 files changed

+197
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
2.0.1
5+
-----
6+
7+
* **2017-09-25**: This bundle can now also directly use the Twig loader instead of the deprecated templating
8+
component. Symfony FrameworkBundle no longer requires symfony/templating since 3.2. If the templating component
9+
is available in your application, it is however still used for BC.
10+
411
2.0.0
512
-----
613

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"require": {
1515
"php": "^5.6|^7.0",
1616
"symfony-cmf/routing": "^2.0",
17-
"symfony/framework-bundle": "^2.8|^3.0"
17+
"symfony/framework-bundle": "^2.8|^3.0",
18+
"symfony/twig-bundle": "^2.8|^3.0"
1819
},
1920
"require-dev": {
2021
"symfony-cmf/testing": "^2.0",

src/CmfRoutingBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Doctrine\ORM\Mapping\Driver\XmlDriver as ORMXmlDriver;
2020
use Doctrine\ORM\Version as ORMVersion;
2121
use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\SetRouterPass;
22+
use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\TemplatingValidatorPass;
2223
use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\ValidationPass;
2324
use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
2425
use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRoutersPass;
@@ -42,6 +43,7 @@ public function build(ContainerBuilder $container)
4243
$container->addCompilerPass(new RegisterRouteEnhancersPass());
4344
$container->addCompilerPass(new SetRouterPass());
4445
$container->addCompilerPass(new ValidationPass());
46+
$container->addCompilerPass(new TemplatingValidatorPass());
4547

4648
$this->buildPhpcrCompilerPass($container);
4749
$this->buildOrmCompilerPass($container);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15+
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTemplatingValidator;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
/**
20+
* To avoid a BC-Break: If templating component exists, we will use the validator using general templating
21+
* from FrameworkBundle.
22+
*
23+
* @author Maximilian Berghoff <[email protected]>
24+
*/
25+
class TemplatingValidatorPass implements CompilerPassInterface
26+
{
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function process(ContainerBuilder $container)
31+
{
32+
if (interface_exists(EngineInterface::class) && $container->has('templating')) {
33+
$templatingDefinition = $container->findDefinition('templating');
34+
$validatorDefinition = $container->getDefinition('cmf_routing.validator.route_defaults');
35+
$validatorDefinition->setClass(RouteDefaultsTemplatingValidator::class);
36+
$validatorDefinition->replaceArgument(1, $templatingDefinition);
37+
}
38+
}
39+
}

src/Doctrine/Phpcr/ContentRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getContentId($content)
4141
if (!is_object($content)) {
4242
return;
4343
}
44+
4445
try {
4546
return $this->getObjectManager()->getUnitOfWork()->getDocumentId($content);
4647
} catch (\Exception $e) {

src/Doctrine/Phpcr/IdPrefixListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected function updateId(LifecycleEventArgs $args)
7676
foreach ($this->getPrefixes() as $prefix) {
7777
if (0 === strpos($doc->getId(), $prefix)) {
7878
$doc->setPrefix($prefix);
79+
7980
break;
8081
}
8182
}

src/Resources/config/validators.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
<service id="cmf_routing.validator.route_defaults" class="Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsValidator">
8+
<service
9+
id="cmf_routing.validator.route_defaults"
10+
class="Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTwigValidator"
11+
>
912
<argument id="controller_resolver" type="service"/>
10-
<argument id="templating" type="service"/>
13+
<argument id="twig.loader" type="service"/>
1114
<tag name="validator.constraint_validator" alias="cmf_routing.validator.route_defaults" />
1215
</service>
1316
</services>

src/Validator/Constraints/RouteDefaultsValidator.php renamed to src/Validator/Constraints/RouteDefaultsTemplatingValidator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@
1717
use Symfony\Component\Validator\Constraint;
1818
use Symfony\Component\Validator\ConstraintValidator;
1919

20-
class RouteDefaultsValidator extends ConstraintValidator
20+
/**
21+
* @deprecated Will be removed in 3.0. Use RouteDefaultsTwigValidator with twig as template engine instead.
22+
*
23+
* @author Maximilian Berghoff <[email protected]>
24+
*/
25+
class RouteDefaultsTemplatingValidator extends ConstraintValidator
2126
{
2227
private $controllerResolver;
28+
29+
/**
30+
* @var EngineInterface
31+
*/
2332
private $templating;
2433

2534
public function __construct(ControllerResolverInterface $controllerResolver, EngineInterface $templating)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
16+
use Symfony\Component\Validator\Constraint;
17+
use Symfony\Component\Validator\ConstraintValidator;
18+
use Twig\Loader\LoaderInterface;
19+
20+
/**
21+
* @author Maximilian Berghoff <[email protected]>
22+
*/
23+
class RouteDefaultsTwigValidator extends ConstraintValidator
24+
{
25+
private $controllerResolver;
26+
27+
/**
28+
* @var LoaderInterface
29+
*/
30+
private $twig;
31+
32+
public function __construct(ControllerResolverInterface $controllerResolver, LoaderInterface $twig)
33+
{
34+
$this->controllerResolver = $controllerResolver;
35+
$this->twig = $twig;
36+
}
37+
38+
public function validate($defaults, Constraint $constraint)
39+
{
40+
if (isset($defaults['_controller']) && null !== $defaults['_controller']) {
41+
$controller = $defaults['_controller'];
42+
43+
$request = new Request([], [], ['_controller' => $controller]);
44+
45+
try {
46+
$this->controllerResolver->getController($request);
47+
} catch (\LogicException $e) {
48+
$this->context->addViolation($e->getMessage());
49+
}
50+
}
51+
52+
if (isset($defaults['_template']) && null !== $defaults['_template']) {
53+
$template = $defaults['_template'];
54+
55+
if (false === $this->twig->exists($template)) {
56+
$this->context->addViolation($constraint->message, ['%name%' => $template]);
57+
}
58+
}
59+
}
60+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Validator\Constraints;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15+
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTemplatingValidator;
16+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
17+
18+
class RouteDefaultsTemplatingValidatorTest extends RouteDefaultsValidatorTest
19+
{
20+
protected function setUp()
21+
{
22+
$this->controllerResolver = $this->createMock(ControllerResolverInterface::class);
23+
$this->engine = $this->createMock(EngineInterface::class);
24+
25+
parent::setUp();
26+
}
27+
28+
protected function createValidator()
29+
{
30+
return new RouteDefaultsTemplatingValidator($this->controllerResolver, $this->engine);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Validator\Constraints;
13+
14+
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsTwigValidator;
15+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
16+
use Twig\Loader\LoaderInterface;
17+
18+
class RouteDefaultsTwigValidatorTest extends RouteDefaultsValidatorTest
19+
{
20+
protected function setUp()
21+
{
22+
$this->controllerResolver = $this->createMock(ControllerResolverInterface::class);
23+
$this->engine = $this->createMock(LoaderInterface::class);
24+
25+
parent::setUp();
26+
}
27+
28+
protected function createValidator()
29+
{
30+
return new RouteDefaultsTwigValidator($this->controllerResolver, $this->engine);
31+
}
32+
}

tests/Unit/Validator/Constraints/RouteDefaultsValidatorTest.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,15 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional\Admin;
12+
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Unit\Validator\Constraints;
1313

14-
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1514
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaults;
16-
use Symfony\Cmf\Bundle\RoutingBundle\Validator\Constraints\RouteDefaultsValidator;
17-
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1815
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
1916

20-
class RouteDefaultsValidatorTest extends AbstractConstraintValidatorTest
17+
abstract class RouteDefaultsValidatorTest extends AbstractConstraintValidatorTest
2118
{
22-
private $controllerResolver;
23-
private $templating;
24-
25-
protected function setUp()
26-
{
27-
$this->controllerResolver = $this->createMock(ControllerResolverInterface::class);
28-
$this->templating = $this->createMock(EngineInterface::class);
29-
30-
parent::setUp();
31-
}
32-
33-
protected function createValidator()
34-
{
35-
return new RouteDefaultsValidator($this->controllerResolver, $this->templating);
36-
}
19+
protected $controllerResolver;
20+
protected $engine;
3721

3822
public function testCorrectControllerPath()
3923
{
@@ -56,7 +40,7 @@ public function testControllerPathViolation()
5640

5741
public function testCorrectTemplate()
5842
{
59-
$this->templating->expects($this->any())
43+
$this->engine->expects($this->any())
6044
->method('exists')
6145
->will($this->returnValue(true))
6246
;
@@ -69,7 +53,7 @@ public function testCorrectTemplate()
6953
public function testTemplateViolation()
7054
{
7155
$this
72-
->templating
56+
->engine
7357
->expects($this->any())
7458
->method('exists')
7559
->will($this->returnValue(false))

0 commit comments

Comments
 (0)