Completed
Pull Request — master (#50)
by Martin
08:35 queued 06:21
created

AbstractTwigExpressionSubscriber::processTwig()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 22
cts 22
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 7
nop 2
crap 5
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 469
    public function __construct(array $placeholder = array('[_TWITAL_[', ']_TWITAL_]'), array $options = array())
19
    {
20 469
        $this->placeholderFormat = $placeholder[0] . '%s' . $placeholder[1];
21
22 469
        $options = array_merge(array(
23 469
            'tag_block' => array('{%', '%}'),
24 469
            'tag_variable' => array('{{', '}}'),
25 469
            'tag_comment' => array('{#', '#}'),
26 469
        ), $options);
27
28 469
        $this->regexes = array(
29 469
            'twig_start' => '{(' . preg_quote($options['tag_block'][0]) . '|' . preg_quote($options['tag_variable'][0]) . '|' . preg_quote($options['tag_comment'][0]) . ')}',
30 469
            'placeholder' => '{(' . preg_quote($placeholder[0]) . '(.+)' . preg_quote($placeholder[1]) . ')}siuU',
31 469
            'twig_inner_' . $options['tag_block'][0] => '{(' . self::REGEX_STRING . '|' . preg_quote($options['tag_block'][1]) . '|([^"\']*?' . preg_quote($options['tag_block'][1]) . ')|[^"\']+?)}si',
32 469
            'twig_inner_' . $options['tag_variable'][0] => '{(' . self::REGEX_STRING . '|' . preg_quote($options['tag_variable'][1]) . '|([^"\']*?' . preg_quote($options['tag_variable'][1]) . ')|[^"\']+?)}si',
33 469
            'twig_inner_' . $options['tag_comment'][0] => '{((.*?' . preg_quote($options['tag_comment'][1]) . '))}si',
34
        );
35 469
    }
36
37 468
    protected function processTwig($template, \CLosure $processor)
38
    {
39 468
        $offset = 0;
40 468
        while (preg_match($this->regexes['twig_start'], $template, $matches, PREG_OFFSET_CAPTURE, $offset)) {
41 128
            $twig = '';
42 128
            $buffer = $matches[0][0];
43 128
            $from = $matches[0][1];
44 128
            $offset = $from + strlen($buffer);
45 128
            $pattern = $this->regexes['twig_inner_' . $buffer];
46 128
            while (preg_match($pattern, $template, $inners, PREG_OFFSET_CAPTURE, $offset)) {
47 128
                $buffer .= $inners[0][0];
48 128
                $offset += strlen($inners[0][0]);
49 128
                if (isset($inners[2])) {
50 127
                    $twig = $buffer;
51 127
                    break;
52
                }
53 37
            };
54
55 128
            if (!$twig) {
56 1
                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 468
        return $template;
65
    }
66
67 466
    protected function processPlaceholder($template, \Closure $processor)
68
    {
69 466
        return preg_replace_callback($this->regexes['placeholder'], $processor, $template);
70
    }
71
}
72