| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- /*
- * This file is part of MODX Revolution.
- *
- * Copyright (c) MODX, LLC. All Rights Reserved.
- *
- * For complete copyright and license information, see the COPYRIGHT and LICENSE
- * files found in the top-level directory of this distribution.
- */
- /**
- * Represents a container used for producing and consuming messages.
- *
- * @abstract Implement a derivative of this class to provide the behavior for
- * the abstract methods, or override other public or protected methods at your
- * discretion.
- *
- * @package modx
- * @subpackage registry
- */
- abstract class modRegister {
- /**
- * A reference to the modX instance the register is loaded by.
- * @var modX
- * @access public
- */
- public $modx = null;
- /**
- * An array of global options applied to the registry.
- * @var array
- * @access public
- */
- public $options = null;
- /**
- * An array of topics and/or messages the register is subscribed to.
- * @var array
- * @access public
- */
- public $subscriptions = array();
- /**
- * An optional current topic to allow writes to relative paths.
- * @var string
- * @access protected
- */
- protected $_currentTopic = '/';
- /**
- * The key identifying this register in a registry.
- * @var string
- * @access protected
- */
- protected $_key = null;
- /**
- * A polling flag that will terminate additional polling when true.
- * @var boolean
- */
- public $__kill = false;
- /**
- * Construct a new modRegister.
- *
- * @param modX &$modx A reference to a modX instance.
- * @param string $key A valid PHP variable which will be set on the modRegistry instance.
- * @param array $options Optional array of registry options.
- */
- function __construct(& $modx, $key, $options = array()) {
- $this->modx =& $modx;
- $this->_key = $key;
- $this->options = $options;
- }
- /**
- * Reads any undigested messages from subscribed topics.
- *
- * @param array $options An array of general or protocol specific options.
- * @return mixed The resulting message from the register.
- */
- abstract public function read(array $options = array());
- /**
- * Send a message to the register.
- *
- * @abstract Implement this function in derivatives to send a message to a
- * specific register (e.g. modFileRegister for file-based registers,
- * modStompRegister for ActiveMQ, etc.).
- * @param string $topic A topic container in which to broadcast the message.
- * @param mixed $message A message, or collection of messages to be sent to
- * the register.
- * @param array $options An optional array of general or protocol
- * specific message properties.
- * @return boolean Indicates if the message was recorded.
- */
- abstract public function send($topic, $message, array $options = array());
- /**
- * Connect to the register service implementation.
- *
- * @abstract Implement this only if necessary for the implementation.
- * @param array $attributes A collection of attributes required for
- * connection to the register.
- * @return boolean Indicates if the connection was successful.
- */
- abstract public function connect(array $attributes = array());
- /**
- * Close the connection to the register service implementation.
- *
- * @abstract Implement this only if necessary for the implementation.
- * @return boolean Indicates if the connection was closed successfully.
- */
- abstract public function close();
- /**
- * Clear all the register messages.
- *
- * @param string $topic The path representing the topic or message.
- * @return boolean Indicates if the clear was successful.
- */
- abstract public function clear($topic);
- /**
- * Subscribe to a topic (or specific message) in the register.
- *
- * @param string $topic The path representing the topic or message.
- * @return boolean Indicates if the subscription was successful.
- */
- public function subscribe($topic) {
- $this->subscriptions[] = $topic;
- return true;
- }
- /**
- * Unsubscribe from a topic (or specific message) in the register.
- *
- * @param string $topic The path representing the topic or message.
- * @return boolean Indicates if the subscription was removed successfully.
- */
- public function unsubscribe($topic) {
- $success = false;
- $topicIdx = array_search($topic, $this->subscriptions);
- if ($topicIdx !== false && $topicIdx !== null) {
- unset($this->subscriptions[$topicIdx]);
- $success = true;
- }
- return $success;
- }
- /**
- * Acknowledge the registry was read
- *
- * @param string $messageKey The key of the message being read
- * @param string $transactionKey The secure key of the transaction that is reading
- * @return void
- */
- public function acknowledge($messageKey, $transactionKey) {}
- /**
- * Begin the reading of the message
- *
- * @param $transactionKey The key of the message
- * @return void
- */
- public function begin($transactionKey) {}
- /**
- * Commit the transaction and finish
- *
- * @param string $transactionKey The key of the transaction
- * @return void
- */
- public function commit($transactionKey) {}
- /**
- * @param $transactionKey
- * @return void
- */
- public function abort($transactionKey) {}
- /**
- * Set the current topic to be read
- *
- * @param string $topic The key of the topic
- * @return void
- */
- public function setCurrentTopic($topic) {
- if (is_string($topic) && strlen($topic) > 0) {
- if ($topic[0] != '/') $topic = $this->_currentTopic . $topic;
- if ($topic[strlen($topic) - 1] != '/') $topic .= '/';
- $topicIdx = array_search($topic, $this->subscriptions);
- if ($topicIdx !== false && $topicIdx !== null) {
- $this->_currentTopic = $topic;
- }
- }
- }
- /**
- * Get the current topic of the register.
- *
- * @return string The current topic set for the register.
- */
- public function getCurrentTopic() {
- return $this->_currentTopic;
- }
- /**
- * Get the key of this registry
- * @return string The key of the current registry
- */
- public function getKey() {
- return $this->_key;
- }
- }
|