-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.php
267 lines (231 loc) · 5.21 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Gestionnaire de vue
*
* @author dev <dev@solire.fr>
* @license CC by-nc http://creativecommons.org/licenses/by-nc/3.0/fr/
*/
namespace Slrfw;
use \Slrfw\Exception\Lib as Exception;
/**
* Gestionnaire de vue
*
* @author dev <dev@solire.fr>
* @license CC by-nc http://creativecommons.org/licenses/by-nc/3.0/fr/
*/
class View
{
/**
* Définit si la vue incluse automatiquement après execution de l'action
*
* @var bool
*/
private $enable = true;
/**
* Objet de traduction
*
* @var TranslateMysql
*/
private $translate = false;
/**
* Chemin vers la vue pour le contenu
*
* @var boolean|Path
*/
private $contentPath = false;
/**
* Chemin vers la vue "main"
*
* @var boolean|Path
*/
private $mainPath = false;
/**
* Préfix du chemin pour trouver les fichiers des vues
*
* @var string
*/
private $prefixPath = '';
/**
* Template du chemin pour trouver les fichiers des vues
*
* @var string
*/
private $formatPath = '%s';
/**
* Chargement d'une nouvelle vue
*/
public function __construct()
{
}
// TRADUCTION
/**
* Chargement de la classe de traduction
*
* @param TranslateMysql $translate Classe de traduction
*
* @return self
*/
public function setTranslate($translate)
{
$this->translate = $translate;
return $this;
}
/**
* Alias à l'utilisation de translate
*
* @param string $string Chaine à traduire
* @param string $aide Texte permettant de situer l'emplacement de la
* chaine à traduire, exemple : 'Situé sur le bas de page'
*
* @return string
* @uses TranslateMysql
*/
public function _($string, $aide = '')
{
if ($this->translate !== false) {
return $this->translate->_($string, $aide);
}
return $string;
}
/**
* Activer ou désactiver la vue
*
* @param boolean $enable Vrai pour activer
*
* @return void
*/
public function enable($enable)
{
$this->enable = (boolean) $enable;
}
/**
* Test si la vue est active
*
* @return boolean
*/
public function isEnabled()
{
return $this->enable;
}
/**
* Affiche le contenu
*
* @return void
*/
public function content()
{
if ($this->contentPath === false) {
throw new Exception('Aucun fichier de vue', 500);
}
include $this->contentPath;
}
/**
* Affiche la vue
*
* @return void
*/
public function display()
{
if ($this->mainPath !== false) {
include $this->mainPath;
} else {
$this->content();
}
}
/**
* Enregistre le fichier "main"
*
* @param string $strPath chemin vers le fichier de vue
*
* @return self
* @uses Path pour contrôler le chemin
*/
public function setViewPath($strPath)
{
$this->contentPath = $this->searchFile($strPath);
return $this;
}
/**
* Enregistre le fichier de vue pour le contenu
*
* @param string $strPath chemin vers le fichier de vue
*
* @return self
* @uses Path pour contrôler le chemin
*/
public function setMainPath($strPath, $noSearch = false)
{
if ($noSearch === true) {
$this->mainPath = $strPath;
return $this;
}
$this->mainPath = $this->searchFile($strPath);
return $this;
}
/**
* Paramètre le préfix des chemins pour les vues
*
* @param string $prefix préfix des chemins
*
* @return \Slrfw\View
*/
public function setPathPrefix($prefix)
{
$this->prefixPath = $prefix;
return $this;
}
/**
* Paramètre le prefix des chemins pour les vues
*
* @param string $format préfix du chemin
*
* @return \Slrfw\View
*/
public function setPathFormat($format)
{
$this->formatPath = (string) $format;
return $this;
}
/**
* Annule l'utilisation du fichier "main"
*
* @return self
*/
public function unsetMain()
{
$this->mainPath = false;
return $this;
}
/**
* Localise le fichier de vue demandé
*
* @param string $path nom du fichier de vue
*
* @return string
* @throws Exception si aucun fichier n'est trouvé
*/
protected function searchFile($path)
{
$path = $this->prefixPath . sprintf($this->formatPath, $path);
$file = FrontController::search($path);
if ($file === false) {
$file = FrontController::search($path, false);
}
if ($file === false) {
throw new Exception('La vue ' . (string) $path . ' ne peut être trouvée', 300);
}
return $file;
}
/**
* Ajoute un fichier
*
* @param string $filePath Nom du fichier avec l'extension
*
* @return self
*/
public function add($filePath)
{
include $this->searchFile($filePath);
return $this;
}
}