HelpSystem.php 14 KB

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