Completed
Pull Request — master (#44)
by Asmir
27:20 queued 25:30
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 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
A getEventDispatcher() 0 6 1
A getNodes() 0 6 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 474
    public function __construct(array $options = array())
45
    {
46 474
        $this->options = $options;
47 474
        $this->dispatcher = new EventDispatcher();
48
49 474
        $this->addExtension(new CoreExtension());
50 474
    }
51
52
    /**
53
     *
54
     * @return \Symfony\Component\EventDispatcher\EventDispatcher
55
     */
56 22
    public function getEventDispatcher()
57 1
    {
58 22
        $this->initExtensions();
59
60 22
        return $this->dispatcher;
61
    }
62
63 467
    public function getNodes()
64
    {
65 467
        $this->initExtensions();
66
67 467
        return $this->nodes;
68 1
    }
69
70 434
    public function getAttributes()
71 1
    {
72 434
        $this->initExtensions();
73
74 434
        return $this->attributes;
75
    }
76
77 468
    protected function initExtensions()
78
    {
79 468
        if (!$this->extensionsInitialized) {
80 468
            foreach ($this->getExtensions() as $extension) {
81 468
                $this->attributes = array_merge_recursive($this->attributes, $extension->getAttributes());
82 468
                $this->nodes = array_merge_recursive($this->nodes, $extension->getNodes());
83
84 468
                foreach ($extension->getSubscribers() as $subscriber) {
85 468
                    $this->dispatcher->addSubscriber($subscriber);
86 468
                }
87 468
            }
88 468
            $this->extensionsInitialized = true;
89 468
        }
90 468
    }
91
92
    /**
93
     *
94
     * @param SourceAdapter $adapter
95
     * @param string $source
96
     * @return string
97
     */
98 467
    public function compile(SourceAdapter $adapter, $source)
99
    {
100 467
        $this->initExtensions();
101
102 467
        $sourceEvent = new SourceEvent($this, $source);
103 467
        $this->dispatcher->dispatch('compiler.pre_load', $sourceEvent);
104 467
        $template = $adapter->load($sourceEvent->getTemplate());
105
106 467
        $templateEvent = new TemplateEvent($this, $template);
107 467
        $this->dispatcher->dispatch('compiler.post_load', $templateEvent);
108
109 467
        $compiler = new Compiler($this, isset($this->options['lexer']) ? $this->options['lexer'] : array());
110 467
        $compiler->compile($templateEvent->getTemplate()->getDocument());
111
112 465
        $templateEvent = new TemplateEvent($this, $templateEvent->getTemplate());
113 466
        $this->dispatcher->dispatch('compiler.pre_dump', $templateEvent);
114 465
        $source = $adapter->dump($templateEvent->getTemplate());
115
116 465
        $sourceEvent = new SourceEvent($this, $source);
117 465
        $this->dispatcher->dispatch('compiler.post_dump', $sourceEvent);
118
119 465
        return $sourceEvent->getTemplate();
120
    }
121
122 472
    public function addExtension(Extension $extension)
123
    {
124 472
        $this->extensionsInitialized = false;
125
126 472
        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 468
    public function getExtensions()
140
    {
141 468
        return $this->extensions;
142
    }
143
}
144