2
0

HelpSystem.php 14 KB

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