mageekguy\atoum\template\parser: lines coverage

96% of 469

OPs

100% of 80

Lines

80% of 61

Branches

6% of 320

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\template\parser::__construct() 27 100% 4 100% 1 0% 1 100%
mageekguy\atoum\template\parser::setNamespace() 10 100% 2 100% 1 100% 1 100%
mageekguy\atoum\template\parser::getNamespace() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\template\parser::setAdapter() 9 100% 2 100% 1 100% 1 100%
mageekguy\atoum\template\parser::getAdapter() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\template\parser::checkString() 11 100% 1 100% 1 100% 1 100%
mageekguy\atoum\template\parser::checkFile() 16 100% 1 100% 1 100% 1 100%
mageekguy\atoum\template\parser::parseString() 15 100% 2 100% 1 100% 1 100%
mageekguy\atoum\template\parser::parseFile() 19 100% 2 100% 1 100% 1 100%
mageekguy\atoum\template\parser::parse() 321 95% 59 100% 48 79% 309 2%
mageekguy\atoum\template\parser::getFileContents() 29 97% 5 100% 4 75% 2 100%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\template;
4

                    
5
use
6
	mageekguy\atoum,
7
	mageekguy\atoum\exceptions,
8
	mageekguy\atoum\template\parser
9
;
10

                    
11
class parser
12
{
13
	const eol = "\n";
14
	const defaultNamespace = 'tpl';
15

                    
16
	protected $namespace = '';
17
	protected $adapter = null;
18
	protected $errorLine = null;
19
	protected $errorOffset = null;
20
	protected $errorMessage = null;
21

                    
22
	public function __construct($namespace = null, atoum\adapter $adapter = null)100%
23
	{
24
		$this
25
			->setNamespace($namespace ?: self::defaultNamespace)
26
			->setAdapter($adapter ?: new atoum\adapter())
27
		;
28
	}
29

                    
30
	public function setNamespace($namespace)100%
31
	{
32
		$this->namespace = (string) $namespace;
33

                    
34
		return $this;
35
	}
36

                    
37
	public function getNamespace()100%
38
	{
39
		return $this->namespace;
40
	}
41

                    
42
	public function setAdapter(atoum\adapter $adapter)100%
43
	{
44
		$this->adapter = $adapter;
45

                    
46
		return $this;
47
	}
48

                    
49
	public function getAdapter()100%
50
	{
51
		return $this->adapter;
52
	}
53

                    
54
	public function checkString($string)100%
55
	{
56
		return $this->parse($string);
57
	}
58

                    
59
	public function checkFile($path)100%
60
	{
61
		return $this->checkString($this->getFileContents($path));
62
	}
63

                    
64
	public function parseString($string, atoum\template $root = null)100%
65
	{
66
		$this->parse((string) $string, $root);
67

                    
68
		return $root;
69
	}
70

                    
71
	public function parseFile($path, atoum\template $root = null)100%
72
	{
73
		$this->parse($this->getfileContents($path), $root);
74

                    
75
		return $root;
76
	}
77

                    
78
	protected function parse($string, & $root = null)100%
79
	{
80
		if ($root === null)
81
		{
82
			$root = new atoum\template();
83
		}
84

                    
85
		$currentTag = $root;
86

                    
87
		$stack = array();
88

                    
89
		$line = 1;
90
		$offset = 1;
91

                    
92
		while (preg_match('%<(/)?' . $this->namespace . ':([^\s/>]+)(?(1)\s*|((?:\s+\w+="[^"]*")*)\s*(/?))(>)%', $string, $tag, PREG_OFFSET_CAPTURE) == true)
93
		{
94
			if ($tag[0][1] != 0)
95
			{
96
				$data = substr($string, 0, $tag[0][1]);
97

                    
98
				$lastEol = strrpos($data, self::eol);
99

                    
100
				if ($lastEol === false)
101
				{
102
					$offset += strlen($data);
103
				}
104
				else
105
				{
106
					$line += substr_count($data, self::eol);
107
					$offset = strlen(substr($data, $lastEol));
108
				}
109

                    
110
				$currentTag->addChild(new data($data));
111
			}
112

                    
113
			$string = substr($string, $tag[5][1] + 1);
114

                    
115
			if ($tag[1][0] == '') # < /> or < > tag
116
			{
117
				$child = new tag($tag[2][0], null, $line, $offset);
118

                    
119
				$currentTag->addChild($child);
120

                    
121
				if (preg_match_all('%(\w+)="([^"]*)"%', $tag[3][0], $attributes) == true)
122
				{
123
					foreach ($attributes[1] as $index => $attribute)
124
					{
125
						try
126
						{
127
							$child->setAttribute($attribute, $attributes[2][$index]);
128
						}
129
						catch (\exception $exception)
130
						{
131
							throw new parser\exception($exception->getMessage(), $line, $offset, $exception);
132
						}
133
					}
134
				}
135

                    
136
				if ($tag[4][0] == '') # < >
137
				{
138
					$stack[] = $child;
139
					$currentTag = $child;
140
				}
141
			}
142
			else # </ >
143
			{
144
				$stackedTemplateTag = array_pop($stack);
145

                    
146
				if ($stackedTemplateTag === null || $stackedTemplateTag->getTag() != $tag[2][0])
147
				{
148
					throw new parser\exception('Tag \'' . $tag[2][0] . '\' is not open', $line, $offset);
149
				}
150
				else
151
				{
152
					$currentTag = end($stack);
153

                    
154
					if ($currentTag === false)
155
					{
156
						$currentTag = $root;
157
					}
158
				}
159
			}
160

                    
161
			$offset += ($tag[5][1] - $tag[0][1]) + 1;
162
		}
163

                    
164
		if ($string != '')
165
		{
166
			$currentTag->addChild(new data($string));
167
		}
168

                    
169
		if (sizeof($stack) > 0)
170
		{
171
			throw new parser\exception('Tag \'' . $currentTag->getTag() . '\' must be closed', $line, $offset + strlen($string));
172
		}
173

                    
174
		return $this;
175
	}
176

                    
177
	protected function getFileContents($path)100%
178
	{
179
		$fileContents = $this->adapter->file_get_contents($path);
180

                    
181
		if ($fileContents === false)
182
		{
183
			throw new exceptions\runtime('Unable to get contents from file \'' . $path . '\'');
184
		}
185

                    
186
		return $fileContents;
187
	}
188
}