2
0

Console.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * @see Zend_Loader
  24. */
  25. require_once 'Zend/Loader.php';
  26. /**
  27. * @see Zend_Tool_Framework_Client_Abstract
  28. */
  29. require_once 'Zend/Tool/Framework/Client/Abstract.php';
  30. /**
  31. * @see Zend_Tool_Framework_Client_Console_ArgumentParser
  32. */
  33. require_once 'Zend/Tool/Framework/Client/Console/ArgumentParser.php';
  34. /**
  35. * @see Zend_Tool_Framework_Client_Interactive_InputInterface
  36. */
  37. require_once 'Zend/Tool/Framework/Client/Interactive/InputInterface.php';
  38. /**
  39. * @see Zend_Tool_Framework_Client_Interactive_OutputInterface
  40. */
  41. require_once 'Zend/Tool/Framework/Client/Interactive/OutputInterface.php';
  42. /**
  43. * @see Zend_Tool_Framework_Client_Response_ContentDecorator_Separator
  44. */
  45. require_once 'Zend/Tool/Framework/Client/Response/ContentDecorator/Separator.php';
  46. /**
  47. * Zend_Tool_Framework_Client_Console - the CLI Client implementation for Zend_Tool_Framework
  48. *
  49. * @category Zend
  50. * @package Zend_Tool
  51. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  52. * @license http://framework.zend.com/license/new-bsd New BSD License
  53. */
  54. class Zend_Tool_Framework_Client_Console
  55. extends Zend_Tool_Framework_Client_Abstract
  56. implements Zend_Tool_Framework_Client_Interactive_InputInterface,
  57. Zend_Tool_Framework_Client_Interactive_OutputInterface
  58. {
  59. /**
  60. * @var Zend_Filter_Word_CamelCaseToDash
  61. */
  62. protected $_filterToClientNaming = null;
  63. /**
  64. * @var Zend_Filter_Word_DashToCamelCase
  65. */
  66. protected $_filterFromClientNaming = null;
  67. /**
  68. * main() - This is typically called from zf.php. This method is a
  69. * self contained main() function.
  70. *
  71. */
  72. public static function main()
  73. {
  74. ini_set('display_errors', true);
  75. $cliClient = new self();
  76. $cliClient->dispatch();
  77. }
  78. /**
  79. * getName() - return the name of the client, in this case 'console'
  80. *
  81. * @return string
  82. */
  83. public function getName()
  84. {
  85. return 'console';
  86. }
  87. /**
  88. * _init() - Tasks processed before the constructor, generally setting up objects to use
  89. *
  90. */
  91. protected function _preInit()
  92. {
  93. // support the changing of the current working directory, necessary for some providers
  94. if (isset($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY'])) {
  95. chdir($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY']);
  96. }
  97. // support setting the loader from the environment
  98. if (isset($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])) {
  99. if (class_exists($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])
  100. || Zend_Loader::loadClass($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])
  101. ) {
  102. $this->_registry->setLoader(new $_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS']);
  103. }
  104. }
  105. return;
  106. }
  107. /**
  108. * _preDispatch() - Tasks handed after initialization but before dispatching
  109. *
  110. */
  111. protected function _preDispatch()
  112. {
  113. $response = $this->_registry->getResponse();
  114. if (function_exists('posix_isatty')) {
  115. require_once 'Zend/Tool/Framework/Client/Console/ResponseDecorator/Colorizer.php';
  116. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer());
  117. }
  118. $response->addContentDecorator(new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator())
  119. ->setDefaultDecoratorOptions(array('separator' => true));
  120. $optParser = new Zend_Tool_Framework_Client_Console_ArgumentParser();
  121. $optParser->setArguments($_SERVER['argv'])
  122. ->setRegistry($this->_registry)
  123. ->parse();
  124. return;
  125. }
  126. /**
  127. * _postDispatch() - Tasks handled after dispatching
  128. *
  129. */
  130. protected function _postDispatch()
  131. {
  132. $request = $this->_registry->getRequest();
  133. $response = $this->_registry->getResponse();
  134. if ($response->isException()) {
  135. require_once 'Zend/Tool/Framework/Client/Console/HelpSystem.php';
  136. $helpSystem = new Zend_Tool_Framework_Client_Console_HelpSystem();
  137. $helpSystem->setRegistry($this->_registry)
  138. ->respondWithErrorMessage($response->getException()->getMessage(), $response->getException())
  139. ->respondWithSpecialtyAndParamHelp(
  140. $request->getProviderName(),
  141. $request->getActionName()
  142. );
  143. }
  144. echo PHP_EOL;
  145. return;
  146. }
  147. /**
  148. * handleInteractiveInputRequest() is required by the Interactive InputInterface
  149. *
  150. *
  151. * @param Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest
  152. * @return string
  153. */
  154. public function handleInteractiveInputRequest(Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest)
  155. {
  156. fwrite(STDOUT, $inputRequest->getContent() . PHP_EOL . 'zf> ');
  157. $inputContent = fgets(STDIN);
  158. return rtrim($inputContent); // remove the return from the end of the string
  159. }
  160. /**
  161. * handleInteractiveOutput() is required by the Interactive OutputInterface
  162. *
  163. * This allows us to display output immediately from providers, rather
  164. * than displaying it after the provider is done.
  165. *
  166. * @param string $output
  167. */
  168. public function handleInteractiveOutput($output)
  169. {
  170. echo $output;
  171. }
  172. /**
  173. * getMissingParameterPromptString()
  174. *
  175. * @param Zend_Tool_Framework_Provider_Interface $provider
  176. * @param Zend_Tool_Framework_Action_Interface $actionInterface
  177. * @param string $missingParameterName
  178. * @return string
  179. */
  180. public function getMissingParameterPromptString(Zend_Tool_Framework_Provider_Interface $provider, Zend_Tool_Framework_Action_Interface $actionInterface, $missingParameterName)
  181. {
  182. return 'Please provide a value for $' . $missingParameterName;
  183. }
  184. /**
  185. * convertToClientNaming()
  186. *
  187. * Convert words to client specific naming, in this case is lower, dash separated
  188. *
  189. * Filters are lazy-loaded.
  190. *
  191. * @param string $string
  192. * @return string
  193. */
  194. public function convertToClientNaming($string)
  195. {
  196. if (!$this->_filterToClientNaming) {
  197. require_once 'Zend/Filter.php';
  198. require_once 'Zend/Filter/Word/CamelCaseToDash.php';
  199. require_once 'Zend/Filter/StringToLower.php';
  200. $filter = new Zend_Filter();
  201. $filter->addFilter(new Zend_Filter_Word_CamelCaseToDash());
  202. $filter->addFilter(new Zend_Filter_StringToLower());
  203. $this->_filterToClientNaming = $filter;
  204. }
  205. return $this->_filterToClientNaming->filter($string);
  206. }
  207. /**
  208. * convertFromClientNaming()
  209. *
  210. * Convert words from client specific naming to code naming - camelcased
  211. *
  212. * Filters are lazy-loaded.
  213. *
  214. * @param string $string
  215. * @return string
  216. */
  217. public function convertFromClientNaming($string)
  218. {
  219. if (!$this->_filterFromClientNaming) {
  220. require_once 'Zend/Filter/Word/DashToCamelCase.php';
  221. $this->_filterFromClientNaming = new Zend_Filter_Word_DashToCamelCase();
  222. }
  223. return $this->_filterFromClientNaming->filter($string);
  224. }
  225. }