PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Wildfire/Channel/HttpHeaders.php

https://github.com/jverkoey/snaapilookup
PHP | 289 lines | 125 code | 36 blank | 128 comment | 18 complexity | f88b4dec2ee5ee88ca719b1092f16a9a MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Wildfire
  17. * @subpackage Channel
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Wildfire_Channel_Interface */
  22. require_once 'Zend/Wildfire/Channel/Interface.php';
  23. /** Zend_Wildfire_Exception */
  24. require_once 'Zend/Wildfire/Exception.php';
  25. /** Zend_Controller_Request_Abstract */
  26. require_once('Zend/Controller/Request/Abstract.php');
  27. /** Zend_Controller_Response_Abstract */
  28. require_once('Zend/Controller/Response/Abstract.php');
  29. /** Zend_Controller_Plugin_Abstract */
  30. require_once 'Zend/Controller/Plugin/Abstract.php';
  31. /** Zend_Wildfire_Protocol_JsonStream */
  32. require_once 'Zend/Wildfire/Protocol/JsonStream.php';
  33. /** Zend_Controller_Front **/
  34. require_once 'Zend/Controller/Front.php';
  35. /**
  36. * Implements communication via HTTP request and response headers for Wildfire Protocols.
  37. *
  38. * @category Zend
  39. * @package Zend_Wildfire
  40. * @subpackage Channel
  41. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Wildfire_Channel_HttpHeaders extends Zend_Controller_Plugin_Abstract implements Zend_Wildfire_Channel_Interface
  45. {
  46. /**
  47. * The string to be used to prefix the headers.
  48. * @var string
  49. */
  50. protected static $_headerPrefix = 'X-WF-';
  51. /**
  52. * Singleton instance
  53. * @var Zend_Wildfire_Channel_HttpHeaders
  54. */
  55. protected static $_instance = null;
  56. /**
  57. * The index of the plugin in the controller dispatch loop plugin stack
  58. * @var integer
  59. */
  60. protected static $_controllerPluginStackIndex = 999;
  61. /**
  62. * The protocol instances for this channel
  63. * @var array
  64. */
  65. protected $_protocols = null;
  66. /**
  67. * Initialize singleton instance.
  68. *
  69. * @param string $class OPTIONAL Subclass of Zend_Wildfire_Channel_HttpHeaders
  70. * @return Zend_Wildfire_Channel_HttpHeaders Returns the singleton Zend_Wildfire_Channel_HttpHeaders instance
  71. * @throws Zend_Wildfire_Exception
  72. */
  73. public static function init($class = null)
  74. {
  75. if (self::$_instance!==null) {
  76. throw new Zend_Wildfire_Exception('Singleton instance of Zend_Wildfire_Channel_HttpHeaders already exists!');
  77. }
  78. if ($class!==null) {
  79. if (!is_string($class)) {
  80. throw new Zend_Wildfire_Exception('Third argument is not a class string');
  81. }
  82. Zend_Loader::loadClass($class);
  83. self::$_instance = new $class();
  84. if (!self::$_instance instanceof Zend_Wildfire_Channel_HttpHeaders) {
  85. self::$_instance = null;
  86. throw new Zend_Wildfire_Exception('Invalid class to third argument. Must be subclass of Zend_Wildfire_Channel_HttpHeaders.');
  87. }
  88. } else {
  89. self::$_instance = new self();
  90. }
  91. return self::$_instance;
  92. }
  93. /**
  94. * Get or create singleton instance
  95. *
  96. * @param $skipCreate boolean True if an instance should not be created
  97. * @return Zend_Wildfire_Channel_HttpHeaders
  98. */
  99. public static function getInstance($skipCreate=false)
  100. {
  101. if (self::$_instance===null && $skipCreate!==true) {
  102. return self::init();
  103. }
  104. return self::$_instance;
  105. }
  106. /**
  107. * Destroys the singleton instance
  108. *
  109. * Primarily used for testing.
  110. *
  111. * @return void
  112. */
  113. public static function destroyInstance()
  114. {
  115. self::$_instance = null;
  116. }
  117. /**
  118. * Get the instance of a give protocol for this channel
  119. *
  120. * @param string $uri The URI for the protocol
  121. * @return object Returns the protocol instance for the diven URI
  122. */
  123. public function getProtocol($uri)
  124. {
  125. if (!isset($this->_protocols[$uri])) {
  126. $this->_protocols[$uri] = $this->_initProtocol($uri);
  127. }
  128. $this->_registerControllerPlugin();
  129. return $this->_protocols[$uri];
  130. }
  131. /**
  132. * Initialize a new protocol
  133. *
  134. * @param string $uri The URI for the protocol to be initialized
  135. * @return object Returns the new initialized protocol instance
  136. * @throws Zend_Wildfire_Exception
  137. */
  138. protected function _initProtocol($uri)
  139. {
  140. switch ($uri) {
  141. case Zend_Wildfire_Protocol_JsonStream::PROTOCOL_URI;
  142. return new Zend_Wildfire_Protocol_JsonStream();
  143. }
  144. throw new Zend_Wildfire_Exception('Tyring to initialize unknown protocol for URI "'.$uri.'".');
  145. }
  146. /**
  147. * Flush all data from all protocols and send all data to response headers.
  148. *
  149. * @return boolean Returns TRUE if data was flushed
  150. */
  151. public function flush()
  152. {
  153. if (!$this->_protocols || !$this->isReady()) {
  154. return false;
  155. }
  156. foreach ( $this->_protocols as $protocol ) {
  157. $payload = $protocol->getPayload($this);
  158. if ($payload) {
  159. foreach( $payload as $message ) {
  160. $this->getResponse()->setHeader(self::$_headerPrefix.$message[0],
  161. $message[1], true);
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * Set the index of the plugin in the controller dispatch loop plugin stack
  169. *
  170. * @param integer $index The index of the plugin in the stack
  171. * @return integer The previous index.
  172. */
  173. public static function setControllerPluginStackIndex($index)
  174. {
  175. $previous = self::$_controllerPluginStackIndex;
  176. self::$_controllerPluginStackIndex = $index;
  177. return $previous;
  178. }
  179. /**
  180. * Register this object as a controller plugin.
  181. *
  182. * @return void
  183. */
  184. protected function _registerControllerPlugin()
  185. {
  186. $controller = Zend_Controller_Front::getInstance();
  187. if (!$controller->hasPlugin(get_class($this))) {
  188. $controller->registerPlugin($this, self::$_controllerPluginStackIndex);
  189. }
  190. }
  191. /*
  192. * Zend_Wildfire_Channel_Interface
  193. */
  194. /**
  195. * Determine if channel is ready.
  196. *
  197. * @return boolean Returns TRUE if channel is ready.
  198. */
  199. public function isReady()
  200. {
  201. return ($this->getResponse()->canSendHeaders() &&
  202. preg_match_all('/\s?FirePHP\/([\.|\d]*)\s?/si',
  203. $this->getRequest()->getHeader('User-Agent'),$m));
  204. }
  205. /*
  206. * Zend_Controller_Plugin_Abstract
  207. */
  208. /**
  209. * Flush messages to headers as late as possible but before headers have been sent.
  210. *
  211. * @return void
  212. */
  213. public function dispatchLoopShutdown()
  214. {
  215. $this->flush();
  216. }
  217. /**
  218. * Get the request object
  219. *
  220. * @return Zend_Controller_Request_Abstract
  221. * @throws Zend_Wildfire_Exception
  222. */
  223. public function getRequest()
  224. {
  225. if (!$this->_request) {
  226. $controller = Zend_Controller_Front::getInstance();
  227. $this->setRequest($controller->getRequest());
  228. }
  229. if (!$this->_request) {
  230. throw new Zend_Wildfire_Exception('Request objects not initialized.');
  231. }
  232. return $this->_request;
  233. }
  234. /**
  235. * Get the response object
  236. *
  237. * @return Zend_Controller_Response_Abstract
  238. * @throws Zend_Wildfire_Exception
  239. */
  240. public function getResponse()
  241. {
  242. if (!$this->_response) {
  243. $response = Zend_Controller_Front::getInstance()->getResponse();
  244. if ($response) {
  245. $this->setResponse($response);
  246. }
  247. }
  248. if (!$this->_response) {
  249. throw new Zend_Wildfire_Exception('Response objects not initialized.');
  250. }
  251. return $this->_response;
  252. }
  253. }