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_Tool_Framework_Client_Abstract
0025  */
0026 // require_once 'Zend/Tool/Framework/Client/Abstract.php';
0027 
0028 /**
0029  * @see Zend_Tool_Framework_Client_Interactive_InputInterface
0030  */
0031 // require_once 'Zend/Tool/Framework/Client/Interactive/InputInterface.php';
0032 
0033 /**
0034  * @see Zend_Tool_Framework_Client_Interactive_OutputInterface
0035  */
0036 // require_once 'Zend/Tool/Framework/Client/Interactive/OutputInterface.php';
0037 
0038 /**
0039  * Zend_Tool_Framework_Client_Console - the CLI Client implementation for Zend_Tool_Framework
0040  *
0041  * @category   Zend
0042  * @package    Zend_Tool
0043  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0044  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0045  *
0046  * @todo methods need more API documentation.
0047  */
0048 class Zend_Tool_Framework_Client_Console
0049     extends Zend_Tool_Framework_Client_Abstract
0050     implements Zend_Tool_Framework_Client_Interactive_InputInterface,
0051                Zend_Tool_Framework_Client_Interactive_OutputInterface
0052 {
0053 
0054     /**
0055      * @var array
0056      */
0057     protected $_configOptions = null;
0058 
0059     /**
0060      * @var array
0061      */
0062     protected $_storageOptions = null;
0063 
0064     /**
0065      * @var Zend_Filter_Word_CamelCaseToDash
0066      */
0067     protected $_filterToClientNaming = null;
0068 
0069     /**
0070      * @var Zend_Filter_Word_DashToCamelCase
0071      */
0072     protected $_filterFromClientNaming = null;
0073 
0074     /**
0075      * @var array
0076      */
0077     protected $_classesToLoad = array();
0078 
0079     /**
0080      * main() - This is typically called from zf.php. This method is a
0081      * self contained main() function.
0082      *
0083      */
0084     public static function main($options = array())
0085     {
0086         $cliClient = new self($options);
0087         $cliClient->dispatch();
0088     }
0089 
0090     /**
0091      * getName() - return the name of the client, in this case 'console'
0092      *
0093      * @return string
0094      */
0095     public function getName()
0096     {
0097         return 'console';
0098     }
0099 
0100     /**
0101      * setConfigOptions()
0102      *
0103      * @param array $configOptions
0104      */
0105     public function setConfigOptions($configOptions)
0106     {
0107         $this->_configOptions = $configOptions;
0108         return $this;
0109     }
0110 
0111     /**
0112      * setStorageOptions()
0113      *
0114      * @param array $storageOptions
0115      */
0116     public function setStorageOptions($storageOptions)
0117     {
0118         $this->_storageOptions = $storageOptions;
0119         return $this;
0120     }
0121 
0122     /**
0123      * @param array $classesToLoad
0124      */
0125     public function setClassesToLoad($classesToLoad)
0126     {
0127         $this->_classesToLoad = $classesToLoad;
0128         return $this;
0129     }
0130 
0131     /**
0132      * _init() - Tasks processed before the constructor, generally setting up objects to use
0133      *
0134      */
0135     protected function _preInit()
0136     {
0137         $config = $this->_registry->getConfig();
0138 
0139         if ($this->_configOptions != null) {
0140             $config->setOptions($this->_configOptions);
0141         }
0142 
0143         $storage = $this->_registry->getStorage();
0144 
0145         if ($this->_storageOptions != null && isset($this->_storageOptions['directory'])) {
0146             $storage->setAdapter(
0147                 new Zend_Tool_Framework_Client_Storage_Directory($this->_storageOptions['directory'])
0148                 );
0149         }
0150 
0151         // which classes are essential to initializing Zend_Tool_Framework_Client_Console
0152         $classesToLoad = array(
0153             'Zend_Tool_Framework_Client_Console_Manifest',
0154             'Zend_Tool_Framework_System_Manifest'
0155             );
0156 
0157         if ($this->_classesToLoad) {
0158             if (is_string($this->_classesToLoad)) {
0159                 $classesToLoad[] = $this->_classesToLoad;
0160             } elseif (is_array($this->_classesToLoad)) {
0161                 $classesToLoad = array_merge($classesToLoad, $this->_classesToLoad);
0162             }
0163         }
0164 
0165         // add classes to the basic loader from the config file basicloader.classes.1 ..
0166         if (isset($config->basicloader) && isset($config->basicloader->classes)) {
0167             foreach ($config->basicloader->classes as $classKey => $className) {
0168                 array_push($classesToLoad, $className);
0169             }
0170         }
0171 
0172         $this->_registry->setLoader(
0173             new Zend_Tool_Framework_Loader_BasicLoader(array('classesToLoad' => $classesToLoad))
0174             );
0175 
0176         return;
0177     }
0178 
0179     /**
0180      * _preDispatch() - Tasks handed after initialization but before dispatching
0181      *
0182      */
0183     protected function _preDispatch()
0184     {
0185         $response = $this->_registry->getResponse();
0186 
0187         $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_AlignCenter());
0188         $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Indention());
0189         $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Blockize());
0190 
0191         if (function_exists('posix_isatty')) {
0192             $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer());
0193         }
0194 
0195         $response->addContentDecorator(new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator())
0196             ->setDefaultDecoratorOptions(array('separator' => true));
0197 
0198         $optParser = new Zend_Tool_Framework_Client_Console_ArgumentParser();
0199         $optParser->setArguments($_SERVER['argv'])
0200             ->setRegistry($this->_registry)
0201             ->parse();
0202 
0203         return;
0204     }
0205 
0206     /**
0207      * _postDispatch() - Tasks handled after dispatching
0208      *
0209      */
0210     protected function _postDispatch()
0211     {
0212         $request = $this->_registry->getRequest();
0213         $response = $this->_registry->getResponse();
0214 
0215         if ($response->isException()) {
0216             $helpSystem = new Zend_Tool_Framework_Client_Console_HelpSystem();
0217             $helpSystem->setRegistry($this->_registry)
0218                 ->respondWithErrorMessage($response->getException()->getMessage(), $response->getException())
0219                 ->respondWithSpecialtyAndParamHelp(
0220                     $request->getProviderName(),
0221                     $request->getActionName()
0222                     );
0223         }
0224 
0225         echo PHP_EOL;
0226         return;
0227     }
0228 
0229     /**
0230      * handleInteractiveInputRequest() is required by the Interactive InputInterface
0231      *
0232      *
0233      * @param Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest
0234      * @return string
0235      */
0236     public function handleInteractiveInputRequest(Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest)
0237     {
0238         fwrite(STDOUT, $inputRequest->getContent() . PHP_EOL . 'zf> ');
0239         $inputContent = fgets(STDIN);
0240         return rtrim($inputContent); // remove the return from the end of the string
0241     }
0242 
0243     /**
0244      * handleInteractiveOutput() is required by the Interactive OutputInterface
0245      *
0246      * This allows us to display output immediately from providers, rather
0247      * than displaying it after the provider is done.
0248      *
0249      * @param string $output
0250      */
0251     public function handleInteractiveOutput($output)
0252     {
0253         echo $output;
0254     }
0255 
0256     /**
0257      * getMissingParameterPromptString()
0258      *
0259      * @param Zend_Tool_Framework_Provider_Interface $provider
0260      * @param Zend_Tool_Framework_Action_Interface $actionInterface
0261      * @param string $missingParameterName
0262      * @return string
0263      */
0264     public function getMissingParameterPromptString(Zend_Tool_Framework_Provider_Interface $provider, Zend_Tool_Framework_Action_Interface $actionInterface, $missingParameterName)
0265     {
0266         return 'Please provide a value for $' . $missingParameterName;
0267     }
0268 
0269 
0270     /**
0271      * convertToClientNaming()
0272      *
0273      * Convert words to client specific naming, in this case is lower, dash separated
0274      *
0275      * Filters are lazy-loaded.
0276      *
0277      * @param string $string
0278      * @return string
0279      */
0280     public function convertToClientNaming($string)
0281     {
0282         if (!$this->_filterToClientNaming) {
0283             $filter = new Zend_Filter();
0284             $filter->addFilter(new Zend_Filter_Word_CamelCaseToDash());
0285             $filter->addFilter(new Zend_Filter_StringToLower());
0286 
0287             $this->_filterToClientNaming = $filter;
0288         }
0289 
0290         return $this->_filterToClientNaming->filter($string);
0291     }
0292 
0293     /**
0294      * convertFromClientNaming()
0295      *
0296      * Convert words from client specific naming to code naming - camelcased
0297      *
0298      * Filters are lazy-loaded.
0299      *
0300      * @param string $string
0301      * @return string
0302      */
0303     public function convertFromClientNaming($string)
0304     {
0305         if (!$this->_filterFromClientNaming) {
0306             $this->_filterFromClientNaming = new Zend_Filter_Word_DashToCamelCase();
0307         }
0308 
0309         return $this->_filterFromClientNaming->filter($string);
0310     }
0311 
0312 }