Share Pastebin
Guest
Public paste!

OnyxRaven

By: a guest | Feb 9th, 2010 | Syntax: PHP | Size: 4.56 KB | Hits: 100 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3. class ZendL_Controller_Action_Helper_ContextSwitch
  4. extends Zend_Controller_Action_Helper_ContextSwitch
  5. {
  6.  
  7.     //adds jsonp support for json context
  8.     protected $_json_callback = false;
  9.     //can set up a xml serializer (maybe).
  10.     protected $_autoXmlSerialization = false;
  11.  
  12.     public function init() {
  13.         parent::init();
  14.  
  15.         $this->setContext('rss', array('suffix'  => 'rss',
  16.                                        'headers' => array('Content-Type' => 'application/rss+xml',
  17.                                                           'charset' => 'utf-8'),
  18.                                       ))
  19.             ->setContext('atom', array('suffix'  => 'atom',
  20.                                        'headers' => array('Content-Type' => 'application/atom+xml',
  21.                                                           'charset' => 'utf-8'),
  22.                                       ))
  23.              ->setContext('xml', array('suffix'  => 'xml',
  24.                                        'headers' => array('Content-Type' => 'application/xml',
  25.                                                           'charset' => 'utf-8'),
  26.                                        'callbacks' => array('init' => 'initXmlContext',
  27.                                                             'post' => 'postXmlContext')
  28.                                       ))
  29.              ;
  30.     }
  31.     public function initJsonContext() {
  32.         $this->_json_callback = preg_replace('/[^\w.=]/', '',
  33.                                              $this->getRequest()->getParam('callback'));
  34.         if ($this->_json_callback) {
  35.             $this->getResponse()->setHeader('Content-Type', 'application/javascript');
  36.         }
  37.         return parent::initJsonContext();
  38.     }
  39.  
  40.     public function postJsonContext() {
  41.         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  42.         if (!$this->getAutoJsonSerialization()) {
  43.             //push the render now, since we don't know what order the
  44.             //action helpers are in, and we'll need it rendered already for postdispatch
  45.             $viewRenderer->render();
  46.             return;
  47.         }
  48.         return parent::postJsonContext();
  49.     }
  50.  
  51.     public function postDispatch() {
  52.         parent::postDispatch();
  53.         if ($this->_json_callback) {
  54.             $body = $this->getResponse()->getBody();
  55.             $body = $this->_json_callback . '(' . $body . ');';
  56.             $this->getResponse()->setBody($body);
  57.         }
  58.     }
  59.  
  60.     public function initXmlContext() {
  61.         if (!$this->getAutoXmlSerialization()) {
  62.             return;
  63.         }
  64.  
  65.         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  66.         $view = $viewRenderer->view;
  67.         if ($view instanceof Zend_View_Interface) {
  68.             $viewRenderer->setNoRender(true);
  69.         }
  70.     }
  71.  
  72.     public function setAutoXmlSerialization($flag) {
  73.         $this->_autoXmlSerialization = (bool) $flag;
  74.         return $this;
  75.     }
  76.  
  77.     public function getAutoXmlSerialization() {
  78.         return $this->_autoXmlSerialization;
  79.     }
  80.  
  81.     //todo allow customized serializer
  82.     public function postXmlContext() {
  83.         if (!$this->getAutoXmlSerialization()) {
  84.             return;
  85.         }
  86.  
  87.         //this section assumes there is a viewhelper that renders xml from the vars in the default view.
  88.         //this is a todo to define the serializer here.
  89.         $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  90.         $view = $viewRenderer->view;
  91.         if ($view instanceof Zend_View_Interface) {
  92.             if (method_exists($view, 'getVars')) {
  93.                 $vars = $view->xmlfromview();
  94.                 $this->getResponse()->setBody($vars);
  95.             } else {
  96.                 // require_once 'Zend/Controller/Action/Exception.php';
  97.                 throw new Zend_Controller_Action_Exception('View does not implement the getVars() method needed to encode the view into XML');
  98.             }
  99.         }
  100.     }
  101.  
  102. }
  103.  
  104. class HomeController extends Zend_Controller_Action {
  105.  
  106.     public function init() {
  107.         parent::init();
  108.  
  109.         $this->_helper->contextSwitch()
  110.             ->setActionContext('index', array('rss', 'atom'))
  111.             ->initContext();
  112.         //todo make feed contexts more pages or something
  113.     }
  114.  
  115. //..... actions happen below as usual
  116. // anything in $view is available in actionname.atom.phtml and actionname.rss.phtml
  117. // right alongside actionname.phtml
  118. // the autoserialization stuff can be turned on too.
  119.  
  120.  
  121.  
  122. }