00001 <?php
00030 class Zend_Queue implements Countable
00031 {
00035 const TIMEOUT = 'timeout';
00036
00040 const VISIBILITY_TIMEOUT = 30;
00041
00045 const NAME = 'name';
00046
00050 protected $_adapter = null;
00051
00057 protected $_options = array();
00058
00064 protected $_messageClass = 'Zend_Queue_Message';
00065
00071 protected $_messageSetClass = 'Zend_Queue_Message_Iterator';
00072
00076 protected $_logger = null;
00077
00092 public function __construct($spec, $options = array())
00093 {
00094 $adapter = null;
00095 if ($spec instanceof Zend_Queue_Adapter_AdapterInterface) {
00096 $adapter = $spec;
00097 } elseif (is_string($spec)) {
00098 $adapter = $spec;
00099 } elseif ($spec instanceof Zend_Config) {
00100 $options = $spec->toArray();
00101 } elseif (is_array($spec)) {
00102 $options = $spec;
00103 }
00104
00105
00106 if ((null === $adapter)
00107 && (!is_array($options) && (!$options instanceof Zend_Config))
00108 ) {
00109 require_once 'Zend/Queue/Exception.php';
00110 throw new Zend_Queue_Exception('No valid params passed to constructor');
00111 }
00112
00113
00114 if ($options instanceof Zend_Config) {
00115 $options = $options->toArray();
00116 } elseif (!is_array($options)) {
00117 $options = array();
00118 }
00119
00120
00121 if (!isset($options[self::TIMEOUT])) {
00122 $options[self::TIMEOUT] = self::VISIBILITY_TIMEOUT;
00123 }
00124
00125
00126 if (!array_key_exists('timeout', $options)) {
00127 $options[self::TIMEOUT] = self::VISIBILITY_TIMEOUT;
00128 }
00129 if (array_key_exists('messageClass', $options)) {
00130 $this->setMessageClass($options['messageClass']);
00131 }
00132 if (array_key_exists('messageSetClass', $options)) {
00133 $this->setMessageSetClass($options['messageSetClass']);
00134 }
00135
00136 $this->setOptions($options);
00137
00138
00139 if (null !== $adapter) {
00140 $this->setAdapter($adapter);
00141 }
00142 }
00143
00150 public function setOptions(array $options)
00151 {
00152 $this->_options = array_merge($this->_options, $options);
00153 return $this;
00154 }
00155
00163 public function setOption($name, $value)
00164 {
00165 $this->_options[(string) $name] = $value;
00166 return $this;
00167 }
00168
00174 public function getOptions()
00175 {
00176 return $this->_options;
00177 }
00178
00185 public function hasOption($name)
00186 {
00187 return array_key_exists($name, $this->_options);
00188 }
00189
00196 public function getOption($name)
00197 {
00198 if ($this->hasOption($name)) {
00199 return $this->_options[$name];
00200 }
00201 return null;
00202 }
00203
00210 public function setAdapter($adapter)
00211 {
00212 if (is_string($adapter)) {
00213 if (null === ($adapterNamespace = $this->getOption('adapterNamespace'))) {
00214 $adapterNamespace = 'Zend_Queue_Adapter';
00215 }
00216
00217 $adapterName = str_replace(
00218 ' ',
00219 '_',
00220 ucwords(
00221 str_replace(
00222 '_',
00223 ' ',
00224 strtolower($adapterNamespace . '_' . $adapter)
00225 )
00226 )
00227 );
00228
00229 if (!class_exists($adapterName)) {
00230 require_once 'Zend/Loader.php';
00231 Zend_Loader::loadClass($adapterName);
00232 }
00233
00234
00235
00236
00237
00238 $adapter = new $adapterName($this->getOptions(), $this);
00239 }
00240
00241 if (!$adapter instanceof Zend_Queue_Adapter_AdapterInterface) {
00242 require_once 'Zend/Queue/Exception.php';
00243 throw new Zend_Queue_Exception("Adapter class '" . get_class($adapterName) . "' does not implement Zend_Queue_Adapter_AdapterInterface");
00244 }
00245
00246 $this->_adapter = $adapter;
00247
00248 $this->_adapter->setQueue($this);
00249
00250 if (null !== ($name = $this->getOption(self::NAME))) {
00251 $this->_setName($name);
00252 }
00253
00254 return $this;
00255 }
00256
00262 public function getAdapter()
00263 {
00264 return $this->_adapter;
00265 }
00266
00271 public function setMessageClass($className)
00272 {
00273 $this->_messageClass = (string) $className;
00274 return $this;
00275 }
00276
00280 public function getMessageClass()
00281 {
00282 return $this->_messageClass;
00283 }
00284
00289 public function setMessageSetClass($className)
00290 {
00291 $this->_messageSetClass = (string) $className;
00292 return $this;
00293 }
00294
00298 public function getMessageSetClass()
00299 {
00300 return $this->_messageSetClass;
00301 }
00302
00311 public function getName()
00312 {
00313 return $this->getOption(self::NAME);
00314 }
00315
00324 public function createQueue($name, $timeout = null)
00325 {
00326 if (!is_string($name)) {
00327 require_once 'Zend/Queue/Exception.php';
00328 throw new Zend_Queue_Exception('$name is not a string');
00329 }
00330
00331 if ((null !== $timeout) && !is_integer($timeout)) {
00332 require_once 'Zend/Queue/Exception.php';
00333 throw new Zend_Queue_Exception('$timeout must be an integer');
00334 }
00335
00336
00337 if (null === $timeout) {
00338 $timeout = $this->getOption(self::TIMEOUT);
00339 }
00340
00341
00342
00343 if ($this->isSupported('create')) {
00344 if ($this->getAdapter()->isExists($name)) {
00345 return false;
00346 }
00347
00348 if (!$this->getAdapter()->create($name, $timeout)) {
00349 return false;
00350 }
00351 }
00352
00353 $options = array(
00354 self::NAME => $name,
00355 'timeout' => $timeout
00356 );
00357
00358 return new self($this->getAdapter(), $options);
00359 }
00360
00369 public function deleteQueue()
00370 {
00371 if ($this->isSupported('delete')) {
00372 $deleted = $this->getAdapter()->delete($this->getName());
00373 }
00374 else {
00375 $deleted = true;
00376 }
00377
00381 require_once('Zend/Queue/Adapter/Null.php');
00382 $this->setAdapter(new Zend_Queue_Adapter_Null($this->getOptions()));
00383
00384 return $deleted;
00385 }
00386
00399 public function deleteMessage(Zend_Queue_Message $message)
00400 {
00401 if ($this->getAdapter()->isSupported('deleteMessage')) {
00402 return $this->getAdapter()->deleteMessage($message);
00403 }
00404 return true;
00405 }
00406
00414 public function send($message)
00415 {
00416 return $this->getAdapter()->send($message);
00417 }
00418
00424 public function count()
00425 {
00426 if ($this->getAdapter()->isSupported('count')) {
00427 return $this->getAdapter()->count();
00428 }
00429 return 0;
00430 }
00431
00439 public function receive($maxMessages=null, $timeout=null)
00440 {
00441 if (($maxMessages !== null) && !is_integer($maxMessages)) {
00442 require_once 'Zend/Queue/Exception.php';
00443 throw new Zend_Queue_Exception('$maxMessages must be an integer or null');
00444 }
00445
00446 if (($timeout !== null) && !is_integer($timeout)) {
00447 require_once 'Zend/Queue/Exception.php';
00448 throw new Zend_Queue_Exception('$timeout must be an integer or null');
00449 }
00450
00451
00452 if ($maxMessages === null) {
00453 $maxMessages = 1;
00454 }
00455
00456
00457 if ($timeout === null) {
00458 $timeout = $this->getOption(self::TIMEOUT);
00459 }
00460
00461 return $this->getAdapter()->receive($maxMessages, $timeout);
00462 }
00463
00473 public function getCapabilities()
00474 {
00475 return $this->getAdapter()->getCapabilities();
00476 }
00477
00484 public function isSupported($name)
00485 {
00486 $translation = array(
00487 'deleteQueue' => 'delete',
00488 'createQueue' => 'create'
00489 );
00490
00491 if (isset($translation[$name])) {
00492 $name = $translation[$name];
00493 }
00494
00495 return $this->getAdapter()->isSupported($name);
00496 }
00497
00504 public function getQueues()
00505 {
00506 if (!$this->isSupported('getQueues')) {
00507 throw new Zend_Queue_Exception( __FUNCTION__ . '() is not supported by ' . get_class($this->getAdapter()));
00508 }
00509
00510 return $this->getAdapter()->getQueues();
00511 }
00512
00521 protected function _setName($name)
00522 {
00523 if (!is_string($name)) {
00527 require_once 'Zend/Queue/Exception.php';
00528 throw new Zend_Queue_Exception("$name is not a string");
00529 }
00530
00531 if ($this->getAdapter()->isSupported('create')) {
00532 if (!$this->getAdapter()->isExists($name)) {
00533 $timeout = $this->getOption(self::TIMEOUT);
00534
00535 if (!$this->getAdapter()->create($name, $timeout)) {
00536
00537 return false;
00538 }
00539 }
00540 }
00541
00542 $this->setOption(self::NAME, $name);
00543
00544 return $this;
00545 }
00546
00553 public function debugInfo()
00554 {
00555 $info = array();
00556 $info['self'] = get_class($this);
00557 $info['adapter'] = get_class($this->getAdapter());
00558 foreach ($this->getAdapter()->getCapabilities() as $feature => $supported) {
00559 $info['adapter-' . $feature] = ($supported) ? 'yes' : 'no';
00560 }
00561 $info['options'] = $this->getOptions();
00562 $info['options']['driverOptions'] = '[hidden]';
00563 $info['currentQueue'] = $this->getName();
00564 $info['messageClass'] = $this->getMessageClass();
00565 $info['messageSetClass'] = $this->getMessageSetClass();
00566
00567 return $info;
00568 }
00569 }