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

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