]> dev.renevier.net Git - syj.git/blob - application/controllers/helpers/SyjApi.php
version 0.1
[syj.git] / application / controllers / helpers / SyjApi.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 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
12     public function init() {
13         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
14         $viewRenderer->setNoRender();
15         $layout = Zend_Layout::getMvcInstance();
16         if (null !== $layout) {
17             $layout->disableLayout();
18         }
19     }
20
21     public function setContentType($contentType) {
22         $this->_contentType = (string)$contentType;
23         return $this;
24     }
25
26     public function getContentType() {
27         $this->_contentType = $contentType;
28     }
29
30     public function setBody($body) {
31         $this->_body = (string)$body;
32         return $this;
33     }
34
35     public function getBody() {
36         return $body;
37     }
38
39     public function setCode($code) {
40         $this->_code = (int)$code;
41         return $this;
42     }
43
44     public function getCode() {
45         return $this->_code;
46     }
47
48     public function setCheckIfNoneMatch($check) {
49         $this->_checkIfNoneMatch = (boolean)$check;
50         return $this;
51     }
52
53     public function getCheckIfNoneMatch() {
54         return $this->_checkIfNoneMatch;
55     }
56
57     public function postDispatch() {
58         $response = $this->getResponse();
59
60         $response->setHeader('Content-Type', $this->_contentType)
61                  ->setHeader('Content-Length', strlen($this->_body));
62
63         if ($this->_checkIfNoneMatch) {
64             $request = $this->getRequest();
65
66             $etag = md5 ($this->_body);
67             if ($request->getServer("HTTP_IF_NONE_MATCH") == $etag) {
68                 $response->setHttpResponseCode(304);
69                 return;
70             }
71
72             // no-cache is needed otherwise IE does not try to get new version.
73             $response->setHeader ('Cache-control', 'no-cache, must-revalidate');
74             $response->setHeader ('Etag', $etag);
75         }
76
77         $response->setHttpResponseCode($this->_code)
78                  ->setBody($this->_body);
79     }
80 }
81 ?>