|
| 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 | +} |
0 commit comments