HelpSystem.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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-2014 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-2014 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. $actionableGlobalMetadatas = $manifest->getMetadatas(array(
  214. 'type' => 'Tool',
  215. 'name' => 'actionableMethodLongParams',
  216. 'providerName' => $providerName,
  217. 'actionName' => $actionName,
  218. 'clientName' => 'console'
  219. ));
  220. if ($actionableGlobalLongParamMetadata) {
  221. if (!$providerNameDisplayed) {
  222. $this->_respondWithProviderName($providerMetadata);
  223. $providerNameDisplayed = true;
  224. }
  225. $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableGlobalLongParamMetadata);
  226. $actionIsGlobal = true;
  227. } else {
  228. $actionIsGlobal = false;
  229. }
  230. // check for providers without a _Global action
  231. $isSingleSpecialProviderAction = false;
  232. if (!$actionIsGlobal && count($actionableGlobalMetadatas) == 1) {
  233. $isSingleSpecialProviderAction = true;
  234. $this->_respondWithProviderName($providerMetadata);
  235. $providerNameDisplayed = true;
  236. }
  237. if ($includeAllSpecialties || $isSingleSpecialProviderAction) {
  238. foreach ($providerSignature->getSpecialties() as $specialtyName) {
  239. if ($specialtyName == '_Global') {
  240. continue;
  241. }
  242. $specialtyMetadata = $manifest->getMetadata(array(
  243. 'type' => 'Tool',
  244. 'name' => 'specialtyName',
  245. 'providerName' => $providerMetadata->getProviderName(),
  246. 'specialtyName' => $specialtyName,
  247. 'clientName' => 'console'
  248. ));
  249. $actionableSpecialtyLongMetadata = $manifest->getMetadata(array(
  250. 'type' => 'Tool',
  251. 'name' => 'actionableMethodLongParams',
  252. 'providerName' => $providerMetadata->getProviderName(),
  253. 'specialtyName' => $specialtyName,
  254. 'actionName' => $actionName,
  255. 'clientName' => 'console'
  256. ));
  257. if($actionableSpecialtyLongMetadata) {
  258. $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableSpecialtyLongMetadata);
  259. }
  260. }
  261. }
  262. // reset the special flag for single provider action with specialty
  263. $isSingleSpecialProviderAction = false;
  264. if (!$includeAllSpecialties && count($actionableGlobalMetadatas) > 1) {
  265. $this->_response->appendContent(' Note: There are specialties, use ', array('color' => 'yellow', 'separator' => false));
  266. $this->_response->appendContent(
  267. 'zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue() . '.?',
  268. array('color' => 'cyan', 'separator' => false)
  269. );
  270. $this->_response->appendContent(' to get specific help on them.', array('color' => 'yellow'));
  271. }
  272. }
  273. if ($providerNameDisplayed) {
  274. $this->_response->appendContent(null, array('separator' => true));
  275. }
  276. }
  277. return $this;
  278. }
  279. /**
  280. * _respondWithProviderName()
  281. *
  282. * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
  283. * @return Zend_Tool_Framework_Client_Console_HelpSystem
  284. */
  285. protected function _respondWithProviderName(Zend_Tool_Framework_Metadata_Tool $providerMetadata)
  286. {
  287. $this->_response->appendContent(' ' . $providerMetadata->getProviderName());
  288. return $this;
  289. }
  290. /**
  291. * _respondWithCommand()
  292. *
  293. * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
  294. * @param Zend_Tool_Framework_Metadata_Tool $actionMetadata
  295. * @param Zend_Tool_Framework_Metadata_Tool $specialtyMetadata
  296. * @param Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata
  297. * @return Zend_Tool_Framework_Client_Console_HelpSystem
  298. */
  299. protected function _respondWithCommand(
  300. Zend_Tool_Framework_Metadata_Tool $providerMetadata,
  301. Zend_Tool_Framework_Metadata_Tool $actionMetadata,
  302. Zend_Tool_Framework_Metadata_Tool $specialtyMetadata,
  303. Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata)//,
  304. //Zend_Tool_Framework_Metadata_Tool $parameterShortMetadata)
  305. {
  306. $this->_response->appendContent(
  307. ' zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue(),
  308. array('color' => 'cyan', 'separator' => false)
  309. );
  310. if ($specialtyMetadata->getSpecialtyName() != '_Global') {
  311. $this->_response->appendContent('.' . $specialtyMetadata->getValue(), array('color' => 'cyan', 'separator' => false));
  312. }
  313. foreach ($parameterLongMetadata->getValue() as $paramName => $consoleParamName) {
  314. $methodInfo = $parameterLongMetadata->getReference();
  315. $paramString = ' ' . $consoleParamName;
  316. if ( ($defaultValue = $methodInfo['parameterInfo'][$paramName]['default']) != null) {
  317. $paramString .= '[=' . $defaultValue . ']';
  318. }
  319. $this->_response->appendContent($paramString . '', array('separator' => false));
  320. }
  321. $this->_response->appendContent(null, array('separator' => true));
  322. return $this;
  323. }
  324. }