]> dev.renevier.net Git - syj.git/blobdiff - application/controllers/helpers/SyjApi.php
SyjNoRender helper
[syj.git] / application / controllers / helpers / SyjApi.php
index 86ea00e3ff1cdc6fa6355843e81b5d164bddc496..029fe90275eb79d3db7a7b026083265c6bedabb0 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
+/*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
     and is published under the AGPL license. */
 
 class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_Abstract
@@ -8,14 +8,10 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
     protected $_checkIfNoneMatch = false;
     protected $_body = '';
     protected $_code = 200;
+    protected $_redirect = '';
 
     public function init() {
-        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
-        $viewRenderer->setNoRender();
-        $layout = Zend_Layout::getMvcInstance();
-        if (null !== $layout) {
-            $layout->disableLayout();
-        }
+        $this->getActionController()->getHelper('SyjNoRender')->disableRender();
     }
 
     public function setContentType($contentType) {
@@ -27,6 +23,12 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
         $this->_contentType = $contentType;
     }
 
+    public function setBodyJson($data) {
+        $this->setBody(json_encode($data))
+             ->setContentType('application/json');
+        return $this;
+    }
+
     public function setBody($body) {
         $this->_body = (string)$body;
         return $this;
@@ -45,6 +47,20 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
         return $this->_code;
     }
 
+    public function setRedirect($url, $code = 0) {
+        $this->_redirect = (string)$url;
+        if (is_numeric($code) && (int)$code >= 300 && (int)$code < 400) {
+            $this->_code;
+        } else if (!isset($this->_code)) {
+            $this->code = 301;
+        }
+        return $this;
+    }
+
+    public function getRedirect() {
+        return $this->_redirect;
+    }
+
     public function setCheckIfNoneMatch($check) {
         $this->_checkIfNoneMatch = (boolean)$check;
         return $this;
@@ -57,8 +73,8 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
     public function postDispatch() {
         $response = $this->getResponse();
 
-        $response->setHeader('Content-Type', $this->_contentType)
-                 ->setHeader('Content-Length', strlen($this->_body));
+        $response->setHeader('Content-Type', $this->_contentType, true)
+                 ->setHeader('Content-Length', strlen($this->_body), true);
 
         if ($this->_checkIfNoneMatch) {
             $request = $this->getRequest();
@@ -74,6 +90,10 @@ class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_
             $response->setHeader ('Etag', $etag);
         }
 
+        if ($this->_redirect) {
+            $response->setHeader ('Location', $this->_redirect);
+        }
+
         $response->setHttpResponseCode($this->_code)
                  ->setBody($this->_body);
     }