PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Tool/Framework/Client/Console/HelpSystem.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 373 lines | 214 code | 54 blank | 105 comment | 23 complexity | 20bf4c06af58531697c97866a5e925be MD5 | raw file
  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: HelpSystem.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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. /**
  147. * @see Zend_Version
  148. */
  149. require_once 'Zend/Version.php';
  150. $this->_response->appendContent('Zend Framework', array('color' => array('hiWhite'), 'separator' => false));
  151. $this->_response->appendContent(' Command Line Console Tool v' . Zend_Version::VERSION . '');
  152. return $this;
  153. }
  154. /**
  155. * _respondWithSystemInformation()
  156. *
  157. * @param string $providerNameFilter
  158. * @param string $actionNameFilter
  159. * @param bool $includeAllSpecialties
  160. * @return Zend_Tool_Framework_Client_Console_HelpSystem
  161. */
  162. protected function _respondWithSystemInformation($providerNameFilter = null, $actionNameFilter = null, $includeAllSpecialties = false)
  163. {
  164. $manifest = $this->_registry->getManifestRepository();
  165. $providerMetadatasSearch = array(
  166. 'type' => 'Tool',
  167. 'name' => 'providerName',
  168. 'clientName' => 'console'
  169. );
  170. if (is_string($providerNameFilter)) {
  171. $providerMetadatasSearch = array_merge($providerMetadatasSearch, array('providerName' => $providerNameFilter));
  172. }
  173. $actionMetadatasSearch = array(
  174. 'type' => 'Tool',
  175. 'name' => 'actionName',
  176. 'clientName' => 'console'
  177. );
  178. if (is_string($actionNameFilter)) {
  179. $actionMetadatasSearch = array_merge($actionMetadatasSearch, array('actionName' => $actionNameFilter));
  180. }
  181. // get the metadata's for the things to display
  182. $displayProviderMetadatas = $manifest->getMetadatas($providerMetadatasSearch);
  183. $displayActionMetadatas = $manifest->getMetadatas($actionMetadatasSearch);
  184. // create index of actionNames
  185. for ($i = 0; $i < count($displayActionMetadatas); $i++) {
  186. $displayActionNames[] = $displayActionMetadatas[$i]->getActionName();
  187. }
  188. foreach ($displayProviderMetadatas as $providerMetadata) {
  189. $providerNameDisplayed = false;
  190. $providerName = $providerMetadata->getProviderName();
  191. $providerSignature = $providerMetadata->getReference();
  192. foreach ($providerSignature->getActions() as $actionInfo) {
  193. $actionName = $actionInfo->getName();
  194. // check to see if this action name is valid
  195. if (($foundActionIndex = array_search($actionName, $displayActionNames)) === false) {
  196. continue;
  197. } else {
  198. $actionMetadata = $displayActionMetadatas[$foundActionIndex];
  199. }
  200. $specialtyMetadata = $manifest->getMetadata(array(
  201. 'type' => 'Tool',
  202. 'name' => 'specialtyName',
  203. 'providerName' => $providerName,
  204. 'specialtyName' => '_Global',
  205. 'clientName' => 'console'
  206. ));
  207. // lets do the main _Global action first
  208. $actionableGlobalLongParamMetadata = $manifest->getMetadata(array(
  209. 'type' => 'Tool',
  210. 'name' => 'actionableMethodLongParams',
  211. 'providerName' => $providerName,
  212. 'specialtyName' => '_Global',
  213. 'actionName' => $actionName,
  214. 'clientName' => 'console'
  215. ));
  216. if ($actionableGlobalLongParamMetadata) {
  217. if (!$providerNameDisplayed) {
  218. $this->_respondWithProviderName($providerMetadata);
  219. $providerNameDisplayed = true;
  220. }
  221. $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableGlobalLongParamMetadata);
  222. $actionIsGlobal = true;
  223. } else {
  224. $actionIsGlobal = false;
  225. }
  226. $actionableGlobalMetadatas = $manifest->getMetadatas(array(
  227. 'type' => 'Tool',
  228. 'name' => 'actionableMethodLongParams',
  229. 'providerName' => $providerName,
  230. 'actionName' => $actionName,
  231. 'clientName' => 'console'
  232. ));
  233. if (!$actionIsGlobal && count($actionableGlobalMetadatas) == 1) {
  234. $this->_response->appendContent('single special action/provider');
  235. }
  236. if ($includeAllSpecialties) {
  237. foreach ($providerSignature->getSpecialties() as $specialtyName) {
  238. if ($specialtyName == '_Global') {
  239. continue;
  240. }
  241. $specialtyMetadata = $manifest->getMetadata(array(
  242. 'type' => 'Tool',
  243. 'name' => 'specialtyName',
  244. 'providerName' => $providerMetadata->getProviderName(),
  245. 'specialtyName' => $specialtyName,
  246. 'clientName' => 'console'
  247. ));
  248. $actionableSpecialtyLongMetadata = $manifest->getMetadata(array(
  249. 'type' => 'Tool',
  250. 'name' => 'actionableMethodLongParams',
  251. 'providerName' => $providerMetadata->getProviderName(),
  252. 'specialtyName' => $specialtyName,
  253. 'actionName' => $actionName,
  254. 'clientName' => 'console'
  255. ));
  256. $this->_respondWithCommand($providerMetadata, $actionMetadata, $specialtyMetadata, $actionableSpecialtyLongMetadata);
  257. }
  258. }
  259. if (!$includeAllSpecialties && count($actionableGlobalMetadatas) > 1) {
  260. $this->_response->appendContent(' Note: There are specialties, use ', array('color' => 'yellow', 'separator' => false));
  261. $this->_response->appendContent(
  262. 'zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue() . '.?',
  263. array('color' => 'cyan', 'separator' => false)
  264. );
  265. $this->_response->appendContent(' to get specific help on them.', array('color' => 'yellow'));
  266. }
  267. }
  268. if ($providerNameDisplayed) {
  269. $this->_response->appendContent(null, array('separator' => true));
  270. }
  271. }
  272. return $this;
  273. }
  274. /**
  275. * _respondWithProviderName()
  276. *
  277. * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
  278. * @return Zend_Tool_Framework_Client_Console_HelpSystem
  279. */
  280. protected function _respondWithProviderName(Zend_Tool_Framework_Metadata_Tool $providerMetadata)
  281. {
  282. $this->_response->appendContent(' ' . $providerMetadata->getProviderName());
  283. return $this;
  284. }
  285. /**
  286. * _respondWithCommand()
  287. *
  288. * @param Zend_Tool_Framework_Metadata_Tool $providerMetadata
  289. * @param Zend_Tool_Framework_Metadata_Tool $actionMetadata
  290. * @param Zend_Tool_Framework_Metadata_Tool $specialtyMetadata
  291. * @param Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata
  292. * @return Zend_Tool_Framework_Client_Console_HelpSystem
  293. */
  294. protected function _respondWithCommand(
  295. Zend_Tool_Framework_Metadata_Tool $providerMetadata,
  296. Zend_Tool_Framework_Metadata_Tool $actionMetadata,
  297. Zend_Tool_Framework_Metadata_Tool $specialtyMetadata,
  298. Zend_Tool_Framework_Metadata_Tool $parameterLongMetadata)//,
  299. //Zend_Tool_Framework_Metadata_Tool $parameterShortMetadata)
  300. {
  301. $this->_response->appendContent(
  302. ' zf ' . $actionMetadata->getValue() . ' ' . $providerMetadata->getValue(),
  303. array('color' => 'cyan', 'separator' => false)
  304. );
  305. if ($specialtyMetadata->getSpecialtyName() != '_Global') {
  306. $this->_response->appendContent('.' . $specialtyMetadata->getValue(), array('color' => 'cyan', 'separator' => false));
  307. }
  308. foreach ($parameterLongMetadata->getValue() as $paramName => $consoleParamName) {
  309. $methodInfo = $parameterLongMetadata->getReference();
  310. $paramString = ' ' . $consoleParamName;
  311. if ( ($defaultValue = $methodInfo['parameterInfo'][$paramName]['default']) != null) {
  312. $paramString .= '[=' . $defaultValue . ']';
  313. }
  314. $this->_response->appendContent($paramString . '', array('separator' => false));
  315. }
  316. $this->_response->appendContent(null, array('separator' => true));
  317. return $this;
  318. }
  319. }