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
| <?php defined('ENVIRON') or exit('Aucun accès directe.');
/**
* view.php
*
*/
/**
* Classe Runfw_View
*
*/
class Runfw_View {
/**
* Nom de la vue
* @var string Vue
*/
protected $file;
/**
* Gloable data
* @var array Data
*/
protected $data = array();
/**
* Returns a new View object. If you do not define the "file" parameter,
* you must call [View::set_filename].
*
* $view = View::factory($file);
*
* @param string view filename
* @param array array of values
* @return View
*/
public static function factory($file = NULL, array $data = NULL) {
return new View($file, $data);
}
public function __construct($file = NULL, array $data = NULL) {
if (!empty($file)) $this->file = Controller::getFilenameVue($file);
if ($data !== NULL) {
// Add the values to the current data
$this->_data = $data + $this->_data;
}
}
public static function capture($vue_filename, array $vue_data = NULL) {
if (empty($vue_filename)) {
//echo '$vue_filename : '.$vue_filename.'<br />';
//$array = debug_backtrace();
//Sp::pr($array);
//exit();
return FALSE;
}
else {
// Import variables de la vue avec leur nom
if (!empty($vue_data)) extract($vue_data, EXTR_SKIP);
//
ob_start();
// Charge la vue dans le scope courant
try {
include($vue_filename);
}
catch (Exception $e) {
// Cette fonction vide le tampon de sortie sans l'envoyer au navigateur.
ob_clean();
Runfw::exceptionHandler($e);
}
// Retourne la capture de sortie et vide le buffer
return ob_get_clean();
}
}
public static function inclure($vue_filename) {
if (empty($vue_filename)) {
//echo '$vue_filename : '.$vue_filename.'<br />';
//$array = debug_backtrace();
//Sp::pr($array);
trigger_error('['.__METHOD__.'] Vue inexistante, inclusion impossible.', E_USER_WARNING);
}
else {
// Charge la vue dans le scope courant
include($vue_filename);
}
}
public function & __get($key) {
if (isset($this->data[$key])) {
return $this->data[$key];
}
}
public function __set($key, $value) {
$this->set($key, $value);
}
public function set($key, $value = NULL) {
if (is_array($key)) {
foreach ($key as $name => $value) {
$this->data[$name] = $value;
}
}
else {
$this->data[$key] = $value;
}
//
return $this;
}
//--------------------------------------------------------------------------
/**
* Magic method, returns the output of [View::render].
* @return string
* @uses View::render
*/
public function __toString() {
try {
return $this->render();
}
catch (Exception $e) {
// Display the exception message
Runfw::exceptionHandler($e);
return '';
}
}
/**
* Renders the view object to a string
* @param string $file
* @return string nom du fichier
* @throws Kohana_View_Exception
*/
public function render($file = NULL) {
if (is_null($file) == FALSE) {
$this->file = Controller::getFilenameVue($file);
}
//
if (empty($this->file)) {
//throw new Kohana_View_Exception('You must set the file to use within your view before rendering');
//Runfw_Exception
throw new Exception('['.__METHOD__.'] Aucun nom pour la vue vue.', E_USER_WARNING);
}
else {
// Inclus la vue avec des données au besoin et renvoi la sortie
return View::capture($this->file, $this->data);
}
}
} |
Partager