1
|
|
|
<?php |
2
|
|
|
namespace Goetas\Twital\EventSubscriber; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* |
8
|
|
|
* @author Martin Hasoň <[email protected]> |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractTwigExpressionSubscriber implements EventSubscriberInterface |
12
|
|
|
{ |
13
|
|
|
const REGEX_STRING = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; |
14
|
|
|
|
15
|
|
|
protected $placeholderFormat = ''; |
16
|
|
|
protected $options = array(); |
17
|
|
|
protected $regexes = array(); |
18
|
487 |
|
|
19
|
|
|
public function __construct(array $placeholder = array('[_TWITAL_[', ']_TWITAL_]'), array $options = array()) |
20
|
487 |
|
{ |
21
|
|
|
$this->placeholderFormat = $placeholder[0] . '%s' . $placeholder[1]; |
22
|
487 |
|
|
23
|
487 |
|
$this->options = array_merge(array( |
24
|
487 |
|
'tag_block' => array('{%', '%}'), |
25
|
487 |
|
'tag_variable' => array('{{', '}}'), |
26
|
487 |
|
'tag_comment' => array('{#', '#}'), |
27
|
|
|
), $options); |
28
|
487 |
|
|
29
|
487 |
|
$this->regexes = array( |
30
|
487 |
|
'twig_start' => '{('.preg_quote($this->options['tag_block'][0]).'|'.preg_quote($this->options['tag_variable'][0]).'|'.preg_quote($this->options['tag_comment'][0]).')}', |
31
|
487 |
|
'placeholder' => '{('.preg_quote($placeholder[0]).'(.+)'.preg_quote($placeholder[1]).')}siuU', |
32
|
487 |
|
'twig_inner_'.$this->options['tag_block'][0] => '{('.self::REGEX_STRING.'|([^"\']*?'.preg_quote($this->options['tag_block'][1]).')|[^"\']+?)}si', |
33
|
487 |
|
'twig_inner_'.$this->options['tag_variable'][0] => '{('.self::REGEX_STRING.'|([^"\']*?'.preg_quote($this->options['tag_variable'][1]).')|[^"\']+?)}si', |
34
|
|
|
'twig_inner_'.$this->options['tag_comment'][0] => '{((.*?'.preg_quote($this->options['tag_comment'][1]).'))}si', |
35
|
487 |
|
); |
36
|
|
|
} |
37
|
486 |
|
|
38
|
|
|
protected function processTwig($template, \CLosure $processor) |
39
|
486 |
|
{ |
40
|
486 |
|
$offset = 0; |
41
|
133 |
|
while (preg_match($this->regexes['twig_start'], $template, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
42
|
133 |
|
$twig = ''; |
43
|
133 |
|
list($buffer, $from) = $matches[0]; |
44
|
133 |
|
$offset = $from + strlen($buffer); |
45
|
133 |
|
$pattern = $this->regexes['twig_inner_' . $buffer]; |
46
|
133 |
|
while (preg_match($pattern, $template, $inners, PREG_OFFSET_CAPTURE, $offset)) { |
47
|
133 |
|
$buffer .= $inners[0][0]; |
48
|
133 |
|
$offset += strlen($inners[0][0]); |
49
|
133 |
|
if (isset($inners[2])) { |
50
|
133 |
|
$twig = $buffer; |
51
|
133 |
|
break; |
52
|
|
|
} |
53
|
41 |
|
} |
54
|
|
|
|
55
|
133 |
|
if (!$twig) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
133 |
|
$replacement = $processor($twig, $template, $from); |
60
|
133 |
|
$template = substr_replace($template, $replacement, $from, $offset - $from); |
61
|
133 |
|
$offset = $from + strlen($replacement); |
62
|
133 |
|
} |
63
|
|
|
|
64
|
486 |
|
return $template; |
65
|
|
|
} |
66
|
|
|
|
67
|
484 |
|
protected function processPlaceholder($template, \Closure $processor) |
68
|
|
|
{ |
69
|
484 |
|
return preg_replace_callback($this->regexes['placeholder'], $processor, $template); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|