web/lib/Zend/Tool/Framework/Client/Console/HelpSystem.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: HelpSystem.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @category   Zend
       
    25  * @package    Zend_Tool
       
    26  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    27  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    28  */
       
    29 class Zend_Tool_Framework_Client_Console_HelpSystem
       
    30 {
       
    31 
       
    32     /**
       
    33      * @var Zend_Tool_Framework_Registry_Interface
       
    34      */
       
    35     protected $_registry = null;
       
    36 
       
    37     /**
       
    38      * @var Zend_Tool_Framework_Client_Response
       
    39      */
       
    40     protected $_response = null;
       
    41 
       
    42     /**
       
    43      * setRegistry()
       
    44      *
       
    45      * @param Zend_Tool_Framework_Registry_Interface $registry
       
    46      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
    47      */
       
    48     public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
       
    49     {
       
    50         $this->_registry = $registry;
       
    51         $this->_response = $registry->getResponse();
       
    52         return $this;
       
    53     }
       
    54 
       
    55     /**
       
    56      * respondWithErrorMessage()
       
    57      *
       
    58      * @param string $errorMessage
       
    59      * @param Exception $exception
       
    60      */
       
    61     public function respondWithErrorMessage($errorMessage, Exception $exception = null)
       
    62     {
       
    63         // break apart the message into wrapped chunks
       
    64         $errorMessages = explode(PHP_EOL, wordwrap($errorMessage, 70, PHP_EOL, false));
       
    65 
       
    66         $text = 'An Error Has Occurred';
       
    67         $this->_response->appendContent($text, array('color' => array('hiWhite', 'bgRed'), 'aligncenter' => true));
       
    68         $this->_response->appendContent($errorMessage, array('indention' => 1, 'blockize' => 72, 'color' => array('white', 'bgRed')));
       
    69 
       
    70         if ($exception && $this->_registry->getRequest()->isDebug()) {
       
    71             $this->_response->appendContent($exception->getTraceAsString());
       
    72         }
       
    73 
       
    74         $this->_response->appendContent(null, array('separator' => true));
       
    75         return $this;
       
    76     }
       
    77 
       
    78     /**
       
    79      * respondWithGeneralHelp()
       
    80      *
       
    81      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
    82      */
       
    83     public function respondWithGeneralHelp()
       
    84     {
       
    85         $this->_respondWithHeader();
       
    86 
       
    87         $noSeparator = array('separator' => false);
       
    88 
       
    89         $this->_response->appendContent('Usage:', array('color' => 'green'))
       
    90             ->appendContent('    ', $noSeparator)
       
    91             ->appendContent('zf', array_merge(array('color' => 'cyan'), $noSeparator))
       
    92             ->appendContent(' [--global-opts]', $noSeparator)
       
    93             ->appendContent(' action-name', array_merge(array('color' => 'cyan'), $noSeparator))
       
    94             ->appendContent(' [--action-opts]', $noSeparator)
       
    95             ->appendContent(' provider-name', array_merge(array('color' => 'cyan'), $noSeparator))
       
    96             ->appendContent(' [--provider-opts]', $noSeparator)
       
    97             ->appendContent(' [provider parameters ...]')
       
    98             ->appendContent('    Note: You may use "?" in any place of the above usage string to ask for more specific help information.', array('color'=>'yellow'))
       
    99             ->appendContent('    Example: "zf ? version" will list all available actions for the version provider.', array('color'=>'yellow', 'separator' => 2))
       
   100             ->appendContent('Providers and their actions:', array('color' => 'green'));
       
   101 
       
   102         $this->_respondWithSystemInformation();
       
   103         return $this;
       
   104     }
       
   105 
       
   106     /**
       
   107      * respondWithActionHelp()
       
   108      *
       
   109      * @param string $actionName
       
   110      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   111      */
       
   112     public function respondWithActionHelp($actionName)
       
   113     {
       
   114         $this->_respondWithHeader();
       
   115         $this->_response->appendContent('Providers that support the action "' . $actionName . '"', array('color' => 'green'));
       
   116         $this->_respondWithSystemInformation(null, $actionName);
       
   117         return $this;
       
   118     }
       
   119 
       
   120     /**
       
   121      * respondWithSpecialtyAndParamHelp()
       
   122      *
       
   123      * @param string $providerName
       
   124      * @param string $actionName
       
   125      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   126      */
       
   127     public function respondWithSpecialtyAndParamHelp($providerName, $actionName)
       
   128     {
       
   129         $this->_respondWithHeader();
       
   130         $this->_response->appendContent(
       
   131             'Details for action "' . $actionName . '" and provider "' . $providerName . '"',
       
   132             array('color' => 'green')
       
   133             );
       
   134         $this->_respondWithSystemInformation($providerName, $actionName, true);
       
   135         return $this;
       
   136     }
       
   137 
       
   138     /**
       
   139      * respondWithProviderHelp()
       
   140      *
       
   141      * @param string $providerName
       
   142      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   143      */
       
   144     public function respondWithProviderHelp($providerName)
       
   145     {
       
   146         $this->_respondWithHeader();
       
   147         $this->_response->appendContent('Actions supported by provider "' . $providerName . '"', array('color' => 'green'));
       
   148         $this->_respondWithSystemInformation($providerName);
       
   149         return $this;
       
   150     }
       
   151 
       
   152     /**
       
   153      * _respondWithHeader()
       
   154      *
       
   155      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   156      */
       
   157     protected function _respondWithHeader()
       
   158     {
       
   159         /**
       
   160          * @see Zend_Version
       
   161          */
       
   162         require_once 'Zend/Version.php';
       
   163         $this->_response->appendContent('Zend Framework', array('color' => array('hiWhite'), 'separator' => false));
       
   164         $this->_response->appendContent(' Command Line Console Tool v' . Zend_Version::VERSION . '');
       
   165         return $this;
       
   166     }
       
   167 
       
   168     /**
       
   169      * _respondWithSystemInformation()
       
   170      *
       
   171      * @param string $providerNameFilter
       
   172      * @param string $actionNameFilter
       
   173      * @param bool $includeAllSpecialties
       
   174      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   175      */
       
   176     protected function _respondWithSystemInformation($providerNameFilter = null, $actionNameFilter = null, $includeAllSpecialties = false)
       
   177     {
       
   178         $manifest = $this->_registry->getManifestRepository();
       
   179 
       
   180         $providerMetadatasSearch = array(
       
   181             'type'       => 'Tool',
       
   182             'name'       => 'providerName',
       
   183             'clientName' => 'console'
       
   184             );
       
   185 
       
   186         if (is_string($providerNameFilter)) {
       
   187             $providerMetadatasSearch = array_merge($providerMetadatasSearch, array('providerName' => $providerNameFilter));
       
   188         }
       
   189 
       
   190         $actionMetadatasSearch = array(
       
   191             'type'       => 'Tool',
       
   192             'name'       => 'actionName',
       
   193             'clientName' => 'console'
       
   194             );
       
   195 
       
   196         if (is_string($actionNameFilter)) {
       
   197             $actionMetadatasSearch = array_merge($actionMetadatasSearch, array('actionName' => $actionNameFilter));
       
   198         }
       
   199 
       
   200         // get the metadata's for the things to display
       
   201         $displayProviderMetadatas = $manifest->getMetadatas($providerMetadatasSearch);
       
   202         $displayActionMetadatas = $manifest->getMetadatas($actionMetadatasSearch);
       
   203 
       
   204         // create index of actionNames
       
   205         for ($i = 0; $i < count($displayActionMetadatas); $i++) {
       
   206             $displayActionNames[] = $displayActionMetadatas[$i]->getActionName();
       
   207         }
       
   208 
       
   209         foreach ($displayProviderMetadatas as $providerMetadata) {
       
   210 
       
   211             $providerNameDisplayed = false;
       
   212 
       
   213             $providerName = $providerMetadata->getProviderName();
       
   214             $providerSignature = $providerMetadata->getReference();
       
   215 
       
   216             foreach ($providerSignature->getActions() as $actionInfo) {
       
   217 
       
   218                 $actionName = $actionInfo->getName();
       
   219 
       
   220                 // check to see if this action name is valid
       
   221                 if (($foundActionIndex = array_search($actionName, $displayActionNames)) === false) {
       
   222                     continue;
       
   223                 } else {
       
   224                     $actionMetadata = $displayActionMetadatas[$foundActionIndex];
       
   225                 }
       
   226 
       
   227                 $specialtyMetadata = $manifest->getMetadata(array(
       
   228                     'type'          => 'Tool',
       
   229                     'name'          => 'specialtyName',
       
   230                     'providerName'  => $providerName,
       
   231                     'specialtyName' => '_Global',
       
   232                     'clientName'    => 'console'
       
   233                     ));
       
   234 
       
   235                 // lets do the main _Global action first
       
   236                 $actionableGlobalLongParamMetadata = $manifest->getMetadata(array(
       
   237                     'type'          => 'Tool',
       
   238                     'name'          => 'actionableMethodLongParams',
       
   239                     'providerName'  => $providerName,
       
   240                     'specialtyName' => '_Global',
       
   241                     'actionName'    => $actionName,
       
   242                     'clientName'    => 'console'
       
   243                     ));
       
   244 
       
   245                 $actionableGlobalMetadatas = $manifest->getMetadatas(array(
       
   246                     'type'          => 'Tool',
       
   247                     'name'          => 'actionableMethodLongParams',
       
   248                     'providerName'  => $providerName,
       
   249                     'actionName'    => $actionName,
       
   250                     'clientName'    => 'console'
       
   251                     ));
       
   252 
       
   253                 if ($actionableGlobalLongParamMetadata) {
       
   254 
       
   255                     if (!$providerNameDisplayed) {
       
   256                         $this->_respondWithProviderName($providerMetadata);
       
   257                         $providerNameDisplayed = true;
       
   258                     }
       
   259 
       
   260                     $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableGlobalLongParamMetadata);
       
   261 
       
   262                     $actionIsGlobal = true;
       
   263                 } else {
       
   264                     $actionIsGlobal = false;
       
   265                 }
       
   266 
       
   267                 // check for providers without a _Global action
       
   268                 $isSingleSpecialProviderAction = false;
       
   269                 if (!$actionIsGlobal && count($actionableGlobalMetadatas) == 1) {
       
   270                     $isSingleSpecialProviderAction = true;
       
   271                     $this->_respondWithProviderName($providerMetadata);
       
   272                     $providerNameDisplayed = true;
       
   273                 }
       
   274                 
       
   275                 if ($includeAllSpecialties || $isSingleSpecialProviderAction) {
       
   276 
       
   277                     foreach ($providerSignature->getSpecialties() as $specialtyName) {
       
   278 
       
   279                         if ($specialtyName == '_Global') {
       
   280                             continue;
       
   281                         }
       
   282 
       
   283                         $specialtyMetadata = $manifest->getMetadata(array(
       
   284                             'type'          => 'Tool',
       
   285                             'name'          => 'specialtyName',
       
   286                             'providerName'  => $providerMetadata->getProviderName(),
       
   287                             'specialtyName' => $specialtyName,
       
   288                             'clientName'    => 'console'
       
   289                             ));
       
   290 
       
   291                         $actionableSpecialtyLongMetadata = $manifest->getMetadata(array(
       
   292                             'type'          => 'Tool',
       
   293                             'name'          => 'actionableMethodLongParams',
       
   294                             'providerName'  => $providerMetadata->getProviderName(),
       
   295                             'specialtyName' => $specialtyName,
       
   296                             'actionName'    => $actionName,
       
   297                             'clientName'    => 'console'
       
   298                             ));
       
   299 
       
   300                         if($actionableSpecialtyLongMetadata) {
       
   301                             $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableSpecialtyLongMetadata);
       
   302                         }
       
   303 
       
   304                     }
       
   305                 }
       
   306                 
       
   307                 // reset the special flag for single provider action with specialty
       
   308                 $isSingleSpecialProviderAction = false;
       
   309 
       
   310                 if (!$includeAllSpecialties && count($actionableGlobalMetadatas) > 1) {
       
   311                     $this->_response->appendContent('    Note: There are specialties, use ', array('color' => 'yellow', 'separator' => false));
       
   312                     $this->_response->appendContent(
       
   313                         'zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue() . '.?',
       
   314                         array('color' => 'cyan', 'separator' => false)
       
   315                         );
       
   316                     $this->_response->appendContent(' to get specific help on them.', array('color' => 'yellow'));
       
   317                 }
       
   318 
       
   319             }
       
   320 
       
   321             if ($providerNameDisplayed) {
       
   322                 $this->_response->appendContent(null, array('separator' => true));
       
   323             }
       
   324         }
       
   325         return $this;
       
   326     }
       
   327 
       
   328     /**
       
   329      * _respondWithProviderName()
       
   330      *
       
   331      * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
       
   332      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   333      */
       
   334     protected function _respondWithProviderName(Zend_Tool_Framework_Metadata_Tool $providerMetadata)
       
   335     {
       
   336         $this->_response->appendContent('  ' . $providerMetadata->getProviderName());
       
   337         return $this;
       
   338     }
       
   339 
       
   340     /**
       
   341      * _respondWithCommand()
       
   342      *
       
   343      * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
       
   344      * @param Zend_Tool_Framework_Metadata_Tool $actionMetadata
       
   345      * @param Zend_Tool_Framework_Metadata_Tool $specialtyMetadata
       
   346      * @param Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata
       
   347      * @return Zend_Tool_Framework_Client_Console_HelpSystem
       
   348      */
       
   349     protected function _respondWithCommand(
       
   350         Zend_Tool_Framework_Metadata_Tool $providerMetadata,
       
   351         Zend_Tool_Framework_Metadata_Tool $actionMetadata,
       
   352         Zend_Tool_Framework_Metadata_Tool $specialtyMetadata,
       
   353         Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata)//,
       
   354         //Zend_Tool_Framework_Metadata_Tool $parameterShortMetadata)
       
   355     {
       
   356         $this->_response->appendContent(
       
   357             '    zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue(),
       
   358             array('color' => 'cyan', 'separator' => false)
       
   359             );
       
   360 
       
   361         if ($specialtyMetadata->getSpecialtyName() != '_Global') {
       
   362             $this->_response->appendContent('.' . $specialtyMetadata->getValue(), array('color' => 'cyan', 'separator' => false));
       
   363         }
       
   364 
       
   365         foreach ($parameterLongMetadata->getValue() as $paramName => $consoleParamName) {
       
   366             $methodInfo = $parameterLongMetadata->getReference();
       
   367             $paramString = ' ' . $consoleParamName;
       
   368             if ( ($defaultValue = $methodInfo['parameterInfo'][$paramName]['default']) != null) {
       
   369                 $paramString .= '[=' . $defaultValue . ']';
       
   370             }
       
   371             $this->_response->appendContent($paramString . '', array('separator' => false));
       
   372         }
       
   373 
       
   374        $this->_response->appendContent(null, array('separator' => true));
       
   375        return $this;
       
   376     }
       
   377 
       
   378 }