Hawk - PHP documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • Hawk
    • View
      • Plugins

Classes

  • Hawk\App
  • Hawk\ButtonInput
  • Hawk\Cache
  • Hawk\CheckboxInput
  • Hawk\ColorInput
  • Hawk\Conf
  • Hawk\Controller
  • Hawk\Crypto
  • Hawk\DatabaseSessionHandler
  • Hawk\DatetimeInput
  • Hawk\DB
  • Hawk\DBExample
  • Hawk\DeleteInput
  • Hawk\Dialogbox
  • Hawk\EmailInput
  • Hawk\ErrorHandler
  • Hawk\Event
  • Hawk\FileInput
  • Hawk\FileSystem
  • Hawk\FloatInput
  • Hawk\Form
  • Hawk\FormFieldset
  • Hawk\FormInput
  • Hawk\GenericModel
  • Hawk\GifImage
  • Hawk\HawkApi
  • Hawk\HawkUpdater
  • Hawk\HiddenInput
  • Hawk\HtmlInput
  • Hawk\HTTPRequest
  • Hawk\Icon
  • Hawk\Image
  • Hawk\IntegerInput
  • Hawk\ItemList
  • Hawk\ItemListField
  • Hawk\JpegImage
  • Hawk\Lang
  • Hawk\Language
  • Hawk\LeftSidebarTab
  • Hawk\Less
  • Hawk\Logger
  • Hawk\Mail
  • Hawk\MenuItem
  • Hawk\Model
  • Hawk\NoSidebarTab
  • Hawk\NumberInput
  • Hawk\ObjectInput
  • Hawk\Option
  • Hawk\Panel
  • Hawk\PasswordInput
  • Hawk\Permission
  • Hawk\Plugin
  • Hawk\PluginInstaller
  • Hawk\PngImage
  • Hawk\ProfileQuestion
  • Hawk\ProfileQuestionValue
  • Hawk\RadioInput
  • Hawk\Request
  • Hawk\Response
  • Hawk\RightSidebarTab
  • Hawk\Role
  • Hawk\RolePermission
  • Hawk\Route
  • Hawk\Router
  • Hawk\SelectInput
  • Hawk\Session
  • Hawk\Singleton
  • Hawk\SubmitInput
  • Hawk\Tabs
  • Hawk\TextareaInput
  • Hawk\TextInput
  • Hawk\Theme
  • Hawk\TimeInput
  • Hawk\Upload
  • Hawk\User
  • Hawk\View
  • Hawk\View\Plugins\Accordion
  • Hawk\View\Plugins\Button
  • Hawk\View\Plugins\Form
  • Hawk\View\Plugins\Icon
  • Hawk\View\Plugins\Import
  • Hawk\View\Plugins\Panel
  • Hawk\View\Plugins\Tabs
  • Hawk\View\Plugins\Text
  • Hawk\View\Plugins\Uri
  • Hawk\View\Plugins\Widget
  • Hawk\ViewPlugin
  • Hawk\Widget
  • Hawk\WysiwygInput

Traits

  • Hawk\Utils

