2
0

HelpSystem.php 15 KB

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