PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/modx/registry/modregister.class.php

https://gitlab.com/haque.mdmanzurul/modx-improve-carolyn
PHP | 223 lines | 52 code | 16 blank | 155 comment | 10 complexity | 45b1d638a97f9b9fa72c3816fe3fa1a9 MD5 | raw file
  1. <?php
  2. /*
  3. * MODX Revolution
  4. *
  5. * Copyright 2006-2014 by MODX, LLC.
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. * Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /**
  23. * Represents a container used for producing and consuming messages.
  24. *
  25. * @abstract Implement a derivative of this class to provide the behavior for
  26. * the abstract methods, or override other public or protected methods at your
  27. * discretion.
  28. *
  29. * @package modx
  30. * @subpackage registry
  31. */
  32. abstract class modRegister {
  33. /**
  34. * A reference to the modX instance the register is loaded by.
  35. * @var modX
  36. * @access public
  37. */
  38. public $modx = null;
  39. /**
  40. * An array of global options applied to the registry.
  41. * @var array
  42. * @access public
  43. */
  44. public $options = null;
  45. /**
  46. * An array of topics and/or messages the register is subscribed to.
  47. * @var array
  48. * @access public
  49. */
  50. public $subscriptions = array();
  51. /**
  52. * An optional current topic to allow writes to relative paths.
  53. * @var string
  54. * @access protected
  55. */
  56. protected $_currentTopic = '/';
  57. /**
  58. * The key identifying this register in a registry.
  59. * @var string
  60. * @access protected
  61. */
  62. protected $_key = null;
  63. /**
  64. * A polling flag that will terminate additional polling when true.
  65. * @var boolean
  66. */
  67. public $__kill = false;
  68. /**
  69. * Construct a new modRegister.
  70. *
  71. * @param modX &$modx A reference to a modX instance.
  72. * @param string $key A valid PHP variable which will be set on the modRegistry instance.
  73. * @param array $options Optional array of registry options.
  74. */
  75. function __construct(& $modx, $key, $options = array()) {
  76. $this->modx =& $modx;
  77. $this->_key = $key;
  78. $this->options = $options;
  79. }
  80. /**
  81. * Reads any undigested messages from subscribed topics.
  82. *
  83. * @param array $options An array of general or protocol specific options.
  84. * @return mixed The resulting message from the register.
  85. */
  86. abstract public function read(array $options = array());
  87. /**
  88. * Send a message to the register.
  89. *
  90. * @abstract Implement this function in derivatives to send a message to a
  91. * specific register (e.g. modFileRegister for file-based registers,
  92. * modStompRegister for ActiveMQ, etc.).
  93. * @param string $topic A topic container in which to broadcast the message.
  94. * @param mixed $message A message, or collection of messages to be sent to
  95. * the register.
  96. * @param array $options An optional array of general or protocol
  97. * specific message properties.
  98. * @return boolean Indicates if the message was recorded.
  99. */
  100. abstract public function send($topic, $message, array $options = array());
  101. /**
  102. * Connect to the register service implementation.
  103. *
  104. * @abstract Implement this only if necessary for the implementation.
  105. * @param array $attributes A collection of attributes required for
  106. * connection to the register.
  107. * @return boolean Indicates if the connection was successful.
  108. */
  109. abstract public function connect(array $attributes = array());
  110. /**
  111. * Close the connection to the register service implementation.
  112. *
  113. * @abstract Implement this only if necessary for the implementation.
  114. * @return boolean Indicates if the connection was closed successfully.
  115. */
  116. abstract public function close();
  117. /**
  118. * Clear all the register messages.
  119. *
  120. * @param string $topic The path representing the topic or message.
  121. * @return boolean Indicates if the clear was successful.
  122. */
  123. abstract public function clear($topic);
  124. /**
  125. * Subscribe to a topic (or specific message) in the register.
  126. *
  127. * @param string $topic The path representing the topic or message.
  128. * @return boolean Indicates if the subscription was successful.
  129. */
  130. public function subscribe($topic) {
  131. $this->subscriptions[] = $topic;
  132. return true;
  133. }
  134. /**
  135. * Unsubscribe from a topic (or specific message) in the register.
  136. *
  137. * @param string $topic The path representing the topic or message.
  138. * @return boolean Indicates if the subscription was removed successfully.
  139. */
  140. public function unsubscribe($topic) {
  141. $success = false;
  142. $topicIdx = array_search($topic, $this->subscriptions);
  143. if ($topicIdx !== false && $topicIdx !== null) {
  144. unset($this->subscriptions[$topicIdx]);
  145. $success = true;
  146. }
  147. return $success;
  148. }
  149. /**
  150. * Acknowledge the registry was read
  151. *
  152. * @param string $messageKey The key of the message being read
  153. * @param string $transactionKey The secure key of the transaction that is reading
  154. * @return void
  155. */
  156. public function acknowledge($messageKey, $transactionKey) {}
  157. /**
  158. * Begin the reading of the message
  159. *
  160. * @param $transactionKey The key of the message
  161. * @return void
  162. */
  163. public function begin($transactionKey) {}
  164. /**
  165. * Commit the transaction and finish
  166. *
  167. * @param string $transactionKey The key of the transaction
  168. * @return void
  169. */
  170. public function commit($transactionKey) {}
  171. /**
  172. * @param $transactionKey
  173. * @return void
  174. */
  175. public function abort($transactionKey) {}
  176. /**
  177. * Set the current topic to be read
  178. *
  179. * @param string $topic The key of the topic
  180. * @return void
  181. */
  182. public function setCurrentTopic($topic) {
  183. if (is_string($topic) && strlen($topic) > 0) {
  184. if ($topic[0] != '/') $topic = $this->_currentTopic . $topic;
  185. if ($topic[strlen($topic) - 1] != '/') $topic .= '/';
  186. $topicIdx = array_search($topic, $this->subscriptions);
  187. if ($topicIdx !== false && $topicIdx !== null) {
  188. $this->_currentTopic = $topic;
  189. }
  190. }
  191. }
  192. /**
  193. * Get the current topic of the register.
  194. *
  195. * @return string The current topic set for the register.
  196. */
  197. public function getCurrentTopic() {
  198. return $this->_currentTopic;
  199. }
  200. /**
  201. * Get the key of this registry
  202. * @return string The key of the current registry
  203. */
  204. public function getKey() {
  205. return $this->_key;
  206. }
  207. }