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

FixTwigExpressionSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 97.37%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 10
c 3
b 1
f 1
cbo 2
dl 0
loc 73
ccs 37
cts 38
cp 0.9737
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A __construct() 0 8 1
B addPlaceholder() 0 24 5
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 22
    public static function getSubscribedEvents()
14
    {
15
        return array(
16 22
            'compiler.pre_load' => array('addPlaceholder', 128),
17 22
            'compiler.post_dump' => array('removePlaceholder', -128),
18 22
        );
19
    }
20
21
    protected $placeholders = array();
22
23 22
    public function __construct($placeholder = array('twital', 'twital'), array $options = array())
24
    {
25 22
        parent::__construct($placeholder, $options);
26
27 22
        $this->regexes = array_merge($this->regexes, array(
28 22
            'placeholder' => '{( ?)('.preg_quote($placeholder[0]).'[a-z0-9]+?'.preg_quote($placeholder[1]).')}iu',
29 22
        ));
30 22
    }
31
32
    /**
33
     *
34
     * @param SourceEvent $event
35
     */
36 21
    public function addPlaceholder(SourceEvent $event)
37
    {
38 21
        $source = $event->getTemplate();
39 21
        $format = $this->placeholderFormat;
40 21
        $placeholders = array();
41
42
        $source = $this->processTwig($source, function ($twig, $source, $offset) use ($format, &$placeholders) {
43 21
            $before = $offset > 0 ? $source[$offset - 1] : '';
44 21
            $id = ('<' === $before || '/' === $before) ? $twig : mt_rand();
45 21
            $placeholder = sprintf($format, md5($id));
46
47 21
            if (!in_array($before, array(' ', '<', '>', '/'), true)) {
48 10
                $placeholder = ' '.$placeholder;
49 10
            }
50
51 21
            $placeholders[$placeholder] = $twig;
52
53 21
            return $placeholder;
54 21
        });
55
56 21
        $this->placeholders = $placeholders;
57
58 21
        $event->setTemplate($source);
59 21
    }
60
61
    /**
62
     *
63
     * @param SourceEvent $event
64
     */
65 21
    public function removePlaceholder(SourceEvent $event)
66
    {
67 21
        $source = $event->getTemplate();
68
69 21
        $placeholders = $this->placeholders;
70
71 21
        $source = $this->processPlaceholder($source, function($matches) use ($placeholders) {
72 21
            if (isset($placeholders[$matches[0]])) {
73 19
                return $placeholders[$matches[0]];
74 9
            } elseif (isset($placeholders[$matches[2]])) {
75 9
                return $matches[1].$placeholders[$matches[2]];
76
            } else {
77
                return $matches[0];
78
            }
79 21
        });
80
81 21
        $event->setTemplate($source);
82 21
    }
83
}
84