toArray(); } elseif (is_array($spec)) { $options = $spec; } if ((null === $adapter) && (!is_array($options) && (!$options instanceof Zend_Config)) ) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception('No valid params passed to constructor'); } if ($options instanceof Zend_Config) { $options = $options->toArray(); } elseif (!is_array($options)) { $options = array(); } if (!isset($options[self::TIMEOUT])) { $options[self::TIMEOUT] = self::VISIBILITY_TIMEOUT; } if (!array_key_exists('timeout', $options)) { $options[self::TIMEOUT] = self::VISIBILITY_TIMEOUT; } if (array_key_exists('messageClass', $options)) { $this->setMessageClass($options['messageClass']); } if (array_key_exists('messageSetClass', $options)) { $this->setMessageSetClass($options['messageSetClass']); } $this->setOptions($options); if (null !== $adapter) { $this->setAdapter($adapter); } } public function setOptions(array $options) { $this->_options = array_merge($this->_options, $options); return $this; } public function setOption($name, $value) { $this->_options[(string) $name] = $value; return $this; } public function getOptions() { return $this->_options; } public function hasOption($name) { return array_key_exists($name, $this->_options); } public function getOption($name) { if ($this->hasOption($name)) { return $this->_options[$name]; } return null; } public function setAdapter($adapter) { if (is_string($adapter)) { if (null === ($adapterNamespace = $this->getOption('adapterNamespace'))) { $adapterNamespace = 'Zend_Queue_Adapter'; } $adapterName = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', strtolower($adapterNamespace . '_' . $adapter) ) ) ); if (!class_exists($adapterName)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($adapterName); } $adapter = new $adapterName($this->getOptions(), $this); } if (!$adapter instanceof Zend_Queue_Adapter_AdapterInterface) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception("Adapter class '" . get_class($adapterName) . "' does not implement Zend_Queue_Adapter_AdapterInterface"); } $this->_adapter = $adapter; $this->_adapter->setQueue($this); if (null !== ($name = $this->getOption(self::NAME))) { $this->_setName($name); } return $this; } public function getAdapter() { return $this->_adapter; } public function setMessageClass($className) { $this->_messageClass = (string) $className; return $this; } public function getMessageClass() { return $this->_messageClass; } public function setMessageSetClass($className) { $this->_messageSetClass = (string) $className; return $this; } public function getMessageSetClass() { return $this->_messageSetClass; } public function getName() { return $this->getOption(self::NAME); } public function createQueue($name, $timeout = null) { if (!is_string($name)) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception('$name is not a string'); } if ((null !== $timeout) && !is_integer($timeout)) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception('$timeout must be an integer'); } if (null === $timeout) { $timeout = $this->getOption(self::TIMEOUT); } if ($this->isSupported('create')) { if ($this->getAdapter()->isExists($name)) { return false; } if (!$this->getAdapter()->create($name, $timeout)) { return false; } } $options = array( self::NAME => $name, 'timeout' => $timeout ); return new self($this->getAdapter(), $options); } public function deleteQueue() { if ($this->isSupported('delete')) { $deleted = $this->getAdapter()->delete($this->getName()); } else { $deleted = true; } require_once('Zend/Queue/Adapter/Null.php'); $this->setAdapter(new Zend_Queue_Adapter_Null($this->getOptions())); return $deleted; } public function deleteMessage(Zend_Queue_Message $message) { if ($this->getAdapter()->isSupported('deleteMessage')) { return $this->getAdapter()->deleteMessage($message); } return true; } public function send($message) { return $this->getAdapter()->send($message); } public function count() { if ($this->getAdapter()->isSupported('count')) { return $this->getAdapter()->count(); } return 0; } public function receive($maxMessages=null, $timeout=null) { if (($maxMessages !== null) && !is_integer($maxMessages)) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception('$maxMessages must be an integer or null'); } if (($timeout !== null) && !is_integer($timeout)) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception('$timeout must be an integer or null'); } if ($maxMessages === null) { $maxMessages = 1; } if ($timeout === null) { $timeout = $this->getOption(self::TIMEOUT); } return $this->getAdapter()->receive($maxMessages, $timeout); } public function getCapabilities() { return $this->getAdapter()->getCapabilities(); } public function isSupported($name) { $translation = array( 'deleteQueue' => 'delete', 'createQueue' => 'create' ); if (isset($translation[$name])) { $name = $translation[$name]; } return $this->getAdapter()->isSupported($name); } public function getQueues() { if (!$this->isSupported('getQueues')) { throw new Zend_Queue_Exception( __FUNCTION__ . '() is not supported by ' . get_class($this->getAdapter())); } return $this->getAdapter()->getQueues(); } protected function _setName($name) { if (!is_string($name)) { require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception("$name is not a string"); } if ($this->getAdapter()->isSupported('create')) { if (!$this->getAdapter()->isExists($name)) { $timeout = $this->getOption(self::TIMEOUT); if (!$this->getAdapter()->create($name, $timeout)) { return false; } } } $this->setOption(self::NAME, $name); return $this; } public function debugInfo() { $info = array(); $info['self'] = get_class($this); $info['adapter'] = get_class($this->getAdapter()); foreach ($this->getAdapter()->getCapabilities() as $feature => $supported) { $info['adapter-' . $feature] = ($supported) ? 'yes' : 'no'; } $info['options'] = $this->getOptions(); $info['options']['driverOptions'] = '[hidden]'; $info['currentQueue'] = $this->getName(); $info['messageClass'] = $this->getMessageClass(); $info['messageSetClass'] = $this->getMessageSetClass(); return $info; } }