modx =& $modx; $this->_options = $options; } /** * Get a modRegister instance from the registry. * * If the register does not exist, it is added to the registry. * * @access public * @param string $key A unique name for the register in the registry. Must * be a valid PHP variable string. * @param string $class The actual modRegister derivative which implements * the register functionality. * @param array $options An optional array of register options. * @return modRegister A modRegister instance. */ public function getRegister($key, $class, array $options = array()) { if (isset($this->_registers[$key])) { if ($this->_registers[$key] !== $class) { $this->addRegister($key, $class, $options); } } else { $this->addRegister($key, $class, $options); } return (isset($this->$key) ? $this->$key : null); } /** * Add a modRegister instance to the registry. * * Once a register is added, it is available directly from this registry * instance by the key provided, e.g. $registry->key. * * @access public * @param string $key A unique name for the register in the registry. Must * be a valid PHP variable string. * @param string $class The actual modRegister derivative which implements * the register functionality. * @param array $options An optional array of register options. */ public function addRegister($key, $class, array $options = array()) { if (!in_array($key, $this->_invalidKeys) && substr($key, 0, 1) !== '_' && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key)) { $this->_registers[$key] = $class; $this->$key = $this->_initRegister($key, $class, $options); } } /** * Remove a modRegister instance from the registry. * * @access public * @param string $key The unique name of the register to remove. */ public function removeRegister($key) { if (!in_array($key, $this->_invalidKeys) && substr($key, 0, 1) !== '_' && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key)) { $this->_registers[$key] = null; $this->$key = null; } } /** * Initialize a register within the registry. * * @access protected * @param string $key The key of the registry * @param string $class The class of the modRegister implementation to * initialize. * @param array $options An optional array of register options. * @return modRegister The register instance. */ protected function _initRegister($key, $class, array $options = array()) { $register = null; if ($className = $this->modx->loadClass($class, '', false, true)) { $register = new $className($this->modx, $key, $options); } return $register; } /** * Set the logging level for the topic. * * @access public * @param modRegister &$register * @param string $topic * @param int $level * @param boolean $clear Clear the register before subscribing to it * @return boolean True if successful. */ public function setLogging(modRegister &$register, $topic, $level = modX::LOG_LEVEL_ERROR, $clear = false) { $set = false; $this->_loggingRegister = &$register; if (isset($topic) && !empty($topic)) { $topic = trim($topic); if ($this->_loggingRegister->connect()) { $this->_prevLogTarget = $this->modx->getLogTarget(); $this->_prevLogLevel = $this->modx->getLogLevel(); if ($clear) $this->_loggingRegister->clear($topic); $this->_loggingRegister->subscribe($topic); $this->_loggingRegister->setCurrentTopic($topic); $this->modx->setLogTarget($this->_loggingRegister); $this->modx->setLogLevel($level); $set = true; } } return $set; } /** * Reset the current logging. * * @access public */ public function resetLogging() { if ($this->_loggingRegister && $this->_prevLogTarget && $this->_prevLogLevel) { $this->modx->setLogTarget($this->_prevLogTarget); $this->modx->setLogLevel($this->_prevLogLevel); $this->_loggingRegister = null; } } /** * Check if logging is currently active * * @access public * @return boolean */ public function isLogging() { return $this->_loggingRegister !== null; } }