-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
478 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Twig Syntax Validator | ||
============= | ||
This Twig Syntax Validator adds an validation constraint that will validate a string as containing valid twig syntax. | ||
|
||
Example | ||
------------- | ||
Enable the validator | ||
```YAML | ||
# app/config/config.yml | ||
boekkooi_twig_jack: | ||
... | ||
constraint: true | ||
``` | ||
Use the constraint. | ||
```php | ||
use Boekkooi\Bundle\TwigJackBundle\Validator\Constraint as TwigAssert; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class MyClass { | ||
// ... | ||
|
||
/** | ||
* @Assert\NotBlank() | ||
* @TwigAssert\TwigSyntax() | ||
*/ | ||
public $template; | ||
|
||
/** | ||
* Check this variable based on syntax only and not availability of the methods, filters, etc. used | ||
* | ||
* @Assert\NotBlank() | ||
* @TwigAssert\TwigSyntax(parse=false) | ||
*/ | ||
public $remote_template; | ||
|
||
// ... | ||
} | ||
``` | ||
|
||
Full configuration | ||
------------- | ||
Default configuration: | ||
```yaml | ||
# app/config/config.yml | ||
boekkooi_twig_jack: | ||
constraint: | ||
enabled: true | ||
environment: 'twig' # Reference to the twig environment service to use by default. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
parameters: | ||
boekkooi.twig_jack.constraint_validator.class: Boekkooi\Bundle\TwigJackBundle\Validator\Constraint\TwigSyntaxValidator | ||
|
||
services: | ||
# defer extension | ||
boekkooi.twig_jack.constraint_validator: | ||
class: %boekkooi.twig_jack.constraint_validator.class% | ||
arguments: [ null ] | ||
tags: | ||
- { name: validator.constraint_validator, alias: 'TwigSyntaxValidator' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Boekkooi\Bundle\TwigJackBundle\Validator\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\InvalidOptionsException; | ||
|
||
/** | ||
* @author Warnar Boekkooi <[email protected]> | ||
*/ | ||
class TwigSyntax extends Constraint | ||
{ | ||
public $message = 'This value is not a valid twig template.'; | ||
public $parse = true; | ||
public $environment = null; | ||
|
||
public function __construct($options = null) | ||
{ | ||
parent::__construct($options); | ||
|
||
if ($this->environment !== null && !$this->environment instanceof \Twig_Environment) { | ||
throw new InvalidOptionsException(sprintf('Option "environment" must be null or a Twig_Environment instance for constraint %s', __CLASS__), array('environment')); | ||
} | ||
} | ||
|
||
public function validatedBy() | ||
{ | ||
return 'TwigSyntaxValidator'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace Boekkooi\Bundle\TwigJackBundle\Validator\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* @author Warnar Boekkooi <[email protected]> | ||
*/ | ||
class TwigSyntaxValidator extends ConstraintValidator | ||
{ | ||
protected $environment; | ||
|
||
public function __construct(\Twig_Environment $environment) | ||
{ | ||
$this->environment = $environment; | ||
} | ||
|
||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof TwigSyntax) { | ||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\TwigSyntax'); | ||
} | ||
|
||
if ($value === '' || $value === null) { | ||
return; | ||
} | ||
|
||
$env = $this->getEnvironment($constraint); | ||
try { | ||
$tokeStream = $env->tokenize($value); | ||
if ($constraint->parse) { | ||
$env->parse($tokeStream); | ||
} | ||
} catch (\Twig_Error_Syntax $e) { | ||
$this->context->addViolation($constraint->message, array( | ||
'{{ value }}' => $this->formatValue($value), | ||
)); | ||
} | ||
} | ||
|
||
protected function getEnvironment(TwigSyntax $constraint) | ||
{ | ||
$env = $constraint->environment; | ||
if ($env !== null) { | ||
return $env; | ||
} | ||
return $this->environment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.