File indexing completed on 2025-01-19 05:21:24
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_Queue 0017 * @subpackage Stomp 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 /** 0024 * @see Zend_Queue_Stomp_FrameInterface 0025 */ 0026 // require_once 'Zend/Queue/Stomp/FrameInterface.php'; 0027 0028 /** 0029 * This class represents a Stomp Frame 0030 * 0031 * @category Zend 0032 * @package Zend_Queue 0033 * @subpackage Stomp 0034 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0035 * @license http://framework.zend.com/license/new-bsd New BSD License 0036 */ 0037 class Zend_Queue_Stomp_Frame 0038 implements Zend_Queue_Stomp_FrameInterface 0039 { 0040 const END_OF_FRAME = "\x00\n"; 0041 const CONTENT_LENGTH = 'content-length'; 0042 const EOL = "\n"; 0043 0044 /** 0045 * Headers for the frame 0046 * 0047 * @var array 0048 */ 0049 protected $_headers = array(); 0050 0051 /** 0052 * The command for the frame 0053 * 0054 * @var string 0055 */ 0056 protected $_command = null; 0057 0058 /** 0059 * The body of the frame 0060 * 0061 * @var string 0062 */ 0063 protected $_body = null; 0064 0065 /** 0066 * Do the content-length automatically? 0067 */ 0068 protected $_autoContentLength = null; 0069 0070 /** 0071 * Constructor 0072 */ 0073 public function __construct() 0074 { 0075 $this->setHeaders(array()); 0076 $this->setBody(null); 0077 $this->setCommand(null); 0078 $this->setAutoContentLength(true); 0079 } 0080 0081 /** 0082 * get the status of the auto content length 0083 * 0084 * If AutoContentLength is true this code will automatically put the 0085 * content-length header in, even if it is already set by the user. 0086 * 0087 * This is done to make the message sending more reliable. 0088 * 0089 * @return boolean 0090 */ 0091 public function getAutoContentLength() 0092 { 0093 return $this->_autoContentLength; 0094 } 0095 0096 /** 0097 * setAutoContentLength() 0098 * 0099 * Set the value on or off. 0100 * 0101 * @param boolean $auto 0102 * @return $this; 0103 * @throws Zend_Queue_Exception 0104 */ 0105 public function setAutoContentLength($auto) 0106 { 0107 if (!is_bool($auto)) { 0108 // require_once 'Zend/Queue/Exception.php'; 0109 throw new Zend_Queue_Exception('$auto is not a boolean'); 0110 } 0111 0112 $this->_autoContentLength = $auto; 0113 return $this; 0114 } 0115 0116 /** 0117 * Get the headers 0118 * 0119 * @return array 0120 */ 0121 public function getHeaders() 0122 { 0123 return $this->_headers; 0124 } 0125 0126 /** 0127 * Set the headers 0128 * 0129 * Throws an exception if the array values are not strings. 0130 * 0131 * @param array $headers 0132 * @return $this 0133 * @throws Zend_Queue_Exception 0134 */ 0135 public function setHeaders(array $headers) 0136 { 0137 foreach ($headers as $header => $value) { 0138 $this->setHeader($header, $value); 0139 } 0140 0141 return $this; 0142 } 0143 0144 /** 0145 * Sets a value for a header 0146 * 0147 * @param string $header 0148 * @param string $value 0149 * @return Zend_Queue_Stomp_Frame 0150 * @throws Zend_Queue_Exception 0151 */ 0152 public function setHeader($header, $value) { 0153 if (!is_string($header)) { 0154 // require_once 'Zend/Queue/Exception.php'; 0155 throw new Zend_Queue_Exception('$header is not a string: ' . print_r($header, true)); 0156 } 0157 0158 if (!is_scalar($value)) { 0159 // require_once 'Zend/Queue/Exception.php'; 0160 throw new Zend_Queue_Exception('$value is not a string: ' . print_r($value, true)); 0161 } 0162 0163 $this->_headers[$header] = $value; 0164 return $this; 0165 } 0166 0167 0168 /** 0169 * Returns a value for a header 0170 * 0171 * Returns false if the header does not exist. 0172 * 0173 * @param string $header 0174 * @return string|false 0175 * @throws Zend_Queue_Exception 0176 */ 0177 public function getHeader($header) 0178 { 0179 if (!is_string($header)) { 0180 // require_once 'Zend/Queue/Exception.php'; 0181 throw new Zend_Queue_Exception('$header is not a string'); 0182 } 0183 0184 return isset($this->_headers[$header]) 0185 ? $this->_headers[$header] 0186 : false; 0187 } 0188 0189 /** 0190 * Return the body for this frame 0191 * 0192 * Returns false if the body does not exist 0193 * 0194 * @return false|string 0195 */ 0196 public function getBody() 0197 { 0198 return $this->_body === null 0199 ? false 0200 : $this->_body; 0201 } 0202 0203 /** 0204 * Set the body for this frame 0205 * 0206 * Set to null for no body. 0207 * 0208 * @param string|null $body 0209 * @return Zend_Queue_Stomp_Frame 0210 * @throws Zend_Queue_Exception 0211 */ 0212 public function setBody($body) 0213 { 0214 if (!is_string($body) && $body !== null) { 0215 // require_once 'Zend/Queue/Exception.php'; 0216 throw new Zend_Queue_Exception('$body is not a string or null'); 0217 } 0218 0219 $this->_body = $body; 0220 return $this; 0221 } 0222 0223 /** 0224 * Return the command for this frame 0225 * 0226 * Return false if the command does not exist 0227 * 0228 * @return string|false 0229 */ 0230 public function getCommand() 0231 { 0232 return $this->_command === null 0233 ? false 0234 : $this->_command; 0235 } 0236 0237 /** 0238 * Set the body for this frame 0239 * 0240 * @param string|null 0241 * @return Zend_Queue_Stomp_Frame 0242 * @throws Zend_Queue_Exception 0243 */ 0244 public function setCommand($command) 0245 { 0246 if (!is_string($command) && $command !== null) { 0247 // require_once 'Zend/Queue/Exception.php'; 0248 throw new Zend_Queue_Exception('$command is not a string or null'); 0249 } 0250 0251 $this->_command = $command; 0252 return $this; 0253 } 0254 0255 /** 0256 * Takes the current parameters and returns a Stomp Frame 0257 * 0258 * @return string 0259 * @throws Zend_Queue_Exception 0260 */ 0261 public function toFrame() 0262 { 0263 if ($this->getCommand() === false) { 0264 // require_once 'Zend/Queue/Exception.php'; 0265 throw new Zend_Queue_Exception('You must set the command'); 0266 } 0267 0268 $command = $this->getCommand(); 0269 $headers = $this->getHeaders(); 0270 $body = $this->getBody(); 0271 $frame = ''; 0272 0273 // add a content-length to the SEND command. 0274 // @see http://stomp.codehaus.org/Protocol 0275 if ($this->getAutoContentLength()) { 0276 $headers[self::CONTENT_LENGTH] = strlen($this->getBody()); 0277 } 0278 0279 // Command 0280 $frame = $command . self::EOL; 0281 0282 // Headers 0283 foreach ($headers as $key=>$value) { 0284 $frame .= $key . ': ' . $value . self::EOL; 0285 } 0286 0287 // Seperator 0288 $frame .= self::EOL; // blank line required by protocol 0289 0290 // add the body if any 0291 if ($body !== false) { 0292 $frame .= $body; 0293 } 0294 $frame .= self::END_OF_FRAME; 0295 0296 return $frame; 0297 } 0298 0299 /** 0300 * @see toFrame() 0301 * @return string 0302 */ 0303 public function __toString() 0304 { 0305 try { 0306 $return = $this->toFrame(); 0307 } catch (Zend_Queue_Exception $e) { 0308 $return = ''; 0309 } 0310 return $return; 0311 } 0312 0313 /** 0314 * Accepts a frame and deconstructs the frame into its component parts 0315 * 0316 * @param string $frame - a stomp frame 0317 * @return $this 0318 */ 0319 public function fromFrame($frame) 0320 { 0321 if (!is_string($frame)) { 0322 // require_once 'Zend/Queue/Exception.php'; 0323 throw new Zend_Queue_Exception('$frame is not a string'); 0324 } 0325 0326 $headers = array(); 0327 $body = null; 0328 $command = false; 0329 $header = ''; 0330 0331 // separate the headers and the body 0332 $match = self::EOL . self::EOL; 0333 if (preg_match('/' . $match . '/', $frame)) { 0334 list ($header, $body) = explode($match, $frame, 2); 0335 } else { 0336 $header = $frame; 0337 } 0338 0339 // blow up headers 0340 $headers = explode(self::EOL, $header); 0341 unset($header); 0342 0343 // get the command (first line) 0344 $this->setCommand(array_shift($headers)); 0345 0346 // set each of the headers. 0347 foreach ($headers as $header) { 0348 if (strpos($header, ':') > 0) { 0349 list($name, $value) = explode(':', $header, 2); 0350 $this->setHeader($name, $value); 0351 } 0352 } 0353 0354 // crop the body if content-length is present 0355 if ($this->getHeader(self::CONTENT_LENGTH) !== false ) { 0356 $length = (int) $this->getHeader(self::CONTENT_LENGTH); 0357 $body = substr($body, 0, $length); 0358 } 0359 0360 $this->setBody($body); 0361 return $this; 0362 } 0363 }