Completed
Pull Request — master (#58)
by Christophe
09:44 queued 06:23
created

processPlaceholder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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