File indexing completed on 2024-06-16 05:30:19

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Queue
0017  * @subpackage Adapter
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Queue
0025  */
0026 // require_once 'Zend/Queue.php';
0027 
0028 /**
0029  * @see Zend_Queue_Adapter_AdapterInterface
0030  */
0031 // require_once 'Zend/Queue/Adapter/AdapterInterface.php';
0032 
0033 /**
0034  * Class for connecting to queues performing common operations.
0035  *
0036  * @category   Zend
0037  * @package    Zend_Queue
0038  * @subpackage Adapter
0039  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0040  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0041  */
0042 abstract class Zend_Queue_Adapter_AdapterAbstract
0043     implements Zend_Queue_Adapter_AdapterInterface
0044 {
0045     /**
0046      * Default timeout for createQueue() function
0047      */
0048     const CREATE_TIMEOUT_DEFAULT = 30;
0049 
0050     /**
0051      * Default timeout for recieve() function
0052      */
0053     const RECEIVE_TIMEOUT_DEFAULT = 30;
0054 
0055     /**
0056      * User-provided options
0057      *
0058      * @var array
0059      */
0060     protected $_options = array();
0061 
0062     /**
0063      * Internal array of queues to save on lookups
0064      *
0065      * @var array
0066      */
0067     protected $_queues = array();
0068 
0069     /**
0070      * Contains the Zend_Queue that this object
0071      *
0072      * @var Zend_Queue_Adapter_Abstract
0073      */
0074     protected $_queue = null;
0075 
0076     /**
0077      * Constructor.
0078      *
0079      * $options is an array of key/value pairs or an instance of Zend_Config
0080      * containing configuration options.  These options are common to most adapters:
0081      *
0082      * See the Zend_Queue Adapter Notes documentation for example configurations.
0083      *
0084      * Some options are used on a case-by-case basis by adapters:
0085      *
0086      * access_key     => (string) Amazon AWS Access Key
0087      * secret_key     => (string) Amazon AWS Secret Key
0088      * dbname         => (string) The name of the database to user
0089      * username       => (string) Connect to the database as this username.
0090      * password       => (string) Password associated with the username.
0091      * host           => (string) What host to connect to, defaults to localhost
0092      * port           => (string) The port of the database
0093      *
0094      * @param  array|Zend_Config $config An array having configuration data
0095      * @param  Zend_Queue The Zend_Queue object that created this class
0096      * @return void
0097      * @throws Zend_Queue_Exception
0098      */
0099     public function __construct($options, Zend_Queue $queue = null)
0100     {
0101         if ($options instanceof Zend_Config) {
0102             $options = $options->toArray();
0103         }
0104 
0105         /*
0106          * Verify that adapter parameters are in an array.
0107          */
0108         if (!is_array($options)) {
0109             // require_once 'Zend/Queue/Exception.php';
0110             throw new Zend_Queue_Exception('Adapter options must be an array or Zend_Config object');
0111         }
0112 
0113         // set the queue
0114         if ($queue !== null) {
0115             $this->setQueue($queue);
0116         }
0117 
0118         $adapterOptions = array();
0119         $driverOptions  = array();
0120 
0121         // Normalize the options and merge with the defaults
0122         if (array_key_exists('options', $options)) {
0123             if (!is_array($options['options'])) {
0124                 // require_once 'Zend/Queue/Exception.php';
0125                 throw new Zend_Queue_Exception("Configuration array 'options' must be an array");
0126             }
0127 
0128             // Can't use array_merge() because keys might be integers
0129             foreach ($options['options'] as $key => $value) {
0130                 $adapterOptions[$key] = $value;
0131             }
0132         }
0133         if (array_key_exists('driverOptions', $options)) {
0134             // can't use array_merge() because keys might be integers
0135             foreach ((array)$options['driverOptions'] as $key => $value) {
0136                 $driverOptions[$key] = $value;
0137             }
0138         }
0139         $this->_options = array_merge($this->_options, $options);
0140         $this->_options['options']       = $adapterOptions;
0141         $this->_options['driverOptions'] = $driverOptions;
0142     }
0143 
0144     /********************************************************************
0145     * Queue management functions
0146      *********************************************************************/
0147     /**
0148      * get the Zend_Queue class that is attached to this object
0149      *
0150      * @return Zend_Queue|null
0151      */
0152     public function getQueue()
0153     {
0154         return $this->_queue;
0155     }
0156 
0157     /**
0158      * set the Zend_Queue class for this object
0159      *
0160      * @param  Zend_Queue $queue
0161      * @return Zend_Queue_Adapter_AdapterInterface
0162      */
0163     public function setQueue(Zend_Queue $queue)
0164     {
0165         $this->_queue = $queue;
0166         return $this;
0167     }
0168 
0169     /**
0170      * Returns the configuration options in this adapter.
0171      *
0172      * @return array
0173      */
0174     public function getOptions()
0175     {
0176         return $this->_options;
0177     }
0178 
0179     /**
0180      * Indicates if a function is supported or not.
0181      *
0182      * @param  string $name
0183      * @return boolean
0184      */
0185     public function isSupported($name)
0186     {
0187         $list = $this->getCapabilities();
0188 
0189         return (isset($list[$name]) && $list[$name]);
0190      }
0191 }