Php::render()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 43
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 43
rs 9.0111
1
<?php
2
namespace Darya\View;
3
4
/**
5
 * Darya's simple PHP view.
6
 *
7
 * @author Chris Andrew <[email protected]>
8
 */
9
class Php extends AbstractView
10
{
11
	/**
12
	 * Render the template.
13
	 *
14
	 * @return string
15
	 */
16
	public function render()
17
	{
18
		// Ensure that the directory exists
19
		if (!is_dir($this->directory)) {
20
			throw new \Exception("Could not find directory when rendering view: \"{$this->directory}\"");
21
		}
22
23
		// Ensure that the selected template file exists
24
		$path = $this->directory . '/' . $this->file;
25
26
		if (!is_file($path)) {
27
			throw new \Exception("Could not find file when rendering view: \"$path\"");
28
		}
29
30
		// Change the working directory
31
		$cwd = null;
32
33
		if ($this->directory) {
34
			$cwd = getcwd();
35
			chdir($this->directory);
36
		}
37
38
		// Change error reporting
39
		$error_reporting = error_reporting();
40
		error_reporting(error_reporting() & ~E_NOTICE & ~E_WARNING);
41
42
		// Extract view arguments
43
		extract($this->arguments);
44
45
		// Run the template
46
		ob_start();
47
		include $this->file;
48
		$output = ob_get_clean();
49
50
		// Restore the working directory
51
		if ($this->directory && $cwd) {
52
			chdir($cwd);
53
		}
54
55
		// Restore error reporting
56
		error_reporting($error_reporting);
57
58
		return $output;
59
	}
60
}
61