]> dev.renevier.net Git - syj.git/blob - application/controllers/helpers/SyjApi.php
SyjNoRender helper
[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         $this->getActionController()->getHelper('SyjNoRender')->disableRender();
15     }
16
17     public function setContentType($contentType) {
18         $this->_contentType = (string)$contentType;
19         return $this;
20     }
21
22     public function getContentType() {
23         $this->_contentType = $contentType;
24     }
25
26     public function setBodyJson($data) {
27         $this->setBody(json_encode($data))
28              ->setContentType('application/json');
29         return $this;
30     }
31
32     public function setBody($body) {
33         $this->_body = (string)$body;
34         return $this;
35     }
36
37     public function getBody() {
38         return $body;
39     }
40
41     public function setCode($code) {
42         $this->_code = (int)$code;
43         return $this;
44     }
45
46     public function getCode() {
47         return $this->_code;
48     }
49
50     public function setRedirect($url, $code = 0) {
51         $this->_redirect = (string)$url;
52         if (is_numeric($code) && (int)$code >= 300 && (int)$code < 400) {
53             $this->_code;
54         } else if (!isset($this->_code)) {
55             $this->code = 301;
56         }
57         return $this;
58     }
59
60     public function getRedirect() {
61         return $this->_redirect;
62     }
63
64     public function setCheckIfNoneMatch($check) {
65         $this->_checkIfNoneMatch = (boolean)$check;
66         return $this;
67     }
68
69     public function getCheckIfNoneMatch() {
70         return $this->_checkIfNoneMatch;
71     }
72
73     public function postDispatch() {
74         $response = $this->getResponse();
75
76         $response->setHeader('Content-Type', $this->_contentType, true)
77                  ->setHeader('Content-Length', strlen($this->_body), true);
78
79         if ($this->_checkIfNoneMatch) {
80             $request = $this->getRequest();
81
82             $etag = md5 ($this->_body);
83             if ($request->getServer("HTTP_IF_NONE_MATCH") == $etag) {
84                 $response->setHttpResponseCode(304);
85                 return;
86             }
87
88             // no-cache is needed otherwise IE does not try to get new version.
89             $response->setHeader ('Cache-control', 'no-cache, must-revalidate');
90             $response->setHeader ('Etag', $etag);
91         }
92
93         if ($this->_redirect) {
94             $response->setHeader ('Location', $this->_redirect);
95         }
96
97         $response->setHttpResponseCode($this->_code)
98                  ->setBody($this->_body);
99     }
100 }
101 ?>