. * * @package NanoMvc * @author Nicolas Joseph * @copyright 2008 Nicolas Joseph * @license http://www.opensource.org/licenses/gpl-3.0.html GPL v3 * @filesource */ $path = dirname (__FILE__); require_once ($path .'/helper/page.php'); class Response { /** * @var Response */ private static $instance = null; /** * @var string */ private $body = ''; /** * @var array */ private $headers = array(); private function __construct () { } /** * Obtenir l'unique instance de la classe. * * @return Response */ public static function get_instance () { if (is_null (self::$instance)) { self::$instance = new self (); } return self::$instance; } /** * Modifie le corps de la page. * * @param string body: le corps de la page */ public function set_body ($body) { $this->body = $body; } /** * Met en place une redirection a partir d'une url. * * @param string url: l'adresse de redirection * @param bool permanent: redirection permanente, ou non */ public function redirect_url ($url, $permanent = false) { if (!$this->headers_sent ()) { if ($permanent) { $this->headers['Status'] = '301 Moved Permanently'; } else { $this->headers['Status'] = '302 Found'; } $this->headers['location'] = $url; } else { throw new Exception ('L\'entete a deja ete envoyee !'); } } /** * Met en place une redirection. * * @param string page: page * @param string action: action * @param bool permanent: redirection permanente, ou non */ public function redirect ($page='', $action='', $permanent = false) { $url = Helper_Page::get_url ($page, $action); $this->redirect_url ($url, $permanent); } public function set_header ($key, $value) { if (!$this->headers_sent ()) { $this->headers[$key] = $value; } else { throw new Exception ('L\'entete a deja ete envoyee !'); } } public function not_found () { $this->set_header ('HTTP/1.0', '404 Not Found'); } /** * Verifie si l'entete a deja ete envoye. * * @return bool */ public function headers_sent () { return headers_sent (); } /** * Affiche la reponse. */ public function print_out () { foreach ($this->headers as $key => $value) { if ($key == 'HTTP/1.0') { header ($key .' '. $value); } else { header ($key .':'. $value); } } echo $this->body; } }