Passed
Push — master ( 3d04cc...319518 )
by Jean-Christophe
23:03 queued 11:07
created

MicroTemplateEngine   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 19
dl 0
loc 42
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 3
A getBlockNames() 0 2 1
A getTemplateParser() 0 5 2
A getCode() 0 3 1
A exists() 0 3 1
A __construct() 0 2 1
1
<?php
2
3
namespace Ubiquity\views\engine\micro;
4
5
use Ubiquity\views\engine\TemplateEngine;
6
use Ubiquity\utils\base\UFileSystem;
7
8
class MicroTemplateEngine extends TemplateEngine {
9
	private $viewsFolder;
10
	private $parsers = [ ];
11
12
	private function getTemplateParser(string $viewName): TemplateParser {
13
		if (! isset ( $this->parsers [$viewName] )) {
14
			$this->parsers [$viewName] = new TemplateParser ( $this->viewsFolder . $viewName );
15
		}
16
		return $this->parsers [$viewName];
17
	}
18
19
	public function __construct() {
20
		$this->viewsFolder = \ROOT . \DS . 'views' . \DS;
21
	}
22
23
	/*
24
	 * (non-PHPdoc)
25
	 * @see TemplateEngine::render()
26
	 */
27
	public function render($viewName, $pData, $asString) {
28
		if (\is_array ( $pData )) {
29
			\extract ( $pData );
30
		}
31
		$content = eval ( '?>' . $this->getTemplateParser ( $viewName )->__toString () );
1 ignored issue
show
introduced by
The use of eval() is discouraged.
Loading history...
32
		if ($asString)
33
			return $content;
34
		else
35
			echo $content;
36
	}
37
38
	public function getBlockNames($templateName) {
39
		return [ ];
40
	}
41
42
	public function getCode($templateName) {
43
		$fileName = $this->viewsFolder . $templateName;
44
		return UFileSystem::load ( $fileName );
45
	}
46
47
	public function exists($name) {
48
		$filename = $this->viewsFolder . $name;
49
		return \file_exists ( $filename );
50
	}
51
}
52