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

FixTwigExpressionSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 97.44%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 9
c 3
b 1
f 1
cbo 2
dl 0
loc 74
ccs 38
cts 39
cp 0.9744
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A __construct() 0 9 1
B addPlaceholder() 0 24 4
A removePlaceholder() 0 18 3
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
            $id = ('<' === $startChar || '/' === $startChar) ? $matches[2] : mt_rand();
46 20
            $placeholder = sprintf($format, md5($id));
47
48 20
            if (!in_array($startChar, array(' ', '<', '>', '/'), true)) {
49 9
                $placeholder = ' '.$placeholder;
50 9
            }
51
52 20
            $placeholders[$placeholder] = $matches[2];
53
54 20
            return $startChar.$placeholder;
55 20
        }, $source);
56
57 20
        $this->placeholders = $placeholders;
58
59 20
        $event->setTemplate($source);
60 20
    }
61
62
    /**
63
     *
64
     * @param SourceEvent $event
65
     */
66 20
    public function removePlaceholder(SourceEvent $event)
67
    {
68 20
        $source = $event->getTemplate();
69
70 20
        $placeholders = $this->placeholders;
71
72 20
        $source = preg_replace_callback($this->regexes['placeholder'], function($matches) use ($placeholders) {
73 20
            if (isset($placeholders[$matches[0]])) {
74 18
                return $placeholders[$matches[0]];
75 8
            } elseif (isset($placeholders[$matches[2]])) {
76 8
                return $matches[1].$placeholders[$matches[2]];
77
            } else {
78
                return $matches[0];
79
            }
80 20
        }, $source);
81
82 20
        $event->setTemplate($source);
83 20
    }
84
}
85