web/lib/Zend/Tool/Framework/Client/Console.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_Tool
       
    17  * @subpackage Framework
       
    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: Console.php 23204 2010-10-21 15:35:21Z ralph $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Framework_Client_Abstract
       
    25  */
       
    26 require_once 'Zend/Tool/Framework/Client/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Tool_Framework_Client_Interactive_InputInterface
       
    30  */
       
    31 require_once 'Zend/Tool/Framework/Client/Interactive/InputInterface.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Tool_Framework_Client_Interactive_OutputInterface
       
    35  */
       
    36 require_once 'Zend/Tool/Framework/Client/Interactive/OutputInterface.php';
       
    37 
       
    38 /**
       
    39  * Zend_Tool_Framework_Client_Console - the CLI Client implementation for Zend_Tool_Framework
       
    40  *
       
    41  * @category   Zend
       
    42  * @package    Zend_Tool
       
    43  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    44  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    45  */
       
    46 class Zend_Tool_Framework_Client_Console
       
    47     extends Zend_Tool_Framework_Client_Abstract
       
    48     implements Zend_Tool_Framework_Client_Interactive_InputInterface,
       
    49                Zend_Tool_Framework_Client_Interactive_OutputInterface
       
    50 {
       
    51 
       
    52     /**
       
    53      * @var array
       
    54      */
       
    55     protected $_configOptions = null;
       
    56 
       
    57     /**
       
    58      * @var array
       
    59      */
       
    60     protected $_storageOptions = null;
       
    61 
       
    62     /**
       
    63      * @var Zend_Filter_Word_CamelCaseToDash
       
    64      */
       
    65     protected $_filterToClientNaming = null;
       
    66 
       
    67     /**
       
    68      * @var Zend_Filter_Word_DashToCamelCase
       
    69      */
       
    70     protected $_filterFromClientNaming = null;
       
    71 
       
    72     /**
       
    73      * @var array
       
    74      */
       
    75     protected $_classesToLoad = array();
       
    76     
       
    77     /**
       
    78      * main() - This is typically called from zf.php. This method is a
       
    79      * self contained main() function.
       
    80      *
       
    81      */
       
    82     public static function main($options = array())
       
    83     {
       
    84         $cliClient = new self($options);
       
    85         $cliClient->dispatch();
       
    86     }
       
    87 
       
    88     /**
       
    89      * getName() - return the name of the client, in this case 'console'
       
    90      *
       
    91      * @return string
       
    92      */
       
    93     public function getName()
       
    94     {
       
    95         return 'console';
       
    96     }
       
    97     
       
    98     /**
       
    99      * setConfigOptions()
       
   100      * 
       
   101      * @param $configOptions
       
   102      */
       
   103     public function setConfigOptions($configOptions)
       
   104     {
       
   105         $this->_configOptions = $configOptions;
       
   106         return $this;
       
   107     }
       
   108 
       
   109     /**
       
   110      * setStorageOptions()
       
   111      * 
       
   112      * @param $storageOptions
       
   113      */
       
   114     public function setStorageOptions($storageOptions)
       
   115     {
       
   116         $this->_storageOptions = $storageOptions;
       
   117         return $this;
       
   118     }
       
   119     
       
   120     public function setClassesToLoad($classesToLoad)
       
   121     {
       
   122         $this->_classesToLoad = $classesToLoad;
       
   123         return $this;
       
   124     }
       
   125 
       
   126     /**
       
   127      * _init() - Tasks processed before the constructor, generally setting up objects to use
       
   128      *
       
   129      */
       
   130     protected function _preInit()
       
   131     {
       
   132         $config = $this->_registry->getConfig();
       
   133 
       
   134         if ($this->_configOptions != null) {
       
   135             $config->setOptions($this->_configOptions);
       
   136         }
       
   137 
       
   138         $storage = $this->_registry->getStorage();
       
   139 
       
   140         if ($this->_storageOptions != null && isset($this->_storageOptions['directory'])) {
       
   141             $storage->setAdapter(
       
   142                 new Zend_Tool_Framework_Client_Storage_Directory($this->_storageOptions['directory'])
       
   143                 );
       
   144         }
       
   145 
       
   146         // which classes are essential to initializing Zend_Tool_Framework_Client_Console
       
   147         $classesToLoad = array(
       
   148             'Zend_Tool_Framework_Client_Console_Manifest',    
       
   149             'Zend_Tool_Framework_System_Manifest'
       
   150             );
       
   151             
       
   152         if ($this->_classesToLoad) {
       
   153             if (is_string($this->_classesToLoad)) {
       
   154                 $classesToLoad[] = $this->_classesToLoad;
       
   155             } elseif (is_array($this->_classesToLoad)) {
       
   156                 $classesToLoad = array_merge($classesToLoad, $this->_classesToLoad);
       
   157             }
       
   158         }
       
   159         
       
   160         // add classes to the basic loader from the config file basicloader.classes.1 ..
       
   161         if (isset($config->basicloader) && isset($config->basicloader->classes)) {
       
   162             foreach ($config->basicloader->classes as $classKey => $className) {
       
   163                 array_push($classesToLoad, $className);
       
   164             }
       
   165         }
       
   166 
       
   167         $this->_registry->setLoader(
       
   168             new Zend_Tool_Framework_Loader_BasicLoader(array('classesToLoad' => $classesToLoad))
       
   169             );
       
   170 
       
   171         return;
       
   172     }
       
   173 
       
   174     /**
       
   175      * _preDispatch() - Tasks handed after initialization but before dispatching
       
   176      *
       
   177      */
       
   178     protected function _preDispatch()
       
   179     {
       
   180         $response = $this->_registry->getResponse();
       
   181 
       
   182         $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_AlignCenter());
       
   183         $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Indention());
       
   184         $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Blockize());
       
   185 
       
   186         if (function_exists('posix_isatty')) {
       
   187             $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer());
       
   188         }
       
   189         
       
   190         $response->addContentDecorator(new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator())
       
   191             ->setDefaultDecoratorOptions(array('separator' => true));
       
   192 
       
   193         $optParser = new Zend_Tool_Framework_Client_Console_ArgumentParser();
       
   194         $optParser->setArguments($_SERVER['argv'])
       
   195             ->setRegistry($this->_registry)
       
   196             ->parse();
       
   197 
       
   198         return;
       
   199     }
       
   200 
       
   201     /**
       
   202      * _postDispatch() - Tasks handled after dispatching
       
   203      *
       
   204      */
       
   205     protected function _postDispatch()
       
   206     {
       
   207         $request = $this->_registry->getRequest();
       
   208         $response = $this->_registry->getResponse();
       
   209 
       
   210         if ($response->isException()) {
       
   211             $helpSystem = new Zend_Tool_Framework_Client_Console_HelpSystem();
       
   212             $helpSystem->setRegistry($this->_registry)
       
   213                 ->respondWithErrorMessage($response->getException()->getMessage(), $response->getException())
       
   214                 ->respondWithSpecialtyAndParamHelp(
       
   215                     $request->getProviderName(),
       
   216                     $request->getActionName()
       
   217                     );
       
   218         }
       
   219 
       
   220         echo PHP_EOL;
       
   221         return;
       
   222     }
       
   223 
       
   224     /**
       
   225      * handleInteractiveInputRequest() is required by the Interactive InputInterface
       
   226      *
       
   227      *
       
   228      * @param Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest
       
   229      * @return string
       
   230      */
       
   231     public function handleInteractiveInputRequest(Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest)
       
   232     {
       
   233         fwrite(STDOUT, $inputRequest->getContent() . PHP_EOL . 'zf> ');
       
   234         $inputContent = fgets(STDIN);
       
   235         return rtrim($inputContent); // remove the return from the end of the string
       
   236     }
       
   237 
       
   238     /**
       
   239      * handleInteractiveOutput() is required by the Interactive OutputInterface
       
   240      *
       
   241      * This allows us to display output immediately from providers, rather
       
   242      * than displaying it after the provider is done.
       
   243      *
       
   244      * @param string $output
       
   245      */
       
   246     public function handleInteractiveOutput($output)
       
   247     {
       
   248         echo $output;
       
   249     }
       
   250 
       
   251     /**
       
   252      * getMissingParameterPromptString()
       
   253      *
       
   254      * @param Zend_Tool_Framework_Provider_Interface $provider
       
   255      * @param Zend_Tool_Framework_Action_Interface $actionInterface
       
   256      * @param string $missingParameterName
       
   257      * @return string
       
   258      */
       
   259     public function getMissingParameterPromptString(Zend_Tool_Framework_Provider_Interface $provider, Zend_Tool_Framework_Action_Interface $actionInterface, $missingParameterName)
       
   260     {
       
   261         return 'Please provide a value for $' . $missingParameterName;
       
   262     }
       
   263 
       
   264 
       
   265     /**
       
   266      * convertToClientNaming()
       
   267      *
       
   268      * Convert words to client specific naming, in this case is lower, dash separated
       
   269      *
       
   270      * Filters are lazy-loaded.
       
   271      *
       
   272      * @param string $string
       
   273      * @return string
       
   274      */
       
   275     public function convertToClientNaming($string)
       
   276     {
       
   277         if (!$this->_filterToClientNaming) {
       
   278             $filter = new Zend_Filter();
       
   279             $filter->addFilter(new Zend_Filter_Word_CamelCaseToDash());
       
   280             $filter->addFilter(new Zend_Filter_StringToLower());
       
   281 
       
   282             $this->_filterToClientNaming = $filter;
       
   283         }
       
   284 
       
   285         return $this->_filterToClientNaming->filter($string);
       
   286     }
       
   287 
       
   288     /**
       
   289      * convertFromClientNaming()
       
   290      *
       
   291      * Convert words from client specific naming to code naming - camelcased
       
   292      *
       
   293      * Filters are lazy-loaded.
       
   294      *
       
   295      * @param string $string
       
   296      * @return string
       
   297      */
       
   298     public function convertFromClientNaming($string)
       
   299     {
       
   300         if (!$this->_filterFromClientNaming) {
       
   301             $this->_filterFromClientNaming = new Zend_Filter_Word_DashToCamelCase();
       
   302         }
       
   303 
       
   304         return $this->_filterFromClientNaming->filter($string);
       
   305     }
       
   306 
       
   307 }