|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Renderer\Adapters; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\Factories\FileSystemFactory; |
|
18
|
|
|
use Quantum\Renderer\Contracts\TemplateRendererInterface; |
|
19
|
|
|
use Quantum\Renderer\Exceptions\RendererException; |
|
20
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
|
21
|
|
|
use Quantum\Di\Exceptions\DiException; |
|
22
|
|
|
use Quantum\Exceptions\BaseException; |
|
23
|
|
|
use ReflectionException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class HtmlAdapter |
|
27
|
|
|
* @package Quantum\Renderer |
|
28
|
|
|
*/ |
|
29
|
|
|
class HtmlAdapter implements TemplateRendererInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var FileSystem |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $fs; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $configs; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array|null $configs |
|
44
|
|
|
* @throws DiException |
|
45
|
|
|
* @throws ReflectionException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(?array $configs = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->configs = $configs; |
|
50
|
|
|
|
|
51
|
|
|
$this->fs = FileSystemFactory::get(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Renders the view |
|
56
|
|
|
* @param string $view |
|
57
|
|
|
* @param array $params |
|
58
|
|
|
* @return string |
|
59
|
|
|
* @throws BaseException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function render(string $view, array $params = []): string |
|
62
|
|
|
{ |
|
63
|
|
|
$filePath = $this->getViewFilePath($view); |
|
64
|
|
|
|
|
65
|
|
|
if (!$this->fs->exists($filePath)) { |
|
66
|
|
|
throw RendererException::fileNotFound($view); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
ob_start(); |
|
70
|
|
|
ob_implicit_flush(0); |
|
71
|
|
|
|
|
72
|
|
|
if (!empty($params)) { |
|
73
|
|
|
extract($params); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
require $filePath; |
|
77
|
|
|
|
|
78
|
|
|
return ob_get_clean(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $view |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getViewFilePath(string $view): string |
|
86
|
|
|
{ |
|
87
|
|
|
$moduleViewPath = modules_dir() . DS . current_module() . DS . 'Views' . DS . $view . '.php'; |
|
88
|
|
|
$sharedViewPath = base_dir() . DS . 'shared' . DS . 'views' . DS . $view . '.php'; |
|
89
|
|
|
|
|
90
|
|
|
return $this->fs->exists($moduleViewPath) ? $moduleViewPath : $sharedViewPath; |
|
91
|
|
|
} |
|
92
|
|
|
} |