Completed
Push — master ( 3aa992...fb367c )
by Asmir
11s queued 10s
created

Twital   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Test Coverage

Coverage 92.73%

Importance

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

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 compile() 0 23 2
A addExtension() 0 6 1
A setExtensions() 0 6 1
A getExtensions() 0 4 1
A initExtensions() 0 14 4
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
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
    /**
79
     *
80
     * @param SourceAdapter $adapter
81
     * @param string $source
82
     * @return string
83
     */
84 483
    public function compile(SourceAdapter $adapter, $source)
85
    {
86 483
        $this->initExtensions();
87
88 483
        $sourceEvent = new SourceEvent($this, $source);
89 483
        $this->dispatcher->dispatch(CompilerEvents::PRE_LOAD, $sourceEvent);
90 483
        $template = $adapter->load($sourceEvent->getTemplate());
91
92 483
        $templateEvent = new TemplateEvent($this, $template);
93 483
        $this->dispatcher->dispatch(CompilerEvents::POST_LOAD, $templateEvent);
94
95 483
        $compiler = new Compiler($this, isset($this->options['lexer']) ? $this->options['lexer'] : array());
96 483
        $compiler->compile($templateEvent->getTemplate()->getDocument());
97
98 481
        $templateEvent = new TemplateEvent($this, $templateEvent->getTemplate());
99 481
        $this->dispatcher->dispatch(CompilerEvents::PRE_DUMP, $templateEvent);
100 481
        $source = $adapter->dump($templateEvent->getTemplate());
101
102 481
        $sourceEvent = new SourceEvent($this, $source);
103 481
        $this->dispatcher->dispatch(CompilerEvents::POST_DUMP, $sourceEvent);
104
105 481
        return $sourceEvent->getTemplate();
106
    }
107
108 488
    public function addExtension(Extension $extension)
109
    {
110 488
        $this->extensionsInitialized = false;
111
112 488
        return $this->extensions[] = $extension;
113 1
    }
114
115
    public function setExtensions(array $extensions)
116
    {
117
        $this->extensionsInitialized = false;
118
119
        $this->extensions = $extensions;
120
    }
121
122
    /**
123
     * @return Extension[]
124
     */
125 484
    public function getExtensions()
126
    {
127 484
        return $this->extensions;
128
    }
129
130 484
    protected function initExtensions()
131
    {
132 484
        if (!$this->extensionsInitialized) {
133 484
            foreach ($this->getExtensions() as $extension) {
134 484
                $this->attributes = array_merge_recursive($this->attributes, $extension->getAttributes());
135 484
                $this->nodes = array_merge_recursive($this->nodes, $extension->getNodes());
136
137 484
                foreach ($extension->getSubscribers() as $subscriber) {
138 484
                    $this->dispatcher->addSubscriber($subscriber);
139 484
                }
140 484
            }
141 484
            $this->extensionsInitialized = true;
142 484
        }
143 484
    }
144
}
145