Completed
Pull Request — master (#35)
by Martin
02:08
created

FixTwigExpressionSubscriber::addPlaceholder()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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