web/lib/Zend/Queue/Adapter/AdapterAbstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Queue
       
    17  * @subpackage Adapter
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: AdapterAbstract.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Queue
       
    25  */
       
    26 require_once 'Zend/Queue.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Queue_Adapter_AdapterInterface
       
    30  */
       
    31 require_once 'Zend/Queue/Adapter/AdapterInterface.php';
       
    32 
       
    33 /**
       
    34  * Class for connecting to queues performing common operations.
       
    35  *
       
    36  * @category   Zend
       
    37  * @package    Zend_Queue
       
    38  * @subpackage Adapter
       
    39  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    40  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    41  */
       
    42 abstract class Zend_Queue_Adapter_AdapterAbstract
       
    43     implements Zend_Queue_Adapter_AdapterInterface
       
    44 {
       
    45     /**
       
    46      * Default timeout for createQueue() function
       
    47      */
       
    48     const CREATE_TIMEOUT_DEFAULT = 30;
       
    49 
       
    50     /**
       
    51      * Default timeout for recieve() function
       
    52      */
       
    53     const RECEIVE_TIMEOUT_DEFAULT = 30;
       
    54 
       
    55     /**
       
    56      * User-provided options
       
    57      *
       
    58      * @var array
       
    59      */
       
    60     protected $_options = array();
       
    61 
       
    62     /**
       
    63      * Internal array of queues to save on lookups
       
    64      *
       
    65      * @var array
       
    66      */
       
    67     protected $_queues = array();
       
    68 
       
    69     /**
       
    70      * Contains the Zend_Queue that this object
       
    71      *
       
    72      * @var Zend_Queue_Adapter_Abstract
       
    73      */
       
    74     protected $_queue = null;
       
    75 
       
    76     /**
       
    77      * Constructor.
       
    78      *
       
    79      * $options is an array of key/value pairs or an instance of Zend_Config
       
    80      * containing configuration options.  These options are common to most adapters:
       
    81      *
       
    82      * See the Zend_Queue Adapter Notes documentation for example configurations.
       
    83      *
       
    84      * Some options are used on a case-by-case basis by adapters:
       
    85      *
       
    86      * access_key     => (string) Amazon AWS Access Key
       
    87      * secret_key     => (string) Amazon AWS Secret Key
       
    88      * dbname         => (string) The name of the database to user
       
    89      * username       => (string) Connect to the database as this username.
       
    90      * password       => (string) Password associated with the username.
       
    91      * host           => (string) What host to connect to, defaults to localhost
       
    92      * port           => (string) The port of the database
       
    93      *
       
    94      * @param  array|Zend_Config $config An array having configuration data
       
    95      * @param  Zend_Queue The Zend_Queue object that created this class
       
    96      * @return void
       
    97      * @throws Zend_Queue_Exception
       
    98      */
       
    99     public function __construct($options, Zend_Queue $queue = null)
       
   100     {
       
   101         if ($options instanceof Zend_Config) {
       
   102             $options = $options->toArray();
       
   103         }
       
   104 
       
   105         /*
       
   106          * Verify that adapter parameters are in an array.
       
   107          */
       
   108         if (!is_array($options)) {
       
   109             require_once 'Zend/Queue/Exception.php';
       
   110             throw new Zend_Queue_Exception('Adapter options must be an array or Zend_Config object');
       
   111         }
       
   112 
       
   113         // set the queue
       
   114         if ($queue !== null) {
       
   115             $this->setQueue($queue);
       
   116         }
       
   117 
       
   118         $adapterOptions = array();
       
   119         $driverOptions  = array();
       
   120 
       
   121         // Normalize the options and merge with the defaults
       
   122         if (array_key_exists('options', $options)) {
       
   123             if (!is_array($options['options'])) {
       
   124                 require_once 'Zend/Queue/Exception.php';
       
   125                 throw new Zend_Queue_Exception("Configuration array 'options' must be an array");
       
   126             }
       
   127 
       
   128             // Can't use array_merge() because keys might be integers
       
   129             foreach ($options['options'] as $key => $value) {
       
   130                 $adapterOptions[$key] = $value;
       
   131             }
       
   132         }
       
   133         if (array_key_exists('driverOptions', $options)) {
       
   134             // can't use array_merge() because keys might be integers
       
   135             foreach ((array)$options['driverOptions'] as $key => $value) {
       
   136                 $driverOptions[$key] = $value;
       
   137             }
       
   138         }
       
   139         $this->_options = array_merge($this->_options, $options);
       
   140         $this->_options['options']       = $adapterOptions;
       
   141         $this->_options['driverOptions'] = $driverOptions;
       
   142     }
       
   143 
       
   144     /********************************************************************
       
   145     * Queue management functions
       
   146      *********************************************************************/
       
   147     /**
       
   148      * get the Zend_Queue class that is attached to this object
       
   149      *
       
   150      * @return Zend_Queue|null
       
   151      */
       
   152     public function getQueue()
       
   153     {
       
   154         return $this->_queue;
       
   155     }
       
   156 
       
   157     /**
       
   158      * set the Zend_Queue class for this object
       
   159      *
       
   160      * @param  Zend_Queue $queue
       
   161      * @return Zend_Queue_Adapter_AdapterInterface
       
   162      */
       
   163     public function setQueue(Zend_Queue $queue)
       
   164     {
       
   165         $this->_queue = $queue;
       
   166         return $this;
       
   167     }
       
   168 
       
   169     /**
       
   170      * Returns the configuration options in this adapter.
       
   171      *
       
   172      * @return array
       
   173      */
       
   174     public function getOptions()
       
   175     {
       
   176         return $this->_options;
       
   177     }
       
   178 
       
   179     /**
       
   180      * Indicates if a function is supported or not.
       
   181      *
       
   182      * @param  string $name
       
   183      * @return boolean
       
   184      */
       
   185     public function isSupported($name)
       
   186     {
       
   187         $list = $this->getCapabilities();
       
   188 
       
   189         return (isset($list[$name]) && $list[$name]);
       
   190      }
       
   191 }