_protocols[$uri])) { $this->_protocols[$uri] = $this->_initProtocol($uri); } $this->_registerControllerPlugin(); return $this->_protocols[$uri]; } /** * Initialize a new protocol * * @param string $uri The URI for the protocol to be initialized * @return object Returns the new initialized protocol instance * @throws Zend_Wildfire_Exception */ protected function _initProtocol($uri) { switch ($uri) { case Zend_Wildfire_Protocol_JsonStream::PROTOCOL_URI; return new Zend_Wildfire_Protocol_JsonStream(); } throw new Zend_Wildfire_Exception('Tyring to initialize unknown protocol for URI "'.$uri.'".'); } /** * Flush all data from all protocols and send all data to response headers. * * @return boolean Returns TRUE if data was flushed */ public function flush() { if (!$this->_protocols || !$this->isReady()) { return false; } foreach ( $this->_protocols as $protocol ) { $payload = $protocol->getPayload($this); if ($payload) { foreach( $payload as $message ) { $this->getResponse()->setHeader(self::$_headerPrefix.$message[0], $message[1], true); } } } return true; } /** * Set the index of the plugin in the controller dispatch loop plugin stack * * @param integer $index The index of the plugin in the stack * @return integer The previous index. */ public static function setControllerPluginStackIndex($index) { $previous = self::$_controllerPluginStackIndex; self::$_controllerPluginStackIndex = $index; return $previous; } /** * Register this object as a controller plugin. * * @return void */ protected function _registerControllerPlugin() { $controller = Zend_Controller_Front::getInstance(); if (!$controller->hasPlugin(get_class($this))) { $controller->registerPlugin($this, self::$_controllerPluginStackIndex); } } /* * Zend_Wildfire_Channel_Interface */ /** * Determine if channel is ready. * * @return boolean Returns TRUE if channel is ready. */ public function isReady() { return ($this->getResponse()->canSendHeaders() && preg_match_all('/\s?FirePHP\/([\.|\d]*)\s?/si', $this->getRequest()->getHeader('User-Agent'),$m)); } /* * Zend_Controller_Plugin_Abstract */ /** * Flush messages to headers as late as possible but before headers have been sent. * * @return void */ public function dispatchLoopShutdown() { $this->flush(); } /** * Get the request object * * @return Zend_Controller_Request_Abstract * @throws Zend_Wildfire_Exception */ public function getRequest() { if (!$this->_request) { $controller = Zend_Controller_Front::getInstance(); $this->setRequest($controller->getRequest()); } if (!$this->_request) { throw new Zend_Wildfire_Exception('Request objects not initialized.'); } return $this->_request; } /** * Get the response object * * @return Zend_Controller_Response_Abstract * @throws Zend_Wildfire_Exception */ public function getResponse() { if (!$this->_response) { $response = Zend_Controller_Front::getInstance()->getResponse(); if ($response) { $this->setResponse($response); } } if (!$this->_response) { throw new Zend_Wildfire_Exception('Response objects not initialized.'); } return $this->_response; } }