modregister.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Represents a container used for producing and consuming messages.
  12. *
  13. * @abstract Implement a derivative of this class to provide the behavior for
  14. * the abstract methods, or override other public or protected methods at your
  15. * discretion.
  16. *
  17. * @package modx
  18. * @subpackage registry
  19. */
  20. abstract class modRegister {
  21. /**
  22. * A reference to the modX instance the register is loaded by.
  23. * @var modX
  24. * @access public
  25. */
  26. public $modx = null;
  27. /**
  28. * An array of global options applied to the registry.
  29. * @var array
  30. * @access public
  31. */
  32. public $options = null;
  33. /**
  34. * An array of topics and/or messages the register is subscribed to.
  35. * @var array
  36. * @access public
  37. */
  38. public $subscriptions = array();
  39. /**
  40. * An optional current topic to allow writes to relative paths.
  41. * @var string
  42. * @access protected
  43. */
  44. protected $_currentTopic = '/';
  45. /**
  46. * The key identifying this register in a registry.
  47. * @var string
  48. * @access protected
  49. */
  50. protected $_key = null;
  51. /**
  52. * A polling flag that will terminate additional polling when true.
  53. * @var boolean
  54. */
  55. public $__kill = false;
  56. /**
  57. * Construct a new modRegister.
  58. *
  59. * @param modX &$modx A reference to a modX instance.
  60. * @param string $key A valid PHP variable which will be set on the modRegistry instance.
  61. * @param array $options Optional array of registry options.
  62. */
  63. function __construct(& $modx, $key, $options = array()) {
  64. $this->modx =& $modx;
  65. $this->_key = $key;
  66. $this->options = $options;
  67. }
  68. /**
  69. * Reads any undigested messages from subscribed topics.
  70. *
  71. * @param array $options An array of general or protocol specific options.
  72. * @return mixed The resulting message from the register.
  73. */
  74. abstract public function read(array $options = array());
  75. /**
  76. * Send a message to the register.
  77. *
  78. * @abstract Implement this function in derivatives to send a message to a
  79. * specific register (e.g. modFileRegister for file-based registers,
  80. * modStompRegister for ActiveMQ, etc.).
  81. * @param string $topic A topic container in which to broadcast the message.
  82. * @param mixed $message A message, or collection of messages to be sent to
  83. * the register.
  84. * @param array $options An optional array of general or protocol
  85. * specific message properties.
  86. * @return boolean Indicates if the message was recorded.
  87. */
  88. abstract public function send($topic, $message, array $options = array());
  89. /**
  90. * Connect to the register service implementation.
  91. *
  92. * @abstract Implement this only if necessary for the implementation.
  93. * @param array $attributes A collection of attributes required for
  94. * connection to the register.
  95. * @return boolean Indicates if the connection was successful.
  96. */
  97. abstract public function connect(array $attributes = array());
  98. /**
  99. * Close the connection to the register service implementation.
  100. *
  101. * @abstract Implement this only if necessary for the implementation.
  102. * @return boolean Indicates if the connection was closed successfully.
  103. */
  104. abstract public function close();
  105. /**
  106. * Clear all the register messages.
  107. *
  108. * @param string $topic The path representing the topic or message.
  109. * @return boolean Indicates if the clear was successful.
  110. */
  111. abstract public function clear($topic);
  112. /**
  113. * Subscribe to a topic (or specific message) in the register.
  114. *
  115. * @param string $topic The path representing the topic or message.
  116. * @return boolean Indicates if the subscription was successful.
  117. */
  118. public function subscribe($topic) {
  119. $this->subscriptions[] = $topic;
  120. return true;
  121. }
  122. /**
  123. * Unsubscribe from a topic (or specific message) in the register.
  124. *
  125. * @param string $topic The path representing the topic or message.
  126. * @return boolean Indicates if the subscription was removed successfully.
  127. */
  128. public function unsubscribe($topic) {
  129. $success = false;
  130. $topicIdx = array_search($topic, $this->subscriptions);
  131. if ($topicIdx !== false && $topicIdx !== null) {
  132. unset($this->subscriptions[$topicIdx]);
  133. $success = true;
  134. }
  135. return $success;
  136. }
  137. /**
  138. * Acknowledge the registry was read
  139. *
  140. * @param string $messageKey The key of the message being read
  141. * @param string $transactionKey The secure key of the transaction that is reading
  142. * @return void
  143. */
  144. public function acknowledge($messageKey, $transactionKey) {}
  145. /**
  146. * Begin the reading of the message
  147. *
  148. * @param $transactionKey The key of the message
  149. * @return void
  150. */
  151. public function begin($transactionKey) {}
  152. /**
  153. * Commit the transaction and finish
  154. *
  155. * @param string $transactionKey The key of the transaction
  156. * @return void
  157. */
  158. public function commit($transactionKey) {}
  159. /**
  160. * @param $transactionKey
  161. * @return void
  162. */
  163. public function abort($transactionKey) {}
  164. /**
  165. * Set the current topic to be read
  166. *
  167. * @param string $topic The key of the topic
  168. * @return void
  169. */
  170. public function setCurrentTopic($topic) {
  171. if (is_string($topic) && strlen($topic) > 0) {
  172. if ($topic[0] != '/') $topic = $this->_currentTopic . $topic;
  173. if ($topic[strlen($topic) - 1] != '/') $topic .= '/';
  174. $topicIdx = array_search($topic, $this->subscriptions);
  175. if ($topicIdx !== false && $topicIdx !== null) {
  176. $this->_currentTopic = $topic;
  177. }
  178. }
  179. }
  180. /**
  181. * Get the current topic of the register.
  182. *
  183. * @return string The current topic set for the register.
  184. */
  185. public function getCurrentTopic() {
  186. return $this->_currentTopic;
  187. }
  188. /**
  189. * Get the key of this registry
  190. * @return string The key of the current registry
  191. */
  192. public function getKey() {
  193. return $this->_key;
  194. }
  195. }