File indexing completed on 2025-01-19 05:21:24

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_Adapter_AdapterAbstract
0025  */
0026 // require_once 'Zend/Queue/Adapter/AdapterAbstract.php';
0027 
0028 /**
0029  * Zend Platform JobQueue adapter
0030  *
0031  * @category   Zend
0032  * @package    Zend_Queue
0033  * @subpackage Adapter
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Queue_Adapter_PlatformJobQueue extends Zend_Queue_Adapter_AdapterAbstract
0038 {
0039     /**
0040      * @var ZendApi_JobQueue
0041      */
0042     protected $_zendQueue;
0043 
0044     /**
0045      * Constructor
0046      *
0047      * @param  array|Zend_Config $options
0048      * @param  Zend_Queue|null $queue
0049      * @return void
0050      */
0051     public function __construct($options, Zend_Queue $queue = null)
0052     {
0053         parent::__construct($options, $queue);
0054 
0055         if (!extension_loaded("jobqueue_client")) {
0056             // require_once 'Zend/Queue/Exception.php';
0057             throw new Zend_Queue_Exception('Platform Job Queue extension does not appear to be loaded');
0058         }
0059 
0060         if (! isset($this->_options['daemonOptions'])) {
0061             // require_once 'Zend/Queue/Exception.php';
0062             throw new Zend_Queue_Exception('Job Queue host and password should be provided');
0063         }
0064 
0065         $options = $this->_options['daemonOptions'];
0066 
0067         if (!array_key_exists('host', $options)) {
0068             // require_once 'Zend/Queue/Exception.php';
0069             throw new Zend_Queue_Exception('Platform Job Queue host should be provided');
0070         }
0071         if (!array_key_exists('password', $options)) {
0072             // require_once 'Zend/Queue/Exception.php';
0073             throw new Zend_Queue_Exception('Platform Job Queue password should be provided');
0074         }
0075 
0076         $this->_zendQueue = new ZendApi_Queue($options['host']);
0077 
0078         if (!$this->_zendQueue) {
0079             // require_once 'Zend/Queue/Exception.php';
0080             throw new Zend_Queue_Exception('Platform Job Queue connection failed');
0081         }
0082         if (!$this->_zendQueue->login($options['password'])) {
0083             // require_once 'Zend/Queue/Exception.php';
0084             throw new Zend_Queue_Exception('Job Queue login failed');
0085         }
0086 
0087         if ($this->_queue) {
0088             $this->_queue->setMessageClass('Zend_Queue_Message_PlatformJob');
0089         }
0090     }
0091 
0092     /********************************************************************
0093      * Queue management functions
0094      ********************************************************************/
0095 
0096     /**
0097      * Does a queue already exist?
0098      *
0099      * @param  string $name
0100      * @return boolean
0101      * @throws Zend_Queue_Exception (not supported)
0102      */
0103     public function isExists($name)
0104     {
0105         // require_once 'Zend/Queue/Exception.php';
0106         throw new Zend_Queue_Exception('isExists() is not supported in this adapter');
0107     }
0108 
0109     /**
0110      * Create a new queue
0111      *
0112      * @param  string  $name    queue name
0113      * @param  integer $timeout default visibility timeout
0114      * @return void
0115      * @throws Zend_Queue_Exception
0116      */
0117     public function create($name, $timeout=null)
0118     {
0119         // require_once 'Zend/Queue/Exception.php';
0120         throw new Zend_Queue_Exception('create() is not supported in ' . get_class($this));
0121     }
0122 
0123     /**
0124      * Delete a queue and all of its messages
0125      *
0126      * @param  string $name queue name
0127      * @return void
0128      * @throws Zend_Queue_Exception
0129      */
0130     public function delete($name)
0131     {
0132         // require_once 'Zend/Queue/Exception.php';
0133         throw new Zend_Queue_Exception('delete() is not supported in ' . get_class($this));
0134     }
0135 
0136     /**
0137      * Get an array of all available queues
0138      *
0139      * @return void
0140      * @throws Zend_Queue_Exception
0141      */
0142     public function getQueues()
0143     {
0144         // require_once 'Zend/Queue/Exception.php';
0145         throw new Zend_Queue_Exception('getQueues() is not supported in this adapter');
0146     }
0147 
0148     /**
0149      * Return the approximate number of messages in the queue
0150      *
0151      * @param  Zend_Queue|null $queue
0152      * @return integer
0153      */
0154     public function count(Zend_Queue $queue = null)
0155     {
0156         if ($queue !== null) {
0157             // require_once 'Zend/Queue/Exception.php';
0158             throw new Zend_Queue_Exception('Queue parameter is not supported');
0159         }
0160 
0161         return $this->_zendQueue->getNumOfJobsInQueue();
0162     }
0163 
0164     /********************************************************************
0165      * Messsage management functions
0166      ********************************************************************/
0167 
0168     /**
0169      * Send a message to the queue
0170      *
0171      * @param  array | ZendAPI_job $message Message to send to the active queue
0172      * @param  Zend_Queue $queue     Not supported
0173      * @return Zend_Queue_Message
0174      * @throws Zend_Queue_Exception
0175      */
0176     public function send($message, Zend_Queue $queue = null)
0177     {
0178         if ($queue !== null) {
0179             // require_once 'Zend/Queue/Exception.php';
0180             throw new Zend_Queue_Exception('Queue parameter is not supported');
0181         }
0182 
0183         // This adapter can work only for this message type
0184         $classname = $this->_queue->getMessageClass();
0185         if (!class_exists($classname)) {
0186             // require_once 'Zend/Loader.php';
0187             Zend_Loader::loadClass($classname);
0188         }
0189 
0190         if ($message instanceof ZendAPI_Job) {
0191             $message = array('data' => $message);
0192         }
0193 
0194         $zendApiJob = new $classname($message);
0195 
0196         // Unfortunately, the Platform JQ API is PHP4-style...
0197         $platformJob = $zendApiJob->getJob();
0198 
0199         $jobId = $this->_zendQueue->addJob($platformJob);
0200 
0201         if (!$jobId) {
0202             // require_once 'Zend/Queue/Exception.php';
0203             throw new Zend_Queue_Exception('Failed to add a job to queue: '
0204                 . $this->_zendQueue->getLastError());
0205         }
0206 
0207         $zendApiJob->setJobId($jobId);
0208         return $zendApiJob;
0209     }
0210 
0211     /**
0212      * Get messages in the queue
0213      *
0214      * @param  integer    $maxMessages  Maximum number of messages to return
0215      * @param  integer    $timeout      Ignored
0216      * @param  Zend_Queue $queue        Not supported
0217      * @throws Zend_Queue_Exception
0218      * @return ArrayIterator
0219      */
0220     public function receive($maxMessages = null, $timeout = null, Zend_Queue $queue = null)
0221     {
0222         if ($maxMessages === null) {
0223             $maxMessages = 1;
0224         }
0225 
0226         if ($queue !== null) {
0227             // require_once 'Zend/Queue/Exception.php';
0228             throw new Zend_Queue_Exception('Queue shouldn\'t be set');
0229         }
0230 
0231         $jobs = $this->_zendQueue->getJobsInQueue(null, $maxMessages, true);
0232 
0233         $classname = $this->_queue->getMessageClass();
0234         if (!class_exists($classname)) {
0235             // require_once 'Zend/Loader.php';
0236             Zend_Loader::loadClass($classname);
0237         }
0238 
0239         $options = array(
0240             'queue'        => $this->_queue,
0241             'data'         => $jobs,
0242             'messageClass' => $this->_queue->getMessageClass(),
0243         );
0244 
0245         $classname = $this->_queue->getMessageSetClass();
0246 
0247         if (!class_exists($classname)) {
0248             // require_once 'Zend/Loader.php';
0249             Zend_Loader::loadClass($classname);
0250         }
0251         return new $classname($options);
0252     }
0253 
0254     /**
0255      * Delete a message from the queue
0256      *
0257      * Returns true if the message is deleted, false if the deletion is
0258      * unsuccessful.
0259      *
0260      * @param  Zend_Queue_Message $message
0261      * @return boolean
0262      * @throws Zend_Queue_Exception
0263      */
0264     public function deleteMessage(Zend_Queue_Message $message)
0265     {
0266         if (get_class($message) != $this->_queue->getMessageClass()) {
0267             // require_once 'Zend/Queue/Exception.php';
0268             throw new Zend_Queue_Exception(
0269                 'Failed to remove job from the queue; only messages of type '
0270                 . 'Zend_Queue_Message_PlatformJob may be used'
0271             );
0272         }
0273 
0274         return $this->_zendQueue->removeJob($message->getJobId());
0275     }
0276 
0277     public function isJobIdExist($id)
0278     {
0279          return (($this->_zendQueue->getJob($id))? true : false);
0280     }
0281 
0282     /********************************************************************
0283      * Supporting functions
0284      ********************************************************************/
0285 
0286     /**
0287      * Return a list of queue capabilities functions
0288      *
0289      * $array['function name'] = true or false
0290      * true is supported, false is not supported.
0291      *
0292      * @param  string $name
0293      * @return array
0294      */
0295     public function getCapabilities()
0296     {
0297          return array(
0298             'create'                => false,
0299             'delete'                => false,
0300             'getQueues'             => false,
0301             'isExists'              => false,
0302             'count'                 => true,
0303             'send'                  => true,
0304             'receive'               => true,
0305             'deleteMessage'         => true,
0306         );
0307     }
0308 
0309     /********************************************************************
0310      * Functions that are not part of the Zend_Queue_Adapter_AdapterAbstract
0311      ********************************************************************/
0312 
0313     /**
0314      * Serialize
0315      *
0316      * @return array
0317      */
0318     public function __sleep()
0319     {
0320         return array('_options');
0321     }
0322 
0323     /**
0324      * Unserialize
0325      *
0326      * @return void
0327      */
0328     public function __wakeup()
0329     {
0330         $options = $this->_options['daemonOptions'];
0331 
0332         $this->_zendQueue = new ZendApi_Queue($options['host']);
0333 
0334         if (!$this->_zendQueue) {
0335             // require_once 'Zend/Queue/Exception.php';
0336             throw new Zend_Queue_Exception('Platform Job Queue connection failed');
0337         }
0338         if (!$this->_zendQueue->login($options['password'])) {
0339             // require_once 'Zend/Queue/Exception.php';
0340             throw new Zend_Queue_Exception('Job Queue login failed');
0341         }
0342     }
0343 }