2016-07-11 15:44:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager;
|
|
|
|
|
|
|
|
use \manager\View;
|
|
|
|
use \manager\ManagerError;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class View{
|
|
|
|
|
|
|
|
public $error = ManagerError::Success;
|
|
|
|
|
|
|
|
|
|
|
|
private $template;
|
|
|
|
private $content;
|
|
|
|
|
|
|
|
private $view;
|
|
|
|
|
2016-07-14 08:16:11 +00:00
|
|
|
public static $htmlError = "<span class='error'>Une erreur est survenue, veuilez contacter le webmaster si cette erreur persiste.</span>";
|
|
|
|
|
2016-07-11 15:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* INITIALISATION DE LA VUE VUE
|
|
|
|
*
|
|
|
|
* @template<String> Nom du modèle de la vue à utiliser
|
|
|
|
* @content<Array> Données pour construire la vue à partir du modèle
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct($template, $content){
|
|
|
|
// Si pas parametre manquant, on quitte
|
|
|
|
if( $template == null ){
|
|
|
|
$this->error = ManagerError::MissingPath;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] On vérifie que le template existe
|
|
|
|
=========================================================*/
|
|
|
|
$templatePath = $this->checkPath($template);
|
|
|
|
|
|
|
|
if( $templatePath === false )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On récupère le chemin du template et on l'enregistre
|
|
|
|
=========================================================*/
|
|
|
|
$this->template = $templatePath;
|
|
|
|
|
|
|
|
/* [3] On enregistre les paramètres
|
|
|
|
=========================================================*/
|
|
|
|
$this->content = $content;
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] On process la vue
|
|
|
|
=========================================================*/
|
|
|
|
$this->view = call_user_func(
|
|
|
|
$this->template,
|
|
|
|
$this->content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* AFFICHE LA VUE CALCULEE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function view(){
|
|
|
|
echo $this->view;
|
|
|
|
}
|
|
|
|
|
2016-07-14 08:52:04 +00:00
|
|
|
/* RETOURNE LA VUE CALCULEE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getView(){
|
|
|
|
return $this->view;
|
|
|
|
}
|
|
|
|
|
2016-07-11 15:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DE L'EXISTENCE D'UN MODELE
|
|
|
|
*
|
|
|
|
* @template<String> Chemin du modèle
|
|
|
|
*
|
|
|
|
* @return status<Boolean> TRUE si le modèle existe, sinon FALSE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function checkPath($template){
|
|
|
|
/* [1] On vérifie le format
|
|
|
|
=========================================================*/
|
|
|
|
if( !preg_match('/^([a-z]+)\.([a-z]+)$/i', $template, $match) ){
|
|
|
|
$this->error = ManagerError::ParamError;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On vérifie que le template existe
|
|
|
|
=========================================================*/
|
2016-07-12 10:06:46 +00:00
|
|
|
$class = '\\manager\\view\\'.$match[1].'\\'.$match[2];
|
|
|
|
$method = 'view';
|
2016-07-11 15:44:18 +00:00
|
|
|
|
|
|
|
/* (1) On vérifie que la classe existe */
|
|
|
|
if( !class_exists($class) ){
|
|
|
|
$this->error = ManagerError::UnknownTemplate;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) On vérifie que la méthode existe */
|
|
|
|
if( !method_exists($class, $method) ){
|
|
|
|
$this->error = ManagerError::UnknownTemplate;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Si tout est bon, on renvoie les données
|
|
|
|
=========================================================*/
|
|
|
|
return [ $class, $method ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* APPLIQUE UN REMPLACEMENT SIMPLE
|
|
|
|
*
|
|
|
|
* @template<String> Contenu HTML du templace
|
2016-07-12 08:40:51 +00:00
|
|
|
* @singles<Array> Variables à insérer
|
2016-07-11 15:44:18 +00:00
|
|
|
*
|
|
|
|
* @return result<String> Retourne le templace rempli
|
|
|
|
*
|
|
|
|
*/
|
2016-07-12 08:40:51 +00:00
|
|
|
public static function replaceSingle($template, $singles){
|
2016-07-11 15:44:18 +00:00
|
|
|
|
2016-07-12 08:40:51 +00:00
|
|
|
/* On applique chaque remplacement */
|
|
|
|
foreach($singles as $k=>$v){
|
2016-07-11 15:44:18 +00:00
|
|
|
|
|
|
|
/* Tant qu'il y a, on remplace */
|
|
|
|
if( strpos($template, '@'.$k) !== false )
|
|
|
|
$template = str_replace('@'.$k, $v, $template);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-12 08:40:51 +00:00
|
|
|
/* APPLIQUE UN REMPLACEMENT MULTIPLE
|
|
|
|
*
|
|
|
|
* @template<String> Contenu HTML du templace
|
|
|
|
* @multiples<Array> Tableau de Singles
|
|
|
|
* @singles<Array> Variables statiques supplémentaires
|
|
|
|
*
|
|
|
|
* @return result<String> Retourne le templace rempli
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function replaceMultiple($template, $multiples, $singles=[]){
|
|
|
|
|
|
|
|
$view = '';
|
|
|
|
|
|
|
|
/* On applique chaque remplacement */
|
|
|
|
foreach($multiples as $multiple)
|
|
|
|
$view .= self::replaceSingle($template, array_merge($multiple, $singles));
|
2016-07-11 15:44:18 +00:00
|
|
|
|
2016-07-12 08:40:51 +00:00
|
|
|
return $view;
|
2016-07-11 15:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// USE case
|
|
|
|
//
|
|
|
|
// $myView = new View('templateParent.templateModel', [
|
|
|
|
// 'myVar' => 1,
|
|
|
|
// 'helloMsg' => 'Hello',
|
|
|
|
// ]);
|
|
|
|
//
|
|
|
|
// $myView->view();
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
?>
|