1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\View; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\View\Converter\BasicConvert; |
5
|
|
|
use JayaCode\Framework\Core\View\Converter\Converter; |
6
|
|
|
use JayaCode\Framework\Core\View\Template\BasicTemplate; |
7
|
|
|
use JayaCode\Framework\Core\View\Template\Template; |
8
|
|
|
use JayaCode\Framework\Core\View\VariableCollector\BasicVariableCollector; |
9
|
|
|
use JayaCode\Framework\Core\View\VariableCollector\VariableCollector; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class View |
13
|
|
|
* @property VariableCollector vars |
14
|
|
|
* @package JayaCode\Framework\Core\View |
15
|
|
|
*/ |
16
|
|
|
class View |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var VariableCollector |
20
|
|
|
*/ |
21
|
|
|
protected $varCollector; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $options = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
6 |
|
* @var Converter |
30
|
|
|
*/ |
31
|
6 |
|
protected $converter; |
32
|
|
|
|
33
|
6 |
|
/** |
34
|
|
|
* View constructor. |
35
|
|
|
* @param string $viewDir |
36
|
6 |
|
* @param $vars |
37
|
|
|
* @param array $options |
38
|
|
|
* @throws \Exception |
39
|
6 |
|
*/ |
40
|
|
|
public function __construct($viewDir = "/", $vars = null, $options = array()) |
41
|
6 |
|
{ |
42
|
|
|
|
43
|
6 |
|
$this->options = $options + [ |
44
|
6 |
|
"extension" => ".vj", |
45
|
|
|
"variableCollectionClass" => BasicVariableCollector::class, |
46
|
|
|
"templateClass" => BasicTemplate::class, |
47
|
|
|
"converter" => BasicConvert::class, |
48
|
|
|
"cacheDir" => null |
49
|
6 |
|
]; |
50
|
|
|
|
51
|
6 |
|
$this->varCollector = new $this->options["variableCollectionClass"](); |
52
|
6 |
|
$this->converter = new $this->options["converter"](); |
53
|
|
|
|
54
|
6 |
|
Template::$extension = $this->options["extension"]; |
55
|
6 |
|
|
56
|
|
|
$this->setViewDir($viewDir); |
57
|
|
|
if ($vars) { |
58
|
|
|
$this->varCollector->add($vars); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $viewDir |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
|
|
public function setViewDir($viewDir) |
67
|
|
|
{ |
68
|
|
|
if (!is_dir($viewDir)) { |
69
|
|
|
throw new \Exception("not found directory {$viewDir}"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->options['viewDir'] = rtrim($viewDir, "/") . "/"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $name |
77
|
|
|
* @return VariableCollector|null |
78
|
|
|
*/ |
79
|
|
|
public function __get($name) |
80
|
|
|
{ |
81
|
|
|
return ($name == "vars")?$this->varCollector:null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $fileTemplate |
86
|
|
|
* @return Template |
87
|
|
|
*/ |
88
|
|
|
public function template($fileTemplate) |
89
|
|
|
{ |
90
|
|
|
$fileTemplateReal = str_replace(".", "/", $fileTemplate); |
91
|
|
|
$fileTemplateReal .= $this->options["extension"]; |
92
|
|
|
|
93
|
|
|
return new $this->options["templateClass"]($this->options["viewDir"], $fileTemplateReal, |
94
|
|
|
$this->varCollector, $this->converter, $this->options['cacheDir']); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|