|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: Sephy |
|
4
|
|
|
* Date: 06/06/2016 |
|
5
|
|
|
* Time: 02:04. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Core; |
|
8
|
|
|
|
|
9
|
|
|
use Core\ViewRenderes\BladeRenderer; |
|
10
|
|
|
use Core\ViewRenderes\TwigRenderer; |
|
11
|
|
|
|
|
12
|
|
|
class View |
|
13
|
|
|
{ |
|
14
|
|
|
private $renderer; |
|
15
|
|
|
private $template; |
|
16
|
|
|
private $params = []; |
|
17
|
|
|
|
|
18
|
|
|
private static $intance; |
|
19
|
|
|
|
|
20
|
|
|
public static function getInstance() |
|
21
|
|
|
{ |
|
22
|
|
|
if (!self::$intance) { |
|
23
|
|
|
self::$intance = (new self()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return self::$intance; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
$config = Config::getInstance(); |
|
32
|
|
|
$view_engine = $config->get('views.engine'); |
|
33
|
|
|
$view_folder = $config->get('views.path_views'); |
|
34
|
|
|
$view_cache = $config->get('views.path_cache'); |
|
35
|
|
|
|
|
36
|
|
|
$paths = new \SplPriorityQueue(); |
|
37
|
|
|
$paths->insert($view_folder, 1); |
|
38
|
|
|
|
|
39
|
|
|
switch ($view_engine) { |
|
40
|
|
|
case 'blade': |
|
41
|
|
|
$this->renderer = new BladeRenderer($paths, ['cache_path' => $view_cache]); |
|
42
|
|
|
break; |
|
43
|
|
|
case 'twig': |
|
44
|
|
|
$this->renderer = new TwigRenderer($paths); |
|
45
|
|
|
break; |
|
46
|
|
|
default: |
|
47
|
|
|
$this->renderer = new BladeRenderer($paths, $view_cache); |
|
48
|
|
|
break; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Render the view specified. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $view |
|
56
|
|
|
*/ |
|
57
|
|
|
public function render($view = null, $params = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$params = ($params) ? $params : $this->params; |
|
60
|
|
|
$view = $view ?: $this->template; |
|
61
|
|
|
echo $this->renderer->render($view, $params); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the view file to be rendered. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $template |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setView($template) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->template = $template; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Adds a parameter in the view. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $name |
|
78
|
|
|
* @param mixed $value |
|
79
|
|
|
*/ |
|
80
|
|
|
public function addParam($name, $value) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->params[$name] = $value; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Add array parameters in view, each position of the array will become a variable |
|
87
|
|
|
* available to the view. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function addParams($array) |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($array as $name => $value) { |
|
94
|
|
|
$this->addParam($name, $value); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the specified parameter. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* |
|
103
|
|
|
* @return void|mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getParam($name) |
|
106
|
|
|
{ |
|
107
|
|
|
if (isset($this->params[$name])) { |
|
108
|
|
|
return $this->params[$name]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|