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

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_Tool
0017  * @subpackage Framework
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_Loader_Autoloader
0025  */
0026 // require_once 'Zend/Loader/Autoloader.php';
0027 
0028 /**
0029  * @see Zend_Tool_Framework_Registry_EnabledInterface
0030  */
0031 // require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_Tool
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 abstract class Zend_Tool_Framework_Client_Abstract implements Zend_Tool_Framework_Registry_EnabledInterface
0040 {
0041 
0042     /**
0043      * @var Zend_Tool_Framework_Registry
0044      */
0045     protected $_registry = null;
0046 
0047     /**
0048      * @var callback|null
0049      */
0050     protected $_interactiveCallback = null;
0051 
0052     /**
0053      * @var bool
0054      */
0055     protected $_isInitialized = false;
0056 
0057     /**
0058      * @var Zend_Log
0059      */
0060     protected $_debugLogger = null;
0061 
0062     public function __construct($options = array())
0063     {
0064         // require autoloader
0065         Zend_Loader_Autoloader::getInstance();
0066 
0067         // this might look goofy, but this is setting up the
0068         // registry for dependency injection into the client
0069         $registry = new Zend_Tool_Framework_Registry();
0070         $registry->setClient($this);
0071 
0072         // NOTE: at this moment, $this->_registry should contain the registry object
0073 
0074         if ($options) {
0075             $this->setOptions($options);
0076         }
0077     }
0078 
0079     public function setOptions(Array $options)
0080     {
0081         foreach ($options as $optionName => $optionValue) {
0082             $setMethodName = 'set' . $optionName;
0083             if (method_exists($this, $setMethodName)) {
0084                 $this->{$setMethodName}($optionValue);
0085             }
0086         }
0087     }
0088 
0089     /**
0090      * getName() - Return the client name which can be used to
0091      * query the manifest if need be.
0092      *
0093      * @return string The client name
0094      */
0095     abstract public function getName();
0096 
0097     /**
0098      * initialized() - This will initialize the client for use
0099      *
0100      */
0101     public function initialize()
0102     {
0103         // if its already initialized, no need to initialize again
0104         if ($this->_isInitialized) {
0105             return;
0106         }
0107 
0108         // run any preInit
0109         $this->_preInit();
0110 
0111         $manifest = $this->_registry->getManifestRepository();
0112         $manifest->addManifest(new Zend_Tool_Framework_Client_Manifest());
0113 
0114         // setup the debug log
0115         if (!$this->_debugLogger instanceof Zend_Log) {
0116             // require_once 'Zend/Log.php';
0117             // require_once 'Zend/Log/Writer/Null.php';
0118             $this->_debugLogger = new Zend_Log(new Zend_Log_Writer_Null());
0119         }
0120 
0121         // let the loader load, then the repositories process whats been loaded
0122         $this->_registry->getLoader()->load();
0123 
0124         // process the action repository
0125         $this->_registry->getActionRepository()->process();
0126 
0127         // process the provider repository
0128         $this->_registry->getProviderRepository()->process();
0129 
0130         // process the manifest repository
0131         $this->_registry->getManifestRepository()->process();
0132 
0133         if ($this instanceof Zend_Tool_Framework_Client_Interactive_InputInterface) {
0134             // require_once 'Zend/Tool/Framework/Client/Interactive/InputHandler.php';
0135         }
0136 
0137         if ($this instanceof Zend_Tool_Framework_Client_Interactive_OutputInterface) {
0138             $this->_registry->getResponse()->setContentCallback(array($this, 'handleInteractiveOutput'));
0139         }
0140 
0141     }
0142 
0143 
0144     /**
0145      * This method should be implemented by the client implementation to
0146      * construct and set custom inflectors, request and response objects.
0147      */
0148     protected function _preInit()
0149     {
0150     }
0151 
0152     /**
0153      * This method *must* be implemented by the client implementation to
0154      * parse out and setup the request objects action, provider and parameter
0155      * information.
0156      */
0157     abstract protected function _preDispatch();
0158 
0159     /**
0160      * This method should be implemented by the client implementation to
0161      * take the output of the response object and return it (in an client
0162      * specific way) back to the Tooling Client.
0163      */
0164     protected function _postDispatch()
0165     {
0166     }
0167 
0168     /**
0169      * setRegistry() - Required by the Zend_Tool_Framework_Registry_EnabledInterface
0170      * interface which ensures proper registry dependency resolution
0171      *
0172      * @param Zend_Tool_Framework_Registry_Interface $registry
0173      * @return Zend_Tool_Framework_Client_Abstract
0174      */
0175     public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
0176     {
0177         $this->_registry = $registry;
0178         return $this;
0179     }
0180 
0181     /**
0182      * getRegistry();
0183      *
0184      * @return Zend_Tool_Framework_Registry_Interface
0185      */
0186     public function getRegistry()
0187     {
0188         return $this->_registry;
0189     }
0190 
0191     /**
0192      * hasInteractiveInput() - Convienence method for determining if this
0193      * client can handle interactive input, and thus be able to run the
0194      * promptInteractiveInput
0195      *
0196      * @return bool
0197      */
0198     public function hasInteractiveInput()
0199     {
0200         return ($this instanceof Zend_Tool_Framework_Client_Interactive_InputInterface);
0201     }
0202 
0203     public function promptInteractiveInput($inputRequest)
0204     {
0205         if (!$this->hasInteractiveInput()) {
0206             // require_once 'Zend/Tool/Framework/Client/Exception.php';
0207             throw new Zend_Tool_Framework_Client_Exception('promptInteractive() cannot be called on a non-interactive client.');
0208         }
0209 
0210         $inputHandler = new Zend_Tool_Framework_Client_Interactive_InputHandler();
0211         $inputHandler->setClient($this);
0212         $inputHandler->setInputRequest($inputRequest);
0213         return $inputHandler->handle();
0214 
0215     }
0216 
0217     /**
0218      * This method should be called in order to "handle" a Tooling Client
0219      * request that has come to the client that has been implemented.
0220      */
0221     public function dispatch()
0222     {
0223         $this->initialize();
0224 
0225         try {
0226 
0227             $this->_preDispatch();
0228 
0229             if ($this->_registry->getRequest()->isDispatchable()) {
0230 
0231                 if ($this->_registry->getRequest()->getActionName() == null) {
0232                     // require_once 'Zend/Tool/Framework/Client/Exception.php';
0233                     throw new Zend_Tool_Framework_Client_Exception('Client failed to setup the action name.');
0234                 }
0235 
0236                 if ($this->_registry->getRequest()->getProviderName() == null) {
0237                     // require_once 'Zend/Tool/Framework/Client/Exception.php';
0238                     throw new Zend_Tool_Framework_Client_Exception('Client failed to setup the provider name.');
0239                 }
0240 
0241                 $this->_handleDispatch();
0242 
0243             }
0244 
0245         } catch (Exception $exception) {
0246             $this->_registry->getResponse()->setException($exception);
0247         }
0248 
0249         $this->_postDispatch();
0250     }
0251 
0252     public function convertToClientNaming($string)
0253     {
0254         return $string;
0255     }
0256 
0257     public function convertFromClientNaming($string)
0258     {
0259         return $string;
0260     }
0261 
0262     protected function _handleDispatch()
0263     {
0264         // get the provider repository
0265         $providerRepository = $this->_registry->getProviderRepository();
0266 
0267         $request = $this->_registry->getRequest();
0268 
0269         // get the dispatchable provider signature
0270         $providerSignature = $providerRepository->getProviderSignature($request->getProviderName());
0271 
0272         // get the actual provider
0273         $provider = $providerSignature->getProvider();
0274 
0275         // ensure that we can pretend if this is a pretend request
0276         if ($request->isPretend() && (!$provider instanceof Zend_Tool_Framework_Provider_Pretendable)) {
0277             // require_once 'Zend/Tool/Framework/Client/Exception.php';
0278             throw new Zend_Tool_Framework_Client_Exception('Dispatcher error - provider does not support pretend');
0279         }
0280 
0281         // get the action name
0282         $actionName = $this->_registry->getRequest()->getActionName();
0283         $specialtyName = $this->_registry->getRequest()->getSpecialtyName();
0284 
0285         if (!$actionableMethod = $providerSignature->getActionableMethodByActionName($actionName, $specialtyName)) {
0286             // require_once 'Zend/Tool/Framework/Client/Exception.php';
0287             throw new Zend_Tool_Framework_Client_Exception('Dispatcher error - actionable method not found');
0288         }
0289 
0290         // get the actual method and param information
0291         $methodName       = $actionableMethod['methodName'];
0292         $methodParameters = $actionableMethod['parameterInfo'];
0293 
0294         // get the provider params
0295         $requestParameters = $this->_registry->getRequest()->getProviderParameters();
0296 
0297         // @todo This seems hackish, determine if there is a better way
0298         $callParameters = array();
0299         foreach ($methodParameters as $methodParameterName => $methodParameterValue) {
0300             if (!array_key_exists($methodParameterName, $requestParameters) && $methodParameterValue['optional'] == false) {
0301                 if ($this instanceof Zend_Tool_Framework_Client_Interactive_InputInterface) {
0302                     $promptSting = $this->getMissingParameterPromptString($provider, $actionableMethod['action'], $methodParameterValue['name']);
0303                     $parameterPromptValue = $this->promptInteractiveInput($promptSting)->getContent();
0304                     if ($parameterPromptValue == null) {
0305                         // require_once 'Zend/Tool/Framework/Client/Exception.php';
0306                         throw new Zend_Tool_Framework_Client_Exception('Value supplied for required parameter "' . $methodParameterValue['name'] . '" is empty');
0307                     }
0308                     $callParameters[] = $parameterPromptValue;
0309                 } else {
0310                     // require_once 'Zend/Tool/Framework/Client/Exception.php';
0311                     throw new Zend_Tool_Framework_Client_Exception('A required parameter "' . $methodParameterValue['name'] . '" was not supplied.');
0312                 }
0313             } else {
0314                 $callParameters[] = (array_key_exists($methodParameterName, $requestParameters)) ? $requestParameters[$methodParameterName] : $methodParameterValue['default'];
0315             }
0316         }
0317 
0318         $this->_handleDispatchExecution($provider, $methodName, $callParameters);
0319     }
0320 
0321     protected function _handleDispatchExecution($class, $methodName, $callParameters)
0322     {
0323         if (method_exists($class, $methodName)) {
0324             call_user_func_array(array($class, $methodName), $callParameters);
0325         } elseif (method_exists($class, $methodName . 'Action')) {
0326             call_user_func_array(array($class, $methodName . 'Action'), $callParameters);
0327         } else {
0328             // require_once 'Zend/Tool/Framework/Client/Exception.php';
0329             throw new Zend_Tool_Framework_Client_Exception('Not a supported method.');
0330         }
0331     }
0332 
0333 }