energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file Response.class.php

Documentation is available at Response.class.php

  1. <?php
  2.  
  3. /**
  4.  * Класс Response.
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author 1m.dm
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/framework/Object.class.php');
  14.  
  15. /**
  16.  * HTTP-ответ.
  17.  *
  18.  * @package energine
  19.  * @subpackage core
  20.  * @author 1m.dm
  21.  * @final
  22.  */
  23. final class Response extends Object {
  24.  
  25.     /**
  26.      * @access private
  27.      * @var Response единый для всей системы экземпляр класса Response
  28.      */
  29.     private static $instance;
  30.  
  31.     /**
  32.      * @access private
  33.      * @var string строка статуса ответа
  34.      */
  35.     private $statusLine;
  36.  
  37.     /**
  38.      * @access private
  39.      * @var array заголовки ответа
  40.      */
  41.     private $headers;
  42.  
  43.     /**
  44.      * @access private
  45.      * @var array cookies ответа
  46.      */
  47.     private $cookies;
  48.  
  49.     /**
  50.      * @access private
  51.      * @var string тело ответа
  52.      */
  53.     private $body;
  54.  
  55.     /**
  56.      * @access private
  57.      * @var array описание кодов ответа
  58.      */
  59.     private $reasonPhrases = array(
  60.     100 => 'Continue',
  61.     101 => 'Switching Protocols',
  62.     200 => 'OK',
  63.     201 => 'Created',
  64.     202 => 'Accepted',
  65.     203 => 'Non-Authoritative Information',
  66.     204 => 'No Content',
  67.     205 => 'Reset Content',
  68.     206 => 'Partial Content',
  69.     300 => 'Multiple Choices',
  70.     301 => 'Moved Permanently',
  71.     302 => 'Found',
  72.     303 => 'See Other',
  73.     304 => 'Not Modified',
  74.     305 => 'Use Proxy',
  75.     307 => 'Temporary Redirect',
  76.     400 => 'Bad Request',
  77.     401 => 'Unauthorized',
  78.     402 => 'Payment Required',
  79.     403 => 'Forbidden',
  80.     404 => 'Not Found',
  81.     405 => 'Method Not Allowed',
  82.     406 => 'Not Acceptable',
  83.     407 => 'Proxy Authentication Required',
  84.     408 => 'Request Time-out',
  85.     409 => 'Conflict',
  86.     410 => 'Gone',
  87.     411 => 'Length Required',
  88.     412 => 'Precondition Failed',
  89.     413 => 'Request Entity Too Large',
  90.     414 => 'Request-URI Too Large',
  91.     415 => 'Unsupported Media Type',
  92.     416 => 'Requested range not satisfiable',
  93.     417 => 'Expectation Failed',
  94.     500 => 'Internal Server Error',
  95.     501 => 'Not Implemented',
  96.     502 => 'Bad Gateway',
  97.     503 => 'Service Unavailable',
  98.     504 => 'Gateway Time-out',
  99.     505 => 'HTTP Version not supported'
  100.     );
  101.  
  102.     /**
  103.      * Конструктор класса.
  104.      *
  105.      * @access public
  106.      * @return void 
  107.      */
  108.     public function __construct({
  109.         parent::__construct();
  110.  
  111.         $this->setStatus(200);
  112.         $this->headers = array();
  113.         $this->cookies = array();
  114.         $this->body = '';
  115.     }
  116.  
  117.     /**
  118.      * Возвращает единый для всей системы экземпляр класса Response.
  119.      *
  120.      * @access public
  121.      * @static
  122.      * @return Response 
  123.      */
  124.     static public function getInstance({
  125.         if (!isset(self::$instance)) {
  126.             self::$instance new Response;
  127.         }
  128.         return self::$instance;
  129.     }
  130.  
  131.     /**
  132.      * Устанавливает статус ответа.
  133.      *
  134.      * @access public
  135.      * @param int $statusCode 
  136.      * @param string $reasonPhrase 
  137.      * @return void 
  138.      */
  139.     public function setStatus($statusCode$reasonPhrase null{
  140.         if (!isset($reasonPhrase)) {
  141.             $reasonPhrase (isset($this->reasonPhrases[$statusCode]$this->reasonPhrases[$statusCode'');
  142.         }
  143.         $this->statusLine = $_SERVER['SERVER_PROTOCOL']." $statusCode $reasonPhrase";
  144.     }
  145.  
  146.     /**
  147.      * Устанавливает поле заголовка ответа.
  148.      *
  149.      * @access public
  150.      * @param string $name 
  151.      * @param string $value 
  152.      * @param boolean $replace 
  153.      * @return void 
  154.      */
  155.     public function setHeader($name$value$replace true{
  156.         if ((!$replace&& isset($this->headers[$name])) {
  157.             return;
  158.         }
  159.         $this->headers[$name$value;
  160.     }
  161.  
  162.     /**
  163.      * Устанавливает cookie.
  164.      *
  165.      * @access public
  166.      * @param string $name 
  167.      * @param string $value 
  168.      * @param int $expire 
  169.      * @param string $path 
  170.      * @param string $domain 
  171.      * @param boolean $secure 
  172.      * @return void 
  173.      */
  174.     public function setCookie($name$value ''$expire ''$path ''$domain ''$secure false{
  175.         $this->cookies[$namecompact('value''expire''path''domain''secure');
  176.     }
  177.  
  178.     /**
  179.      * Удаляет cookie.
  180.      *
  181.      * @access public
  182.      * @param string $name 
  183.      * @param string $path 
  184.      * @param string $domain 
  185.      * @param boolean $secure 
  186.      * @return void 
  187.      */
  188.     public function deleteCookie($name$path ''$domain ''$secure false{
  189.         $this->setCookie($name''(time(1)$path$domain$secure);
  190.     }
  191.  
  192.     /**
  193.      * Устанавливает адрес для переадресации.
  194.      *
  195.      * @param string $location 
  196.      * @return void 
  197.      * @access public
  198.      */
  199.     public function setRedirect($location{
  200.         $this->setHeader('Location'$location);
  201.         $this->commit();
  202.     }
  203.  
  204.     /**
  205.      * Устанавливает адрес переадресации
  206.      *
  207.      * @param string $action 
  208.      * @return void 
  209.      * @access public
  210.      */
  211.     public function redirectToCurrentSection($action ''{
  212.         if ($action && substr($action-1!== '/'{
  213.             $action .= '/';
  214.         }
  215.         $request Request::getInstance();
  216.         $this->setRedirect(
  217.         $request->getBasePath()
  218.         .$request->getLangSegment()
  219.         .$request->getPath(Request::PATH_TEMPLATEtrue)
  220.         .$action
  221.         );
  222.     }
  223.  
  224.     /**
  225.      * Добавляет данные к телу ответа.
  226.      *
  227.      * @access public
  228.      * @param string $data 
  229.      * @return void 
  230.      */
  231.     public function write($data{
  232.         $this->body .= $data;
  233.     }
  234.  
  235.     /**
  236.      * Отправляет ответ клиенту и завершает работу программы.
  237.      *
  238.      * @access public
  239.      * @return void 
  240.      */
  241.     public function commit({
  242.         if (!headers_sent($filename$linenum)) {
  243.             header($this->statusLine);
  244.             foreach ($this->headers as $name => $value{
  245.                 header("$name$value");
  246.             }
  247.             foreach ($this->cookies as $name => $params{
  248.                 setcookie($name$params['value']$params['expire']$params['path']$params['domain']$params['secure']);
  249.             }
  250.         }
  251.         else {
  252.             //throw new SystemException('ERR_HEADERS_SENT', SystemException::ERR_CRITICAL, array($filename, $linenum));
  253.         }
  254.         $contents $this->body;
  255.  
  256.         if ((bool)Object::_getConfigValue('site.compress'&& (strpos($_SERVER['HTTP_ACCEPT_ENCODING']'gzip'!== false)) {
  257.             header("Vary: Accept-Encoding");
  258.             header("Content-Encoding: gzip");
  259.             $contents gzencode($contents6);
  260.         }
  261.  
  262.         echo $contents;
  263.         session_write_close();
  264.         exit;
  265.     }
  266. }
В создании документации нам помог: phpDocumentor