PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Wildfire/Channel/HttpHeaders.php

http://firephp.googlecode.com/
PHP | 325 lines | 135 code | 37 blank | 153 comment | 21 complexity | 1c067274b52f05fc20cbe852217bc6c1 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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_Controller_Request_Abstract */
  24. require_once('Zend/Controller/Request/Abstract.php');
  25. /** Zend_Controller_Response_Abstract */
  26. require_once('Zend/Controller/Response/Abstract.php');
  27. /** Zend_Controller_Plugin_Abstract */
  28. require_once 'Zend/Controller/Plugin/Abstract.php';
  29. /** Zend_Wildfire_Protocol_JsonStream */
  30. require_once 'Zend/Wildfire/Protocol/JsonStream.php';
  31. /** Zend_Controller_Front **/
  32. require_once 'Zend/Controller/Front.php';
  33. /**
  34. * Implements communication via HTTP request and response headers for Wildfire Protocols.
  35. *
  36. * @category Zend
  37. * @package Zend_Wildfire
  38. * @subpackage Channel
  39. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Wildfire_Channel_HttpHeaders extends Zend_Controller_Plugin_Abstract implements Zend_Wildfire_Channel_Interface
  43. {
  44. /**
  45. * The string to be used to prefix the headers.
  46. * @var string
  47. */
  48. protected static $_headerPrefix = 'X-WF-';
  49. /**
  50. * Singleton instance
  51. * @var Zend_Wildfire_Channel_HttpHeaders
  52. */
  53. protected static $_instance = null;
  54. /**
  55. * The index of the plugin in the controller dispatch loop plugin stack
  56. * @var integer
  57. */
  58. protected static $_controllerPluginStackIndex = 999;
  59. /**
  60. * The protocol instances for this channel
  61. * @var array
  62. */
  63. protected $_protocols = null;
  64. /**
  65. * Initialize singleton instance.
  66. *
  67. * @param string $class OPTIONAL Subclass of Zend_Wildfire_Channel_HttpHeaders
  68. * @return Zend_Wildfire_Channel_HttpHeaders Returns the singleton Zend_Wildfire_Channel_HttpHeaders instance
  69. * @throws Zend_Wildfire_Exception
  70. */
  71. public static function init($class = null)
  72. {
  73. if (self::$_instance!==null) {
  74. require_once 'Zend/Wildfire/Exception.php';
  75. throw new Zend_Wildfire_Exception('Singleton instance of Zend_Wildfire_Channel_HttpHeaders already exists!');
  76. }
  77. if ($class!==null) {
  78. if (!is_string($class)) {
  79. require_once 'Zend/Wildfire/Exception.php';
  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. require_once 'Zend/Wildfire/Exception.php';
  87. throw new Zend_Wildfire_Exception('Invalid class to third argument. Must be subclass of Zend_Wildfire_Channel_HttpHeaders.');
  88. }
  89. } else {
  90. self::$_instance = new self();
  91. }
  92. return self::$_instance;
  93. }
  94. /**
  95. * Get or create singleton instance
  96. *
  97. * @param $skipCreate boolean True if an instance should not be created
  98. * @return Zend_Wildfire_Channel_HttpHeaders
  99. */
  100. public static function getInstance($skipCreate=false)
  101. {
  102. if (self::$_instance===null && $skipCreate!==true) {
  103. return self::init();
  104. }
  105. return self::$_instance;
  106. }
  107. /**
  108. * Destroys the singleton instance
  109. *
  110. * Primarily used for testing.
  111. *
  112. * @return void
  113. */
  114. public static function destroyInstance()
  115. {
  116. self::$_instance = null;
  117. }
  118. /**
  119. * Get the instance of a give protocol for this channel
  120. *
  121. * @param string $uri The URI for the protocol
  122. * @return object Returns the protocol instance for the diven URI
  123. */
  124. public function getProtocol($uri)
  125. {
  126. if (!isset($this->_protocols[$uri])) {
  127. $this->_protocols[$uri] = $this->_initProtocol($uri);
  128. }
  129. $this->_registerControllerPlugin();
  130. return $this->_protocols[$uri];
  131. }
  132. /**
  133. * Initialize a new protocol
  134. *
  135. * @param string $uri The URI for the protocol to be initialized
  136. * @return object Returns the new initialized protocol instance
  137. * @throws Zend_Wildfire_Exception
  138. */
  139. protected function _initProtocol($uri)
  140. {
  141. switch ($uri) {
  142. case Zend_Wildfire_Protocol_JsonStream::PROTOCOL_URI;
  143. return new Zend_Wildfire_Protocol_JsonStream();
  144. }
  145. require_once 'Zend/Wildfire/Exception.php';
  146. throw new Zend_Wildfire_Exception('Tyring to initialize unknown protocol for URI "'.$uri.'".');
  147. }
  148. /**
  149. * Flush all data from all protocols and send all data to response headers.
  150. *
  151. * @return boolean Returns TRUE if data was flushed
  152. */
  153. public function flush()
  154. {
  155. if (!$this->_protocols || !$this->isReady()) {
  156. return false;
  157. }
  158. foreach ( $this->_protocols as $protocol ) {
  159. $payload = $protocol->getPayload($this);
  160. if ($payload) {
  161. foreach( $payload as $message ) {
  162. $this->getResponse()->setHeader(self::$_headerPrefix.$message[0],
  163. $message[1], true);
  164. }
  165. }
  166. }
  167. return true;
  168. }
  169. /**
  170. * Set the index of the plugin in the controller dispatch loop plugin stack
  171. *
  172. * @param integer $index The index of the plugin in the stack
  173. * @return integer The previous index.
  174. */
  175. public static function setControllerPluginStackIndex($index)
  176. {
  177. $previous = self::$_controllerPluginStackIndex;
  178. self::$_controllerPluginStackIndex = $index;
  179. return $previous;
  180. }
  181. /**
  182. * Register this object as a controller plugin.
  183. *
  184. * @return void
  185. */
  186. protected function _registerControllerPlugin()
  187. {
  188. $controller = Zend_Controller_Front::getInstance();
  189. if (!$controller->hasPlugin(get_class($this))) {
  190. $controller->registerPlugin($this, self::$_controllerPluginStackIndex);
  191. }
  192. }
  193. /*
  194. * Zend_Wildfire_Channel_Interface
  195. */
  196. /**
  197. * Determine if channel is ready.
  198. *
  199. * The channel is ready as long as the request and response objects are initialized,
  200. * can send headers and the FirePHP header exists in the User-Agent.
  201. *
  202. * If the header does not exist in the User-Agent, no appropriate client
  203. * is making this request and the messages should not be sent.
  204. *
  205. * A timing issue arises when messages are logged before the request/response
  206. * objects are initialized. In this case we do not yet know if the client
  207. * will be able to accept the messages. If we consequently indicate that
  208. * the channel is not ready, these messages will be dropped which is in
  209. * most cases not the intended behaviour. The intent is to send them at the
  210. * end of the request when the request/response objects will be available
  211. * for sure.
  212. *
  213. * If the request/response objects are not yet initialized we assume if messages are
  214. * logged, the client will be able to receive them. As soon as the request/response
  215. * objects are availoable and a message is logged this assumption is challenged.
  216. * If the client cannot accept the messages any further messages are dropped
  217. * and messages sent prior are kept but discarded when the channel is finally
  218. * flushed at the end of the request.
  219. *
  220. * When the channel is flushed the $forceCheckRequest option is used to force
  221. * a check of the request/response objects. This is the last verification to ensure
  222. * messages are only sent when the client can accept them.
  223. *
  224. * @param boolean $forceCheckRequest OPTIONAL Set to TRUE if the request must be checked
  225. * @return boolean Returns TRUE if channel is ready.
  226. */
  227. public function isReady($forceCheckRequest=false)
  228. {
  229. if (!$forceCheckRequest
  230. && !$this->_request
  231. && !$this->_response) {
  232. return true;
  233. }
  234. return ($this->getResponse()->canSendHeaders() &&
  235. preg_match_all('/\s?FirePHP\/([\.|\d]*)\s?/si',
  236. $this->getRequest()->getHeader('User-Agent'),$m));
  237. }
  238. /*
  239. * Zend_Controller_Plugin_Abstract
  240. */
  241. /**
  242. * Flush messages to headers as late as possible but before headers have been sent.
  243. *
  244. * @return void
  245. */
  246. public function dispatchLoopShutdown()
  247. {
  248. $this->flush();
  249. }
  250. /**
  251. * Get the request object
  252. *
  253. * @return Zend_Controller_Request_Abstract
  254. * @throws Zend_Wildfire_Exception
  255. */
  256. public function getRequest()
  257. {
  258. if (!$this->_request) {
  259. $controller = Zend_Controller_Front::getInstance();
  260. $this->setRequest($controller->getRequest());
  261. }
  262. if (!$this->_request) {
  263. require_once 'Zend/Wildfire/Exception.php';
  264. throw new Zend_Wildfire_Exception('Request objects not initialized.');
  265. }
  266. return $this->_request;
  267. }
  268. /**
  269. * Get the response object
  270. *
  271. * @return Zend_Controller_Response_Abstract
  272. * @throws Zend_Wildfire_Exception
  273. */
  274. public function getResponse()
  275. {
  276. if (!$this->_response) {
  277. $response = Zend_Controller_Front::getInstance()->getResponse();
  278. if ($response) {
  279. $this->setResponse($response);
  280. }
  281. }
  282. if (!$this->_response) {
  283. require_once 'Zend/Wildfire/Exception.php';
  284. throw new Zend_Wildfire_Exception('Response objects not initialized.');
  285. }
  286. return $this->_response;
  287. }
  288. }