File indexing completed on 2025-01-26 05:29:59

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  * @category   Zend
0025  * @package    Zend_Tool
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */
0029 class Zend_Tool_Framework_Client_Console_HelpSystem
0030 {
0031 
0032     /**
0033      * @var Zend_Tool_Framework_Registry_Interface
0034      */
0035     protected $_registry = null;
0036 
0037     /**
0038      * @var Zend_Tool_Framework_Client_Response
0039      */
0040     protected $_response = null;
0041 
0042     /**
0043      * setRegistry()
0044      *
0045      * @param Zend_Tool_Framework_Registry_Interface $registry
0046      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0047      */
0048     public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
0049     {
0050         $this->_registry = $registry;
0051         $this->_response = $registry->getResponse();
0052         return $this;
0053     }
0054 
0055     /**
0056      * respondWithErrorMessage()
0057      *
0058      * @param string $errorMessage
0059      * @param Exception $exception
0060      */
0061     public function respondWithErrorMessage($errorMessage, Exception $exception = null)
0062     {
0063         // break apart the message into wrapped chunks
0064         $errorMessages = explode(PHP_EOL, wordwrap($errorMessage, 70, PHP_EOL, false));
0065 
0066         $text = 'An Error Has Occurred';
0067         $this->_response->appendContent($text, array('color' => array('hiWhite', 'bgRed'), 'aligncenter' => true));
0068         $this->_response->appendContent($errorMessage, array('indention' => 1, 'blockize' => 72, 'color' => array('white', 'bgRed')));
0069 
0070         if ($exception && $this->_registry->getRequest()->isDebug()) {
0071             $this->_response->appendContent($exception->getTraceAsString());
0072         }
0073 
0074         $this->_response->appendContent(null, array('separator' => true));
0075         return $this;
0076     }
0077 
0078     /**
0079      * respondWithGeneralHelp()
0080      *
0081      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0082      */
0083     public function respondWithGeneralHelp()
0084     {
0085         $this->_respondWithHeader();
0086 
0087         $noSeparator = array('separator' => false);
0088 
0089         $this->_response->appendContent('Usage:', array('color' => 'green'))
0090             ->appendContent('    ', $noSeparator)
0091             ->appendContent('zf', array_merge(array('color' => 'cyan'), $noSeparator))
0092             ->appendContent(' [--global-opts]', $noSeparator)
0093             ->appendContent(' action-name', array_merge(array('color' => 'cyan'), $noSeparator))
0094             ->appendContent(' [--action-opts]', $noSeparator)
0095             ->appendContent(' provider-name', array_merge(array('color' => 'cyan'), $noSeparator))
0096             ->appendContent(' [--provider-opts]', $noSeparator)
0097             ->appendContent(' [provider parameters ...]')
0098             ->appendContent('    Note: You may use "?" in any place of the above usage string to ask for more specific help information.', array('color'=>'yellow'))
0099             ->appendContent('    Example: "zf ? version" will list all available actions for the version provider.', array('color'=>'yellow', 'separator' => 2))
0100             ->appendContent('Providers and their actions:', array('color' => 'green'));
0101 
0102         $this->_respondWithSystemInformation();
0103         return $this;
0104     }
0105 
0106     /**
0107      * respondWithActionHelp()
0108      *
0109      * @param string $actionName
0110      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0111      */
0112     public function respondWithActionHelp($actionName)
0113     {
0114         $this->_respondWithHeader();
0115         $this->_response->appendContent('Providers that support the action "' . $actionName . '"', array('color' => 'green'));
0116         $this->_respondWithSystemInformation(null, $actionName);
0117         return $this;
0118     }
0119 
0120     /**
0121      * respondWithSpecialtyAndParamHelp()
0122      *
0123      * @param string $providerName
0124      * @param string $actionName
0125      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0126      */
0127     public function respondWithSpecialtyAndParamHelp($providerName, $actionName)
0128     {
0129         $this->_respondWithHeader();
0130         $this->_response->appendContent(
0131             'Details for action "' . $actionName . '" and provider "' . $providerName . '"',
0132             array('color' => 'green')
0133             );
0134         $this->_respondWithSystemInformation($providerName, $actionName, true);
0135         return $this;
0136     }
0137 
0138     /**
0139      * respondWithProviderHelp()
0140      *
0141      * @param string $providerName
0142      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0143      */
0144     public function respondWithProviderHelp($providerName)
0145     {
0146         $this->_respondWithHeader();
0147         $this->_response->appendContent('Actions supported by provider "' . $providerName . '"', array('color' => 'green'));
0148         $this->_respondWithSystemInformation($providerName);
0149         return $this;
0150     }
0151 
0152     /**
0153      * _respondWithHeader()
0154      *
0155      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0156      */
0157     protected function _respondWithHeader()
0158     {
0159         /**
0160          * @see Zend_Version
0161          */
0162         // require_once 'Zend/Version.php';
0163         $this->_response->appendContent('Zend Framework', array('color' => array('hiWhite'), 'separator' => false));
0164         $this->_response->appendContent(' Command Line Console Tool v' . Zend_Version::VERSION . '');
0165         return $this;
0166     }
0167 
0168     /**
0169      * _respondWithSystemInformation()
0170      *
0171      * @param string $providerNameFilter
0172      * @param string $actionNameFilter
0173      * @param bool $includeAllSpecialties
0174      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0175      */
0176     protected function _respondWithSystemInformation($providerNameFilter = null, $actionNameFilter = null, $includeAllSpecialties = false)
0177     {
0178         $manifest = $this->_registry->getManifestRepository();
0179 
0180         $providerMetadatasSearch = array(
0181             'type'       => 'Tool',
0182             'name'       => 'providerName',
0183             'clientName' => 'console'
0184             );
0185 
0186         if (is_string($providerNameFilter)) {
0187             $providerMetadatasSearch = array_merge($providerMetadatasSearch, array('providerName' => $providerNameFilter));
0188         }
0189 
0190         $actionMetadatasSearch = array(
0191             'type'       => 'Tool',
0192             'name'       => 'actionName',
0193             'clientName' => 'console'
0194             );
0195 
0196         if (is_string($actionNameFilter)) {
0197             $actionMetadatasSearch = array_merge($actionMetadatasSearch, array('actionName' => $actionNameFilter));
0198         }
0199 
0200         // get the metadata's for the things to display
0201         $displayProviderMetadatas = $manifest->getMetadatas($providerMetadatasSearch);
0202         $displayActionMetadatas = $manifest->getMetadatas($actionMetadatasSearch);
0203 
0204         // create index of actionNames
0205         for ($i = 0; $i < count($displayActionMetadatas); $i++) {
0206             $displayActionNames[] = $displayActionMetadatas[$i]->getActionName();
0207         }
0208 
0209         foreach ($displayProviderMetadatas as $providerMetadata) {
0210 
0211             $providerNameDisplayed = false;
0212 
0213             $providerName = $providerMetadata->getProviderName();
0214             $providerSignature = $providerMetadata->getReference();
0215 
0216             foreach ($providerSignature->getActions() as $actionInfo) {
0217 
0218                 $actionName = $actionInfo->getName();
0219 
0220                 // check to see if this action name is valid
0221                 if (($foundActionIndex = array_search($actionName, $displayActionNames)) === false) {
0222                     continue;
0223                 } else {
0224                     $actionMetadata = $displayActionMetadatas[$foundActionIndex];
0225                 }
0226 
0227                 $specialtyMetadata = $manifest->getMetadata(array(
0228                     'type'          => 'Tool',
0229                     'name'          => 'specialtyName',
0230                     'providerName'  => $providerName,
0231                     'specialtyName' => '_Global',
0232                     'clientName'    => 'console'
0233                     ));
0234 
0235                 // lets do the main _Global action first
0236                 $actionableGlobalLongParamMetadata = $manifest->getMetadata(array(
0237                     'type'          => 'Tool',
0238                     'name'          => 'actionableMethodLongParams',
0239                     'providerName'  => $providerName,
0240                     'specialtyName' => '_Global',
0241                     'actionName'    => $actionName,
0242                     'clientName'    => 'console'
0243                     ));
0244 
0245                 $actionableGlobalMetadatas = $manifest->getMetadatas(array(
0246                     'type'          => 'Tool',
0247                     'name'          => 'actionableMethodLongParams',
0248                     'providerName'  => $providerName,
0249                     'actionName'    => $actionName,
0250                     'clientName'    => 'console'
0251                     ));
0252 
0253                 if ($actionableGlobalLongParamMetadata) {
0254 
0255                     if (!$providerNameDisplayed) {
0256                         $this->_respondWithProviderName($providerMetadata);
0257                         $providerNameDisplayed = true;
0258                     }
0259 
0260                     $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableGlobalLongParamMetadata);
0261 
0262                     $actionIsGlobal = true;
0263                 } else {
0264                     $actionIsGlobal = false;
0265                 }
0266 
0267                 // check for providers without a _Global action
0268                 $isSingleSpecialProviderAction = false;
0269                 if (!$actionIsGlobal && count($actionableGlobalMetadatas) == 1) {
0270                     $isSingleSpecialProviderAction = true;
0271                     $this->_respondWithProviderName($providerMetadata);
0272                     $providerNameDisplayed = true;
0273                 }
0274 
0275                 if ($includeAllSpecialties || $isSingleSpecialProviderAction) {
0276 
0277                     foreach ($providerSignature->getSpecialties() as $specialtyName) {
0278 
0279                         if ($specialtyName == '_Global') {
0280                             continue;
0281                         }
0282 
0283                         $specialtyMetadata = $manifest->getMetadata(array(
0284                             'type'          => 'Tool',
0285                             'name'          => 'specialtyName',
0286                             'providerName'  => $providerMetadata->getProviderName(),
0287                             'specialtyName' => $specialtyName,
0288                             'clientName'    => 'console'
0289                             ));
0290 
0291                         $actionableSpecialtyLongMetadata = $manifest->getMetadata(array(
0292                             'type'          => 'Tool',
0293                             'name'          => 'actionableMethodLongParams',
0294                             'providerName'  => $providerMetadata->getProviderName(),
0295                             'specialtyName' => $specialtyName,
0296                             'actionName'    => $actionName,
0297                             'clientName'    => 'console'
0298                             ));
0299 
0300                         if($actionableSpecialtyLongMetadata) {
0301                             $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableSpecialtyLongMetadata);
0302                         }
0303 
0304                     }
0305                 }
0306 
0307                 // reset the special flag for single provider action with specialty
0308                 $isSingleSpecialProviderAction = false;
0309 
0310                 if (!$includeAllSpecialties && count($actionableGlobalMetadatas) > 1) {
0311                     $this->_response->appendContent('    Note: There are specialties, use ', array('color' => 'yellow', 'separator' => false));
0312                     $this->_response->appendContent(
0313                         'zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue() . '.?',
0314                         array('color' => 'cyan', 'separator' => false)
0315                         );
0316                     $this->_response->appendContent(' to get specific help on them.', array('color' => 'yellow'));
0317                 }
0318 
0319             }
0320 
0321             if ($providerNameDisplayed) {
0322                 $this->_response->appendContent(null, array('separator' => true));
0323             }
0324         }
0325         return $this;
0326     }
0327 
0328     /**
0329      * _respondWithProviderName()
0330      *
0331      * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
0332      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0333      */
0334     protected function _respondWithProviderName(Zend_Tool_Framework_Metadata_Tool $providerMetadata)
0335     {
0336         $this->_response->appendContent('  ' . $providerMetadata->getProviderName());
0337         return $this;
0338     }
0339 
0340     /**
0341      * _respondWithCommand()
0342      *
0343      * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
0344      * @param Zend_Tool_Framework_Metadata_Tool $actionMetadata
0345      * @param Zend_Tool_Framework_Metadata_Tool $specialtyMetadata
0346      * @param Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata
0347      * @return Zend_Tool_Framework_Client_Console_HelpSystem
0348      */
0349     protected function _respondWithCommand(
0350         Zend_Tool_Framework_Metadata_Tool $providerMetadata,
0351         Zend_Tool_Framework_Metadata_Tool $actionMetadata,
0352         Zend_Tool_Framework_Metadata_Tool $specialtyMetadata,
0353         Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata)//,
0354         //Zend_Tool_Framework_Metadata_Tool $parameterShortMetadata)
0355     {
0356         $this->_response->appendContent(
0357             '    zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue(),
0358             array('color' => 'cyan', 'separator' => false)
0359             );
0360 
0361         if ($specialtyMetadata->getSpecialtyName() != '_Global') {
0362             $this->_response->appendContent('.' . $specialtyMetadata->getValue(), array('color' => 'cyan', 'separator' => false));
0363         }
0364 
0365         foreach ($parameterLongMetadata->getValue() as $paramName => $consoleParamName) {
0366             $methodInfo = $parameterLongMetadata->getReference();
0367             $paramString = ' ' . $consoleParamName;
0368             if ( ($defaultValue = $methodInfo['parameterInfo'][$paramName]['default']) != null) {
0369                 $paramString .= '[=' . $defaultValue . ']';
0370             }
0371             $this->_response->appendContent($paramString . '', array('separator' => false));
0372         }
0373 
0374        $this->_response->appendContent(null, array('separator' => true));
0375        return $this;
0376     }
0377 
0378 }