/library/Zend/Queue/Stomp/Frame.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 363 lines · 152 code · 42 blank · 169 comment · 16 complexity · dc67915c32c11ba12a3f4588875b781d 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_Queue
  17. * @subpackage Stomp
  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$
  21. */
  22. /**
  23. * @see Zend_Queue_Stomp_FrameInterface
  24. */
  25. // require_once 'Zend/Queue/Stomp/FrameInterface.php';
  26. /**
  27. * This class represents a Stomp Frame
  28. *
  29. * @category Zend
  30. * @package Zend_Queue
  31. * @subpackage Stomp
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Queue_Stomp_Frame
  36. implements Zend_Queue_Stomp_FrameInterface
  37. {
  38. const END_OF_FRAME = "\x00\n";
  39. const CONTENT_LENGTH = 'content-length';
  40. const EOL = "\n";
  41. /**
  42. * Headers for the frame
  43. *
  44. * @var array
  45. */
  46. protected $_headers = array();
  47. /**
  48. * The command for the frame
  49. *
  50. * @var string
  51. */
  52. protected $_command = null;
  53. /**
  54. * The body of the frame
  55. *
  56. * @var string
  57. */
  58. protected $_body = null;
  59. /**
  60. * Do the content-length automatically?
  61. */
  62. protected $_autoContentLength = null;
  63. /**
  64. * Constructor
  65. */
  66. public function __construct()
  67. {
  68. $this->setHeaders(array());
  69. $this->setBody(null);
  70. $this->setCommand(null);
  71. $this->setAutoContentLength(true);
  72. }
  73. /**
  74. * get the status of the auto content length
  75. *
  76. * If AutoContentLength is true this code will automatically put the
  77. * content-length header in, even if it is already set by the user.
  78. *
  79. * This is done to make the message sending more reliable.
  80. *
  81. * @return boolean
  82. */
  83. public function getAutoContentLength()
  84. {
  85. return $this->_autoContentLength;
  86. }
  87. /**
  88. * setAutoContentLength()
  89. *
  90. * Set the value on or off.
  91. *
  92. * @param boolean $auto
  93. * @return $this;
  94. * @throws Zend_Queue_Exception
  95. */
  96. public function setAutoContentLength($auto)
  97. {
  98. if (!is_bool($auto)) {
  99. // require_once 'Zend/Queue/Exception.php';
  100. throw new Zend_Queue_Exception('$auto is not a boolean');
  101. }
  102. $this->_autoContentLength = $auto;
  103. return $this;
  104. }
  105. /**
  106. * Get the headers
  107. *
  108. * @return array
  109. */
  110. public function getHeaders()
  111. {
  112. return $this->_headers;
  113. }
  114. /**
  115. * Set the headers
  116. *
  117. * Throws an exception if the array values are not strings.
  118. *
  119. * @param array $headers
  120. * @return $this
  121. * @throws Zend_Queue_Exception
  122. */
  123. public function setHeaders(array $headers)
  124. {
  125. foreach ($headers as $header => $value) {
  126. $this->setHeader($header, $value);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Sets a value for a header
  132. *
  133. * @param string $header
  134. * @param string $value
  135. * @return Zend_Queue_Stomp_Frame
  136. * @throws Zend_Queue_Exception
  137. */
  138. public function setHeader($header, $value) {
  139. if (!is_string($header)) {
  140. // require_once 'Zend/Queue/Exception.php';
  141. throw new Zend_Queue_Exception('$header is not a string: ' . print_r($header, true));
  142. }
  143. if (!is_scalar($value)) {
  144. // require_once 'Zend/Queue/Exception.php';
  145. throw new Zend_Queue_Exception('$value is not a string: ' . print_r($value, true));
  146. }
  147. $this->_headers[$header] = $value;
  148. return $this;
  149. }
  150. /**
  151. * Returns a value for a header
  152. *
  153. * Returns false if the header does not exist.
  154. *
  155. * @param string $header
  156. * @return string|false
  157. * @throws Zend_Queue_Exception
  158. */
  159. public function getHeader($header)
  160. {
  161. if (!is_string($header)) {
  162. // require_once 'Zend/Queue/Exception.php';
  163. throw new Zend_Queue_Exception('$header is not a string');
  164. }
  165. return isset($this->_headers[$header])
  166. ? $this->_headers[$header]
  167. : false;
  168. }
  169. /**
  170. * Return the body for this frame
  171. *
  172. * Returns false if the body does not exist
  173. *
  174. * @return false|string
  175. */
  176. public function getBody()
  177. {
  178. return $this->_body === null
  179. ? false
  180. : $this->_body;
  181. }
  182. /**
  183. * Set the body for this frame
  184. *
  185. * Set to null for no body.
  186. *
  187. * @param string|null $body
  188. * @return Zend_Queue_Stomp_Frame
  189. * @throws Zend_Queue_Exception
  190. */
  191. public function setBody($body)
  192. {
  193. if (!is_string($body) && $body !== null) {
  194. // require_once 'Zend/Queue/Exception.php';
  195. throw new Zend_Queue_Exception('$body is not a string or null');
  196. }
  197. $this->_body = $body;
  198. return $this;
  199. }
  200. /**
  201. * Return the command for this frame
  202. *
  203. * Return false if the command does not exist
  204. *
  205. * @return string|false
  206. */
  207. public function getCommand()
  208. {
  209. return $this->_command === null
  210. ? false
  211. : $this->_command;
  212. }
  213. /**
  214. * Set the body for this frame
  215. *
  216. * @param string|null
  217. * @return Zend_Queue_Stomp_Frame
  218. * @throws Zend_Queue_Exception
  219. */
  220. public function setCommand($command)
  221. {
  222. if (!is_string($command) && $command !== null) {
  223. // require_once 'Zend/Queue/Exception.php';
  224. throw new Zend_Queue_Exception('$command is not a string or null');
  225. }
  226. $this->_command = $command;
  227. return $this;
  228. }
  229. /**
  230. * Takes the current parameters and returns a Stomp Frame
  231. *
  232. * @return string
  233. * @throws Zend_Queue_Exception
  234. */
  235. public function toFrame()
  236. {
  237. if ($this->getCommand() === false) {
  238. // require_once 'Zend/Queue/Exception.php';
  239. throw new Zend_Queue_Exception('You must set the command');
  240. }
  241. $command = $this->getCommand();
  242. $headers = $this->getHeaders();
  243. $body = $this->getBody();
  244. $frame = '';
  245. // add a content-length to the SEND command.
  246. // @see http://stomp.codehaus.org/Protocol
  247. if ($this->getAutoContentLength()) {
  248. $headers[self::CONTENT_LENGTH] = strlen($this->getBody());
  249. }
  250. // Command
  251. $frame = $command . self::EOL;
  252. // Headers
  253. foreach ($headers as $key=>$value) {
  254. $frame .= $key . ': ' . $value . self::EOL;
  255. }
  256. // Seperator
  257. $frame .= self::EOL; // blank line required by protocol
  258. // add the body if any
  259. if ($body !== false) {
  260. $frame .= $body;
  261. }
  262. $frame .= self::END_OF_FRAME;
  263. return $frame;
  264. }
  265. /**
  266. * @see toFrame()
  267. * @return string
  268. */
  269. public function __toString()
  270. {
  271. try {
  272. $return = $this->toFrame();
  273. } catch (Zend_Queue_Exception $e) {
  274. $return = '';
  275. }
  276. return $return;
  277. }
  278. /**
  279. * Accepts a frame and deconstructs the frame into its component parts
  280. *
  281. * @param string $frame - a stomp frame
  282. * @return $this
  283. */
  284. public function fromFrame($frame)
  285. {
  286. if (!is_string($frame)) {
  287. // require_once 'Zend/Queue/Exception.php';
  288. throw new Zend_Queue_Exception('$frame is not a string');
  289. }
  290. $headers = array();
  291. $body = null;
  292. $command = false;
  293. $header = '';
  294. // separate the headers and the body
  295. $match = self::EOL . self::EOL;
  296. if (preg_match('/' . $match . '/', $frame)) {
  297. list ($header, $body) = explode($match, $frame, 2);
  298. } else {
  299. $header = $frame;
  300. }
  301. // blow up headers
  302. $headers = explode(self::EOL, $header);
  303. unset($header);
  304. // get the command (first line)
  305. $this->setCommand(array_shift($headers));
  306. // set each of the headers.
  307. foreach ($headers as $header) {
  308. if (strpos($header, ':') > 0) {
  309. list($name, $value) = explode(':', $header, 2);
  310. $this->setHeader($name, $value);
  311. }
  312. }
  313. // crop the body if content-length is present
  314. if ($this->getHeader(self::CONTENT_LENGTH) !== false ) {
  315. $length = (int) $this->getHeader(self::CONTENT_LENGTH);
  316. $body = substr($body, 0, $length);
  317. }
  318. $this->setBody($body);
  319. return $this;
  320. }
  321. }