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 $regexes = array(); |
17
|
|
|
|
18
|
448 |
|
public function __construct(array $placeholder = array('[_TWITAL_[', ']_TWITAL_]'), array $options = array()) |
19
|
|
|
{ |
20
|
448 |
|
$this->placeholderFormat = $placeholder[0].'%s'.$placeholder[1]; |
21
|
|
|
|
22
|
448 |
|
$options = array_merge(array( |
23
|
448 |
|
'tag_block' => array('{%', '%}'), |
24
|
448 |
|
'tag_variable' => array('{{', '}}'), |
25
|
448 |
|
'tag_comment' => array('{#', '#}'), |
26
|
448 |
|
), $options); |
27
|
|
|
|
28
|
448 |
|
$this->regexes = array( |
29
|
448 |
|
'twig_start' => '{('.preg_quote($options['tag_block'][0]).'|'.preg_quote($options['tag_variable'][0]).'|'.preg_quote($options['tag_comment'][0]).')}', |
30
|
448 |
|
'placeholder' => '{('.preg_quote($placeholder[0]).'(.+)'.preg_quote($placeholder[1]).')}siuU', |
31
|
448 |
|
'twig_inner_'.$options['tag_block'][0] => '{('.self::REGEX_STRING.'|'.preg_quote($options['tag_block'][1]).'|([^"\']*?'.preg_quote($options['tag_block'][1]).')|[^"\']*)}si', |
32
|
448 |
|
'twig_inner_'.$options['tag_variable'][0] => '{('.self::REGEX_STRING.'|'.preg_quote($options['tag_variable'][1]).'|([^"\']*?'.preg_quote($options['tag_variable'][1]).')|[^"\']*)}si', |
33
|
448 |
|
'twig_inner_'.$options['tag_comment'][0] => '{((.*?'.preg_quote($options['tag_comment'][1]).'))}si', |
34
|
|
|
); |
35
|
448 |
|
} |
36
|
|
|
|
37
|
447 |
|
protected function processTwig($template, \CLosure $processor) |
38
|
|
|
{ |
39
|
447 |
|
$offset = 0; |
40
|
447 |
|
while (preg_match($this->regexes['twig_start'], $template, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
41
|
127 |
|
$twig = ''; |
42
|
127 |
|
$buffer = $matches[0][0]; |
43
|
127 |
|
$from = $matches[0][1]; |
44
|
127 |
|
$offset = $from + strlen($buffer); |
45
|
127 |
|
$pattern = $this->regexes['twig_inner_'.$buffer]; |
46
|
127 |
|
while (preg_match($pattern, $template, $inners, PREG_OFFSET_CAPTURE, $offset)) { |
47
|
127 |
|
$buffer .= $inners[0][0]; |
48
|
127 |
|
$offset += strlen($inners[0][0]); |
49
|
127 |
|
if (isset($inners[2])) { |
50
|
127 |
|
$twig = $buffer; |
51
|
127 |
|
break; |
52
|
|
|
} |
53
|
36 |
|
}; |
54
|
|
|
|
55
|
127 |
|
if (!$twig) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
127 |
|
$replacement = $processor($twig, $template, $from); |
60
|
127 |
|
$template = substr_replace($template, $replacement, $from, $offset - $from); |
61
|
127 |
|
$offset = $from + strlen($replacement); |
62
|
127 |
|
} |
63
|
|
|
|
64
|
447 |
|
return $template; |
65
|
|
|
} |
66
|
|
|
|
67
|
445 |
|
protected function processPlaceholder($template, \Closure $processor) |
68
|
|
|
{ |
69
|
445 |
|
return preg_replace_callback($this->regexes['placeholder'], $processor, $template); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|