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

/library/Zend/Wildfire/Protocol/JsonStream.php

http://github.com/michael-romer/zf-boilerplate
PHP | 235 lines | 119 code | 35 blank | 81 comment | 12 complexity | fa0c57f687052ae5301451c1931972c6 MD5 | raw file
Possible License(s): Unlicense, 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 Protocol
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: JsonStream.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /** Zend_Wildfire_Plugin_Interface */
  23. require_once 'Zend/Wildfire/Plugin/Interface.php';
  24. /** Zend_Wildfire_Channel_Interface */
  25. require_once 'Zend/Wildfire/Channel/Interface.php';
  26. /** Zend_Json */
  27. require_once 'Zend/Json.php';
  28. /**
  29. * Encodes messages into the Wildfire JSON Stream Communication Protocol.
  30. *
  31. * @category Zend
  32. * @package Zend_Wildfire
  33. * @subpackage Protocol
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Wildfire_Protocol_JsonStream
  38. {
  39. /**
  40. * The protocol URI for this protocol
  41. */
  42. const PROTOCOL_URI = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
  43. /**
  44. * All messages to be sent.
  45. * @var array
  46. */
  47. protected $_messages = array();
  48. /**
  49. * Plugins that are using this protocol
  50. * @var array
  51. */
  52. protected $_plugins = array();
  53. /**
  54. * Register a plugin that uses this protocol
  55. *
  56. * @param Zend_Wildfire_Plugin_Interface $plugin The plugin to be registered
  57. * @return boolean Returns TRUE if plugin was registered, false if it was already registered
  58. */
  59. public function registerPlugin(Zend_Wildfire_Plugin_Interface $plugin)
  60. {
  61. if (in_array($plugin,$this->_plugins)) {
  62. return false;
  63. }
  64. $this->_plugins[] = $plugin;
  65. return true;
  66. }
  67. /**
  68. * Record a message with the given data in the given structure
  69. *
  70. * @param Zend_Wildfire_Plugin_Interface $plugin The plugin recording the message
  71. * @param string $structure The structure to be used for the data
  72. * @param array $data The data to be recorded
  73. * @return boolean Returns TRUE if message was recorded
  74. */
  75. public function recordMessage(Zend_Wildfire_Plugin_Interface $plugin, $structure, $data)
  76. {
  77. if(!isset($this->_messages[$structure])) {
  78. $this->_messages[$structure] = array();
  79. }
  80. $uri = $plugin->getUri();
  81. if(!isset($this->_messages[$structure][$uri])) {
  82. $this->_messages[$structure][$uri] = array();
  83. }
  84. $this->_messages[$structure][$uri][] = $this->_encode($data);
  85. return true;
  86. }
  87. /**
  88. * Remove all qued messages
  89. *
  90. * @param Zend_Wildfire_Plugin_Interface $plugin The plugin for which to clear messages
  91. * @return boolean Returns TRUE if messages were present
  92. */
  93. public function clearMessages(Zend_Wildfire_Plugin_Interface $plugin)
  94. {
  95. $uri = $plugin->getUri();
  96. $present = false;
  97. foreach ($this->_messages as $structure => $messages) {
  98. if(!isset($this->_messages[$structure][$uri])) {
  99. continue;
  100. }
  101. $present = true;
  102. unset($this->_messages[$structure][$uri]);
  103. if (!$this->_messages[$structure]) {
  104. unset($this->_messages[$structure]);
  105. }
  106. }
  107. return $present;
  108. }
  109. /**
  110. * Get all qued messages
  111. *
  112. * @return mixed Returns qued messages or FALSE if no messages are qued
  113. */
  114. public function getMessages()
  115. {
  116. if (!$this->_messages) {
  117. return false;
  118. }
  119. return $this->_messages;
  120. }
  121. /**
  122. * Use the JSON encoding scheme for the value specified
  123. *
  124. * @param mixed $value The value to be encoded
  125. * @return string The encoded value
  126. */
  127. protected function _encode($value)
  128. {
  129. return Zend_Json::encode($value, true, array('silenceCyclicalExceptions'=>true));
  130. }
  131. /**
  132. * Retrieves all formatted data ready to be sent by the channel.
  133. *
  134. * @param Zend_Wildfire_Channel_Interface $channel The instance of the channel that will be transmitting the data
  135. * @return mixed Returns the data to be sent by the channel.
  136. * @throws Zend_Wildfire_Exception
  137. */
  138. public function getPayload(Zend_Wildfire_Channel_Interface $channel)
  139. {
  140. if (!$channel instanceof Zend_Wildfire_Channel_HttpHeaders) {
  141. require_once 'Zend/Wildfire/Exception.php';
  142. throw new Zend_Wildfire_Exception('The '.get_class($channel).' channel is not supported by the '.get_class($this).' protocol.');
  143. }
  144. if ($this->_plugins) {
  145. foreach ($this->_plugins as $plugin) {
  146. $plugin->flushMessages(self::PROTOCOL_URI);
  147. }
  148. }
  149. if (!$this->_messages) {
  150. return false;
  151. }
  152. $protocol_index = 1;
  153. $structure_index = 1;
  154. $plugin_index = 1;
  155. $message_index = 1;
  156. $payload = array();
  157. $payload[] = array('Protocol-'.$protocol_index, self::PROTOCOL_URI);
  158. foreach ($this->_messages as $structure_uri => $plugin_messages ) {
  159. $payload[] = array($protocol_index.'-Structure-'.$structure_index, $structure_uri);
  160. foreach ($plugin_messages as $plugin_uri => $messages ) {
  161. $payload[] = array($protocol_index.'-Plugin-'.$plugin_index, $plugin_uri);
  162. foreach ($messages as $message) {
  163. $parts = explode("\n",chunk_split($message, 5000, "\n"));
  164. for ($i=0 ; $i<count($parts) ; $i++) {
  165. $part = $parts[$i];
  166. if ($part) {
  167. $msg = '';
  168. if (count($parts)>2) {
  169. $msg = (($i==0)?strlen($message):'')
  170. . '|' . $part . '|'
  171. . (($i<count($parts)-2)?'\\':'');
  172. } else {
  173. $msg = strlen($part) . '|' . $part . '|';
  174. }
  175. $payload[] = array($protocol_index . '-'
  176. . $structure_index . '-'
  177. . $plugin_index . '-'
  178. . $message_index,
  179. $msg);
  180. $message_index++;
  181. if ($message_index > 99999) {
  182. require_once 'Zend/Wildfire/Exception.php';
  183. throw new Zend_Wildfire_Exception('Maximum number (99,999) of messages reached!');
  184. }
  185. }
  186. }
  187. }
  188. $plugin_index++;
  189. }
  190. $structure_index++;
  191. }
  192. return $payload;
  193. }
  194. }