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
| <?php
class View
{
private $file;
private $template;
private $vue;
private $content;
private $currentTemplate;
public function __construct($dossier , $fichier_vue = null , $fichier_template = null)
{
if ( $fichier_vue != null || $fichier_template != null ) {
if ( is_string($dossier) && is_string($fichier_vue) && is_string($fichier_template) ) {
$this -> findFiles($fichier_template , $dossier , $fichier_vue);
}
else {
die("Le format des paramètres est invalide. String attendu");
}
}
}
private function findFiles($nomTemplate , $nomDossier , $nomVue)
{
$path_template = "templates/" . $nomDossier . "/template-" . $nomTemplate . ".php";
$path_view = "views/" . $nomDossier . "/view-" . $nomVue . ".php";
// le template
if ( file_exists($path_template) ) {
$this -> template = $path_template;
$this -> currentTemplate = $nomTemplate;
}
else {
die("La template : " . $path_template . " est introuvable");
}
// la vue
if ( file_exists($path_view) ) {
$this -> file = $path_view;
}
else {
die("La vue : " . $path_view . " est introuvable");
}
}
public function generer(array $data = null)
{
if ( $data == null ) {
$this -> content = $this -> extractData($this -> file);
}
else {
$this -> content = $this -> extractData($this -> file , $data);
}
if ( $this -> template != null ) {
if ( $this -> currentTemplate == "home" || $this -> currentTemplate == "general" ) {
// Récupération des données dans la BDD
$coordonnees = new IdentityManager();
$infos = $coordonnees -> getInfo();
$this -> vue = $this -> extractData($this -> template , array(
"content" => $this -> content ,
"title" => $this -> title ,
"description" => $this -> description ,
"titre_page" => $this -> titre_page ,
"coordonnees" => $infos
));
}
else {
$this -> vue = $this -> extractData($this -> template , array(
"content" => $this -> content ,
"title" => $this -> title ,
"titre_page" => $this -> titre_page ));
}
}
else {
$this -> vue = $this -> content;
}
echo $this -> vue;
}
private function extractData($file , $data = null)
{
if ( $data == null ) {
ob_start();
require_once $file;
return ob_get_clean();
}
else if ( is_array($data) ) {
ob_start();
extract($data);
require_once $file;
return ob_get_clean();
}
}
} |
Partager