File indexing completed on 2024-12-22 05:37:14
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Wildfire 0017 * @subpackage Protocol 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Wildfire_Plugin_Interface */ 0024 // require_once 'Zend/Wildfire/Plugin/Interface.php'; 0025 0026 /** Zend_Wildfire_Channel_Interface */ 0027 // require_once 'Zend/Wildfire/Channel/Interface.php'; 0028 0029 /** Zend_Json */ 0030 // require_once 'Zend/Json.php'; 0031 0032 /** 0033 * Encodes messages into the Wildfire JSON Stream Communication Protocol. 0034 * 0035 * @category Zend 0036 * @package Zend_Wildfire 0037 * @subpackage Protocol 0038 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0039 * @license http://framework.zend.com/license/new-bsd New BSD License 0040 */ 0041 class Zend_Wildfire_Protocol_JsonStream 0042 { 0043 /** 0044 * The protocol URI for this protocol 0045 */ 0046 const PROTOCOL_URI = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2'; 0047 0048 /** 0049 * All messages to be sent. 0050 * @var array 0051 */ 0052 protected $_messages = array(); 0053 0054 /** 0055 * Plugins that are using this protocol 0056 * @var array 0057 */ 0058 protected $_plugins = array(); 0059 0060 /** 0061 * Register a plugin that uses this protocol 0062 * 0063 * @param Zend_Wildfire_Plugin_Interface $plugin The plugin to be registered 0064 * @return boolean Returns TRUE if plugin was registered, false if it was already registered 0065 */ 0066 public function registerPlugin(Zend_Wildfire_Plugin_Interface $plugin) 0067 { 0068 if (in_array($plugin,$this->_plugins)) { 0069 return false; 0070 } 0071 $this->_plugins[] = $plugin; 0072 return true; 0073 } 0074 0075 /** 0076 * Record a message with the given data in the given structure 0077 * 0078 * @param Zend_Wildfire_Plugin_Interface $plugin The plugin recording the message 0079 * @param string $structure The structure to be used for the data 0080 * @param array $data The data to be recorded 0081 * @return boolean Returns TRUE if message was recorded 0082 */ 0083 public function recordMessage(Zend_Wildfire_Plugin_Interface $plugin, $structure, $data) 0084 { 0085 if(!isset($this->_messages[$structure])) { 0086 $this->_messages[$structure] = array(); 0087 } 0088 0089 $uri = $plugin->getUri(); 0090 0091 if(!isset($this->_messages[$structure][$uri])) { 0092 $this->_messages[$structure][$uri] = array(); 0093 } 0094 0095 $this->_messages[$structure][$uri][] = $this->_encode($data); 0096 return true; 0097 } 0098 0099 /** 0100 * Remove all qued messages 0101 * 0102 * @param Zend_Wildfire_Plugin_Interface $plugin The plugin for which to clear messages 0103 * @return boolean Returns TRUE if messages were present 0104 */ 0105 public function clearMessages(Zend_Wildfire_Plugin_Interface $plugin) 0106 { 0107 $uri = $plugin->getUri(); 0108 0109 $present = false; 0110 foreach ($this->_messages as $structure => $messages) { 0111 0112 if(!isset($this->_messages[$structure][$uri])) { 0113 continue; 0114 } 0115 0116 $present = true; 0117 0118 unset($this->_messages[$structure][$uri]); 0119 0120 if (!$this->_messages[$structure]) { 0121 unset($this->_messages[$structure]); 0122 } 0123 } 0124 return $present; 0125 } 0126 0127 /** 0128 * Get all qued messages 0129 * 0130 * @return mixed Returns qued messages or FALSE if no messages are qued 0131 */ 0132 public function getMessages() 0133 { 0134 if (!$this->_messages) { 0135 return false; 0136 } 0137 return $this->_messages; 0138 } 0139 0140 /** 0141 * Use the JSON encoding scheme for the value specified 0142 * 0143 * @param mixed $value The value to be encoded 0144 * @return string The encoded value 0145 */ 0146 protected function _encode($value) 0147 { 0148 return Zend_Json::encode($value, true, array('silenceCyclicalExceptions'=>true)); 0149 } 0150 0151 /** 0152 * Retrieves all formatted data ready to be sent by the channel. 0153 * 0154 * @param Zend_Wildfire_Channel_Interface $channel The instance of the channel that will be transmitting the data 0155 * @return mixed Returns the data to be sent by the channel. 0156 * @throws Zend_Wildfire_Exception 0157 */ 0158 public function getPayload(Zend_Wildfire_Channel_Interface $channel) 0159 { 0160 if (!$channel instanceof Zend_Wildfire_Channel_HttpHeaders) { 0161 // require_once 'Zend/Wildfire/Exception.php'; 0162 throw new Zend_Wildfire_Exception('The '.get_class($channel).' channel is not supported by the '.get_class($this).' protocol.'); 0163 } 0164 0165 if ($this->_plugins) { 0166 foreach ($this->_plugins as $plugin) { 0167 $plugin->flushMessages(self::PROTOCOL_URI); 0168 } 0169 } 0170 0171 if (!$this->_messages) { 0172 return false; 0173 } 0174 0175 $protocol_index = 1; 0176 $structure_index = 1; 0177 $plugin_index = 1; 0178 $message_index = 1; 0179 0180 $payload = array(); 0181 0182 $payload[] = array('Protocol-'.$protocol_index, self::PROTOCOL_URI); 0183 0184 foreach ($this->_messages as $structure_uri => $plugin_messages ) { 0185 0186 $payload[] = array($protocol_index.'-Structure-'.$structure_index, $structure_uri); 0187 0188 foreach ($plugin_messages as $plugin_uri => $messages ) { 0189 0190 $payload[] = array($protocol_index.'-Plugin-'.$plugin_index, $plugin_uri); 0191 0192 foreach ($messages as $message) { 0193 0194 $parts = explode("\n",chunk_split($message, 5000, "\n")); 0195 0196 for ($i=0 ; $i<count($parts) ; $i++) { 0197 0198 $part = $parts[$i]; 0199 if ($part) { 0200 0201 $msg = ''; 0202 0203 if (count($parts)>2) { 0204 $msg = (($i==0)?strlen($message):'') 0205 . '|' . $part . '|' 0206 . (($i<count($parts)-2)?'\\':''); 0207 } else { 0208 $msg = strlen($part) . '|' . $part . '|'; 0209 } 0210 0211 $payload[] = array($protocol_index . '-' 0212 . $structure_index . '-' 0213 . $plugin_index . '-' 0214 . $message_index, 0215 $msg); 0216 0217 $message_index++; 0218 0219 if ($message_index > 99999) { 0220 // require_once 'Zend/Wildfire/Exception.php'; 0221 throw new Zend_Wildfire_Exception('Maximum number (99,999) of messages reached!'); 0222 } 0223 } 0224 } 0225 } 0226 $plugin_index++; 0227 } 0228 $structure_index++; 0229 } 0230 0231 return $payload; 0232 } 0233 0234 } 0235