/xampp/htdocs/magento/lib/Zend/Wildfire/Protocol/JsonStream.php

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