|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Mvc; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* View class |
|
15
|
|
|
* |
|
16
|
|
|
* This class manages views and parameters |
|
17
|
|
|
*/ |
|
18
|
|
|
class View |
|
19
|
|
|
{ |
|
20
|
|
|
use \Drone\Util\ParamTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* View name |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $name; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The path where views are located. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $path; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the view name |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getName() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->name; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the path |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getPath() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->path; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sets the view name |
|
58
|
|
|
* |
|
59
|
|
|
* @param string |
|
60
|
|
|
* |
|
61
|
|
|
* @return null |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setName($name) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->name = $name; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sets the path |
|
70
|
|
|
* |
|
71
|
|
|
* @param string |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function setPath($path) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->path = $path; |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Constructor |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @param array $parameters |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function __construct($name, Array $parameters = []) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->name = $name; |
|
89
|
1 |
|
$this->setParams($parameters); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the contents of the view |
|
94
|
|
|
* |
|
95
|
|
|
* @throws Exception\ViewNotFoundException |
|
96
|
|
|
* |
|
97
|
|
|
* @return null |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getContents() |
|
100
|
|
|
{ |
|
101
|
1 |
|
$contents = file_get_contents($_view); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
1 |
|
if ($contents === false) { |
|
104
|
|
|
throw new Exception\ViewGetContentsErrorException("The view '" .$this->name. "' does not exists"); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return $contents; |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
1 |
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Loads the view |
|
112
|
|
|
* |
|
113
|
1 |
|
* @return null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function render() |
|
116
|
|
|
{ |
|
117
|
|
|
if (count($this->getParams())) { |
|
118
|
|
|
extract($this->getParams(), EXTR_SKIP); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
include $this->getFile(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Gets the file path |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
private function getFile() |
|
130
|
|
|
{ |
|
131
|
|
|
$_view = $this->path . DIRECTORY_SEPARATOR . $this->name . '.phtml'; |
|
132
|
|
|
|
|
133
|
|
|
if (!file_exists($_view)) { |
|
134
|
|
|
throw new Exception\ViewNotFoundException("The view '" .$this->name. "' does not exists"); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $_view; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|