Completed
Push — master ( 00bb44...3aa992 )
by Asmir
04:54 queued 01:00
created

Twital   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Test Coverage

Coverage 92.59%

Importance

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