FixTwigExpressionSubscriber::addPlaceholder()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 9.2248
c 0
b 0
f 0
cc 5
nc 1
nop 1
crap 5
1
<?php
2
namespace Goetas\Twital\EventSubscriber;
3
4
use Goetas\Twital\EventDispatcher\CompilerEvents;
5
use Goetas\Twital\EventDispatcher\SourceEvent;
6
7
/**
8
 *
9
 * @author Martin Hasoň <[email protected]>
10
 *
11
 */
12
class FixTwigExpressionSubscriber extends AbstractTwigExpressionSubscriber
13
{
14
    protected $placeholders = array();
15
16 25
    public function __construct($placeholder = array('twital', 'twital'), array $options = array())
17
    {
18 25
        parent::__construct($placeholder, $options);
19
20 25
        $this->regexes = array_merge($this->regexes, array(
21 25
            'placeholder' => '{( ?)(' . preg_quote($placeholder[0]) . '[a-z0-9]+?' . preg_quote($placeholder[1]) . ')}iu',
22 25
        ));
23 25
    }
24
25 25
    public static function getSubscribedEvents()
26
    {
27
        return array(
28 25
            CompilerEvents::PRE_LOAD => array('addPlaceholder', 128),
29 25
            CompilerEvents::POST_DUMP => array('removePlaceholder', -128),
30 25
        );
31
    }
32
33
    /**
34
     *
35
     * @param SourceEvent $event
36
     */
37 24
    public function addPlaceholder(SourceEvent $event)
38
    {
39 24
        $source = $event->getTemplate();
40 24
        $format = $this->placeholderFormat;
41 24
        $placeholders = array();
42
43
        $source = $this->processTwig($source, function ($twig, $source, $offset) use ($format, &$placeholders) {
44 24
            $before = $offset > 0 ? $source[$offset - 1] : '';
45 24
            $id = ('<' === $before || '/' === $before) ? $twig : mt_rand();
46 24
            $placeholder = sprintf($format, md5($id));
47
48 24
            if (!in_array($before, array(' ', '<', '>', '/'), true)) {
49 12
                $placeholder = ' ' . $placeholder;
50 12
            }
51
52 24
            $placeholders[$placeholder] = $twig;
53
54 24
            return $placeholder;
55 24
        });
56
57 24
        $this->placeholders = $placeholders;
58
59 24
        $event->setTemplate($source);
60 24
    }
61
62
    /**
63
     *
64
     * @param SourceEvent $event
65
     */
66 24
    public function removePlaceholder(SourceEvent $event)
67
    {
68 24
        $source = $event->getTemplate();
69
70 24
        $placeholders = $this->placeholders;
71
72 24
        $source = $this->processPlaceholder($source, function ($matches) use ($placeholders) {
73 24
            if (isset($placeholders[$matches[0]])) {
74 22
                return $placeholders[$matches[0]];
75 9
            } elseif (isset($placeholders[$matches[2]])) {
76 9
                return $matches[1] . $placeholders[$matches[2]];
77
            } else {
78
                return $matches[0];
79
            }
80 24
        });
81
82 24
        $event->setTemplate($source);
83 24
    }
84
}
85