Test Failed
Pull Request — master (#313)
by Jean-Christophe
28:33
created

MicroTemplateEngine::addFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Ubiquity\views\engine\micro;
4
5
use Ubiquity\utils\base\UFileSystem;
6
use Ubiquity\views\engine\TemplateEngine;
7
use Ubiquity\views\engine\TemplateGenerator;
8
9
class MicroTemplateEngine extends TemplateEngine {
10
	private string $viewsFolder;
11
	private array $parsers = [];
12
13
	private function getTemplateParser(string $viewName): TemplateParser {
14
		if (!isset ($this->parsers [$viewName])) {
15
			$this->parsers [$viewName] = new TemplateParser ($this->viewsFolder . $viewName);
16
		}
17
		return $this->parsers [$viewName];
18
	}
19
20
	public function __construct() {
21
		$this->viewsFolder = \ROOT . \DS . 'views' . \DS;
22
	}
23
24
	/*
25
	 * (non-PHPdoc)
26
	 * @see TemplateEngine::render()
27
	 */
28
	public function render(string $viewName, ?array $pData = [], bool $asString = false) {
29
		if (\is_array($pData)) {
0 ignored issues
show
introduced by
The condition is_array($pData) is always true.
Loading history...
30
			\extract($pData);
31
		}
32
		$content = eval ('?>' . $this->getTemplateParser($viewName)->__toString());
33
		if ($asString) {
34
			return $content;
35
		}
36
		echo $content;
37
	}
38
39
	public function getBlockNames(string $templateName): array {
40
		return [];
41
	}
42
43
	public function getCode(string $templateName): string {
44
		$fileName = $this->viewsFolder . $templateName;
45
		return UFileSystem::load($fileName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Ubiquity\utils\ba...System::load($fileName) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
46
	}
47
48
	public function exists(string $name): bool {
49
		$filename = $this->viewsFolder . $name;
50
		return \file_exists($filename);
51
	}
52
53
	public function addFunction(string $name, $callback, array $options = []): void {
54
		throw new \BadMethodCallException('addFunction method has no sense with MicroTemplateEngine');
55
	}
56
57
	protected function addFilter(string $name, $callback, array $options = []): void {
58
		throw new \BadMethodCallException('addFilter method has no sense with MicroTemplateEngine');
59
	}
60
61
	protected function addExtension($extension): void {
62
		throw new \BadMethodCallException('addExtension method has no sense with MicroTemplateEngine');
63
	}
64
65
	public function getGenerator(): ?TemplateGenerator {
66
		return null;
67
	}
68
}
69