Completed
Push — twig-2 ( 00bf9b...3d05b1 )
by Asmir
05:31
created

Twital   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
cbo 8
dl 0
loc 130
ccs 49
cts 53
cp 0.9245
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getEventDispatcher() 0 6 1
A getNodes() 0 6 1
A initExtensions() 0 14 4
A getAttributes() 0 6 1
A compile() 0 23 2
A addExtension() 0 6 1
A setExtensions() 0 6 1
A getExtensions() 0 4 1
1
<?php
2
namespace Goetas\Twital;
3
4
use Goetas\Twital\Extension\CoreExtension;
5
use Symfony\Component\EventDispatcher\EventDispatcher;
6
use Goetas\Twital\EventDispatcher\SourceEvent;
7
use Goetas\Twital\EventDispatcher\TemplateEvent;
8
9
/**
10
 *
11
 * @author Asmir Mustafic <[email protected]>
12
 *
13
 */
14
class Twital
15
{
16
    const NS = 'urn:goetas:twital';
17
18
    protected $extensionsInitialized = false;
19
20
    /**
21
     *
22
     * @var EventDispatcher
23
     */
24
    protected $dispatcher;
25
26
    /**
27
     *
28
     * @var array
29
     */
30
    private $attributes = array();
31
32
    /**
33
     *
34
     * @var array
35
     */
36
    private $nodes = array();
37
38
    /**
39
     *
40
     * @var array
41
     */
42
    private $extensions = array();
43
44 468
    public function __construct(array $options = array())
45
    {
46 468
        $this->options = $options;
47 468
        $this->dispatcher = new EventDispatcher();
48
49 468
        $this->addExtension(new CoreExtension());
50 468
    }
51
52
    /**
53
     *
54
     * @return \Symfony\Component\EventDispatcher\EventDispatcher
55
     */
56 22
    public function getEventDispatcher()
57
    {
58 22
        $this->initExtensions();
59
60 22
        return $this->dispatcher;
61
    }
62
63 461
    public function getNodes()
64 1
    {
65 461
        $this->initExtensions();
66
67 461
        return $this->nodes;
68 1
    }
69
70 428
    public function getAttributes()
71 1
    {
72 428
        $this->initExtensions();
73
74 428
        return $this->attributes;
75
    }
76
77 462
    protected function initExtensions()
78
    {
79 462
        if (!$this->extensionsInitialized) {
80 462
            foreach ($this->getExtensions() as $extension) {
81 462
                $this->attributes = array_merge_recursive($this->attributes, $extension->getAttributes());
82 462
                $this->nodes = array_merge_recursive($this->nodes, $extension->getNodes());
83
84 462
                foreach ($extension->getSubscribers() as $subscriber) {
85 462
                    $this->dispatcher->addSubscriber($subscriber);
86 462
                }
87 462
            }
88 462
            $this->extensionsInitialized = true;
89 462
        }
90 462
    }
91
92
    /**
93
     *
94
     * @param SourceAdapter $adapter
95
     * @param string $source
96
     * @return string
97
     */
98 461
    public function compile(SourceAdapter $adapter, $source)
99
    {
100 461
        $this->initExtensions();
101
102 461
        $sourceEvent = new SourceEvent($this, $source);
103 461
        $this->dispatcher->dispatch('compiler.pre_load', $sourceEvent);
104 461
        $template = $adapter->load($sourceEvent->getTemplate());
105
106 461
        $templateEvent = new TemplateEvent($this, $template);
107 461
        $this->dispatcher->dispatch('compiler.post_load', $templateEvent);
108
109 461
        $compiler = new Compiler($this, isset($this->options['lexer']) ? $this->options['lexer'] : array());
110 461
        $compiler->compile($templateEvent->getTemplate()->getDocument());
111
112 459
        $templateEvent = new TemplateEvent($this, $templateEvent->getTemplate());
113 460
        $this->dispatcher->dispatch('compiler.pre_dump', $templateEvent);
114 459
        $source = $adapter->dump($templateEvent->getTemplate());
115
116 459
        $sourceEvent = new SourceEvent($this, $source);
117 459
        $this->dispatcher->dispatch('compiler.post_dump', $sourceEvent);
118
119 459
        return $sourceEvent->getTemplate();
120
    }
121
122 466
    public function addExtension(Extension $extension)
123
    {
124 466
        $this->extensionsInitialized = false;
125
126 466
        return $this->extensions[] = $extension;
127
    }
128
129
    public function setExtensions(array $extensions)
130
    {
131
        $this->extensionsInitialized = false;
132
133
        $this->extensions = $extensions;
134
    }
135
136
    /**
137
     * @return Extension[]
138
     */
139 462
    public function getExtensions()
140
    {
141 462
        return $this->extensions;
142
    }
143
}
144