Exceptions

  • Hawk\AppStopException
  • Hawk\DBExampleException
  • Hawk\DBException
  • Hawk\FileSystemException
  • Hawk\HawkApiException
  • Hawk\ImageException
  • Hawk\MailException
  • Hawk\UploadException
  • Hawk\ViewException
  1 <?php
  2 /**
  3  * Response.php
  4  *
  5  * @author  Elvyrra SAS
  6  * @license http://rem.mit-license.org/ MIT
  7  */
  8 
  9 namespace Hawk;
 10 
 11 /**
 12  * This class defines static methods to prepare and send the HTTP response to the client
 13  *
 14  * @package Core
 15  */
 16 final class Response extends Singleton{
 17     /**
 18      * The response content
 19      *
 20      * @var string|array
 21      */
 22     private $body,
 23 
 24     /**
 25      * The response status
 26      *
 27      * @var int
 28      */
 29     $status = 200,
 30 
 31     /**
 32      * The response headers
 33      *
 34      * @var array
 35      */
 36     $headers = array(),
 37 
 38     /**
 39      * The response cookies
 40      *
 41      * @var array
 42      */
 43     $cookies = array(),
 44 
 45     /**
 46      * The response type
 47      *
 48      * @var string
 49      */
 50     $contentType = 'html';
 51 
 52 
 53     /**
 54      * The response instance
 55      *
 56      * @var Response
 57      */
 58     protected static $instance;
 59 
 60     /**
 61      * Predefined content types
 62      *
 63      * @var array
 64      */
 65     private static $contentTypes = array(
 66         'html' => 'text/html',
 67         'json' => 'application/json',
 68         'javascript' => 'application/javascript',
 69         'css' => 'text/css',
 70         'text' => 'text/plain',
 71         'xml' => 'application/xml',
 72         'stream' => 'application/octet-stream'
 73     );
 74 
 75     /**
 76      * Constructor
 77      */
 78     protected function __construct(){
 79         $this->setContentType('html');
 80     }
 81 
 82 
 83     /**
 84      * Get the content to return
 85      *
 86      * @return string
 87      */
 88     public function getBody(){
 89         return $this->body;
 90     }
 91 
 92 
 93     /**
 94      * Set the content to return to the client
 95      *
 96      * @param mixed $body The content to set
 97      */
 98     public function setBody($body){
 99         $this->body = $body;
100     }
101 
102     /**
103      * Set response headers
104      *
105      * @param string $name  The header name
106      * @param string $value The header value
107      */
108     public function header($name, $value){
109         $this->headers[$name] = $value;
110     }
111 
112     /**
113      * Set response cookie
114      *
115      * @param string $name The cookie name
116      * @param mixed  $data The cookie $data. Can be a string value, or an array containing the properties
117      *                     'value', 'expires', 'path', 'domain', 'secure' or 'httponly'
118      */
119     public function setCookie($name, $data){
120         if(is_string($data)) {
121             $data = array(
122                 'value' => $data,
123             );
124         }
125 
126         if(empty($data['path'])) {
127             $data['path'] = '/';
128         }
129 
130         $this->cookies[$name] = $data;
131     }
132 
133     /**
134      * Set the content type of the HTTP response
135      *
136      * @param string $type The type to set
137      */
138     public function setContentType($type){
139         App::logger()->debug('change content type of response to ' . $type);
140 
141         $this->contentType = $type;
142         if(isset(self::$contentTypes[$type])) {
143             $type = self::$contentTypes[$type];
144         }
145         $this->header('Content-type', $type);
146     }
147 
148 
149     /**
150      * Set the response as HTML
151      */
152     public function setHtml(){
153         $this->setContentType('html');
154     }
155 
156     /**
157      * Set the response as JSON
158      */
159     public function setJson(){
160         $this->setContentType('json');
161     }
162 
163     /**
164      * Set the response as JavaScript
165      */
166     public function setScript(){
167         $this->setContentType('javascript');
168     }
169 
170     /**
171      * Get the response type
172      */
173     public function getContentType(){
174         return $this->contentType;
175     }
176 
177 
178     /**
179      * Set the response HTTP code
180      *
181      * @param int $code The HTTP code to set
182      */
183     public function setStatus($code){
184         $this->status = $code;
185     }
186 
187     /**
188      * Get the response HTTP status
189      *
190      * @return int
191      */
192     public function getStatus(){
193         return $this->status;
194     }
195 
196 
197 
198     /**
199      * Return the HTTP response to the client, add exit the script
200      *
201      * @param string $content The content to set in the response body before returning it to the client
202      */
203     public function end($content = null){
204         http_response_code($this->status);
205 
206         // Set the response cookies
207         $lines = array();
208         foreach($this->cookies as $name => $data){
209             $line = $name . '=' . $data['value'];
210             if(!empty($data['expires'])) {
211                 $line .= ';expires=' . gmdate('D, d M Y H:i:s \G\M\T', $data['expires']);
212             }
213             if(!empty($data['path'])) {
214                 $line .= ';path=' . $data['path'];
215             }
216             if(!empty($data['domain'])) {
217                 $line .= ';domain=' . $data['domain'];
218             }
219             if(!empty($data['secure'])) {
220                 $line .= ';secure';
221             }
222             if(!empty($data['httponly'])) {
223                 $line .= ';httponly';
224             }
225 
226             $lines[] = $line;
227         }
228         if(!empty($lines)) {
229             $this->header('Set-Cookie', implode(PHP_EOL, $lines));
230         }
231 
232 
233         // Set the response headers
234         foreach($this->headers as $name => $value){
235             header($name .': ' . $value);
236         }
237 
238 
239         // Set the response body
240         if($content !== null) {
241             $this->setBody($content);
242         }
243 
244 
245         switch($this->contentType){
246             case 'json' :
247                 echo json_encode($this->body, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_NUMERIC_CHECK);
248                 break;
249             default :
250                 echo $this->body;
251                 break;
252         }
253 
254         App::logger()->debug('script execution time : ' . ((microtime(true) - SCRIPT_START_TIME) * 1000) . ' ms');
255         exit();
256     }
257 
258 
259     /**
260      * Redirect to another URL
261      *
262      * @param string $url The URL to redirect to
263      */
264     public function redirect($url){
265         App::logger()->debug('redirect to ' . $url);
266         $this->header('Location', $url);
267         $this->end();
268     }
269 
270     /**
271      * Redirect to a route
272      *
273      * @param string $route The route name to redirect to
274      * @param array  $vars  The route parameters value to set
275      */
276     public function redirectToAction($route, $vars = array()){
277         $url = App::router()->getUri($route, $vars = array());
278         $this->redirect($url);
279     }
280 }
281 
Hawk - PHP documentation API documentation generated by ApiGen