Twital   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Test Coverage

Coverage 92.73%

Importance

Changes 0
Metric Value
wmc 15
cbo 8
dl 0
loc 139
c 0
b 0
f 0
ccs 51
cts 55
cp 0.9273
rs 10

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