modregistry.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 collection of message registers.
  12. *
  13. * A register can consist of loggable audit events, error events, debug events,
  14. * and any other messages that may be sent to a message queue and later
  15. * retrieved or redirected to some other output source. Some key features will
  16. * include:
  17. *
  18. * -Logging of registry transactions to file or DB
  19. * -Tracking progress of asynchonous processes
  20. * -Can serve as a generic message queue, where MODX elements can register new
  21. * messages or grab the latest messages via scheduled or ad hoc requests.
  22. *
  23. * @todo Encapsulate all debugging, error handling, error reporting, and audit
  24. * logging features into appropriate registers.
  25. *
  26. * @package modx
  27. * @subpackage registry
  28. */
  29. class modRegistry {
  30. /**
  31. * A reference to the modX instance the registry is loaded by.
  32. * @var modX
  33. * @access public
  34. */
  35. public $modx = null;
  36. /**
  37. * An array of global options applied to the registry.
  38. * @var array
  39. * @access protected
  40. */
  41. public $_options = array();
  42. /**
  43. * An array of register keys that are reserved from use.
  44. * @var array
  45. * @access protected
  46. */
  47. protected $_invalidKeys = array(
  48. 'modx',
  49. );
  50. /**
  51. * An array of MODX registers managed by the registry.
  52. * @var array
  53. * @access private
  54. */
  55. protected $_registers = array();
  56. /**
  57. * @var modRegister The current logging registry
  58. */
  59. protected $_loggingRegister = null;
  60. /**
  61. * @var string The previous logTarget for xPDO, to be reset when finished
  62. */
  63. protected $_prevLogTarget = null;
  64. /**
  65. * @var integer The previous log level for xPDO, to be reset when finished
  66. */
  67. protected $_prevLogLevel = null;
  68. /**
  69. * Construct a new registry instance.
  70. *
  71. * @param modX &$modx A reference to a modX instance.
  72. * @param array $options Optional array of registry options.
  73. */
  74. function __construct(modX &$modx, array $options = array()) {
  75. $this->modx =& $modx;
  76. $this->_options = $options;
  77. }
  78. /**
  79. * Get a modRegister instance from the registry.
  80. *
  81. * If the register does not exist, it is added to the registry.
  82. *
  83. * @access public
  84. * @param string $key A unique name for the register in the registry. Must
  85. * be a valid PHP variable string.
  86. * @param string $class The actual modRegister derivative which implements
  87. * the register functionality.
  88. * @param array $options An optional array of register options.
  89. * @return modRegister A modRegister instance.
  90. */
  91. public function getRegister($key, $class, array $options = array()) {
  92. if (isset($this->_registers[$key])) {
  93. if ($this->_registers[$key] !== $class) {
  94. $this->addRegister($key, $class, $options);
  95. }
  96. } else {
  97. $this->addRegister($key, $class, $options);
  98. }
  99. return (isset($this->$key) ? $this->$key : null);
  100. }
  101. /**
  102. * Add a modRegister instance to the registry.
  103. *
  104. * Once a register is added, it is available directly from this registry
  105. * instance by the key provided, e.g. $registry->key.
  106. *
  107. * @access public
  108. * @param string $key A unique name for the register in the registry. Must
  109. * be a valid PHP variable string.
  110. * @param string $class The actual modRegister derivative which implements
  111. * the register functionality.
  112. * @param array $options An optional array of register options.
  113. */
  114. public function addRegister($key, $class, array $options = array()) {
  115. if (!in_array($key, $this->_invalidKeys) && substr($key, 0, 1) !== '_' && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key)) {
  116. $this->_registers[$key] = $class;
  117. $this->$key = $this->_initRegister($key, $class, $options);
  118. }
  119. }
  120. /**
  121. * Remove a modRegister instance from the registry.
  122. *
  123. * @access public
  124. * @param string $key The unique name of the register to remove.
  125. */
  126. public function removeRegister($key) {
  127. if (!in_array($key, $this->_invalidKeys) && substr($key, 0, 1) !== '_' && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key)) {
  128. $this->_registers[$key] = null;
  129. $this->$key = null;
  130. }
  131. }
  132. /**
  133. * Initialize a register within the registry.
  134. *
  135. * @access protected
  136. * @param string $key The key of the registry
  137. * @param string $class The class of the modRegister implementation to
  138. * initialize.
  139. * @param array $options An optional array of register options.
  140. * @return modRegister The register instance.
  141. */
  142. protected function _initRegister($key, $class, array $options = array()) {
  143. $register = null;
  144. if ($className = $this->modx->loadClass($class, '', false, true)) {
  145. $register = new $className($this->modx, $key, $options);
  146. }
  147. return $register;
  148. }
  149. /**
  150. * Set the logging level for the topic.
  151. *
  152. * @access public
  153. * @param modRegister &$register
  154. * @param string $topic
  155. * @param int $level
  156. * @param boolean $clear Clear the register before subscribing to it
  157. * @return boolean True if successful.
  158. */
  159. public function setLogging(modRegister &$register, $topic, $level = modX::LOG_LEVEL_ERROR, $clear = false) {
  160. $set = false;
  161. $this->_loggingRegister = &$register;
  162. if (isset($topic) && !empty($topic)) {
  163. $topic = trim($topic);
  164. if ($this->_loggingRegister->connect()) {
  165. $this->_prevLogTarget = $this->modx->getLogTarget();
  166. $this->_prevLogLevel = $this->modx->getLogLevel();
  167. if ($clear) $this->_loggingRegister->clear($topic);
  168. $this->_loggingRegister->subscribe($topic);
  169. $this->_loggingRegister->setCurrentTopic($topic);
  170. $this->modx->setLogTarget($this->_loggingRegister);
  171. $this->modx->setLogLevel($level);
  172. $set = true;
  173. }
  174. }
  175. return $set;
  176. }
  177. /**
  178. * Reset the current logging.
  179. *
  180. * @access public
  181. */
  182. public function resetLogging() {
  183. if ($this->_loggingRegister && $this->_prevLogTarget && $this->_prevLogLevel) {
  184. $this->modx->setLogTarget($this->_prevLogTarget);
  185. $this->modx->setLogLevel($this->_prevLogLevel);
  186. $this->_loggingRegister = null;
  187. }
  188. }
  189. /**
  190. * Check if logging is currently active
  191. *
  192. * @access public
  193. * @return boolean
  194. */
  195. public function isLogging() {
  196. return $this->_loggingRegister !== null;
  197. }
  198. }