]> dev.renevier.net Git - syj.git/blob - application/controllers/helpers/SyjApi.php
set charset in default content-type http header
[syj.git] / application / controllers / helpers / SyjApi.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_Abstract
6 {
7     protected $_contentType = 'text/plain';
8     protected $_checkIfNoneMatch = false;
9     protected $_body = '';
10     protected $_code = 200;
11     protected $_redirect = '';
12
13     public function init() {
14         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
15         $viewRenderer->setNoRender();
16         $layout = Zend_Layout::getMvcInstance();
17         if (null !== $layout) {
18             $layout->disableLayout();
19         }
20     }
21
22     public function setContentType($contentType) {
23         $this->_contentType = (string)$contentType;
24         return $this;
25     }
26
27     public function getContentType() {
28         $this->_contentType = $contentType;
29     }
30
31     public function setBodyJson($data) {
32         $this->setBody(json_encode($data))
33              ->setContentType('application/json');
34         return $this;
35     }
36
37     public function setBody($body) {
38         $this->_body = (string)$body;
39         return $this;
40     }
41
42     public function getBody() {
43         return $body;
44     }
45
46     public function setCode($code) {
47         $this->_code = (int)$code;
48         return $this;
49     }
50
51     public function getCode() {
52         return $this->_code;
53     }
54
55     public function setRedirect($url, $code = 0) {
56         $this->_redirect = (string)$url;
57         if (is_numeric($code) && (int)$code >= 300 && (int)$code < 400) {
58             $this->_code;
59         } else if (!isset($this->_code)) {
60             $this->code = 301;
61         }
62         return $this;
63     }
64
65     public function getRedirect() {
66         return $this->_redirect;
67     }
68
69     public function setCheckIfNoneMatch($check) {
70         $this->_checkIfNoneMatch = (boolean)$check;
71         return $this;
72     }
73
74     public function getCheckIfNoneMatch() {
75         return $this->_checkIfNoneMatch;
76     }
77
78     public function postDispatch() {
79         $response = $this->getResponse();
80
81         $response->setHeader('Content-Type', $this->_contentType, true)
82                  ->setHeader('Content-Length', strlen($this->_body), true);
83
84         if ($this->_checkIfNoneMatch) {
85             $request = $this->getRequest();
86
87             $etag = md5 ($this->_body);
88             if ($request->getServer("HTTP_IF_NONE_MATCH") == $etag) {
89                 $response->setHttpResponseCode(304);
90                 return;
91             }
92
93             // no-cache is needed otherwise IE does not try to get new version.
94             $response->setHeader ('Cache-control', 'no-cache, must-revalidate');
95             $response->setHeader ('Etag', $etag);
96         }
97
98         if ($this->_redirect) {
99             $response->setHeader ('Location', $this->_redirect);
100         }
101
102         $response->setHttpResponseCode($this->_code)
103                  ->setBody($this->_body);
104     }
105 }
106 ?>