Console.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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']) && Zend_Loader::loadClass($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])) {
  99. $this->_registry->setLoader(new $_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS']);
  100. }
  101. return;
  102. }
  103. /**
  104. * _preDispatch() - Tasks handed after initialization but before dispatching
  105. *
  106. */
  107. protected function _preDispatch()
  108. {
  109. $response = $this->_registry->getResponse();
  110. if (function_exists('posix_isatty')) {
  111. require_once 'Zend/Tool/Framework/Client/Console/ResponseDecorator/Colorizer.php';
  112. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer());
  113. }
  114. $response->addContentDecorator(new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator())
  115. ->setDefaultDecoratorOptions(array('separator' => true));
  116. $optParser = new Zend_Tool_Framework_Client_Console_ArgumentParser();
  117. $optParser->setArguments($_SERVER['argv'])
  118. ->setRegistry($this->_registry)
  119. ->parse();
  120. return;
  121. }
  122. /**
  123. * _postDispatch() - Tasks handled after dispatching
  124. *
  125. */
  126. protected function _postDispatch()
  127. {
  128. $request = $this->_registry->getRequest();
  129. $response = $this->_registry->getResponse();
  130. if ($response->isException()) {
  131. require_once 'Zend/Tool/Framework/Client/Console/HelpSystem.php';
  132. $helpSystem = new Zend_Tool_Framework_Client_Console_HelpSystem();
  133. $helpSystem->setRegistry($this->_registry)
  134. ->respondWithErrorMessage($response->getException()->getMessage())
  135. ->respondWithSpecialtyAndParamHelp(
  136. $request->getProviderName(),
  137. $request->getActionName()
  138. );
  139. }
  140. echo PHP_EOL;
  141. return;
  142. }
  143. /**
  144. * handleInteractiveInputRequest() is required by the Interactive InputInterface
  145. *
  146. *
  147. * @param Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest
  148. * @return string
  149. */
  150. public function handleInteractiveInputRequest(Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest)
  151. {
  152. fwrite(STDOUT, $inputRequest->getContent() . PHP_EOL . 'zf> ');
  153. $inputContent = fgets(STDIN);
  154. return rtrim($inputContent); // remove the return from the end of the string
  155. }
  156. /**
  157. * handleInteractiveOutput() is required by the Interactive OutputInterface
  158. *
  159. * This allows us to display output immediately from providers, rather
  160. * than displaying it after the provider is done.
  161. *
  162. * @param string $output
  163. */
  164. public function handleInteractiveOutput($output)
  165. {
  166. echo $output;
  167. }
  168. /**
  169. * getMissingParameterPromptString()
  170. *
  171. * @param Zend_Tool_Framework_Provider_Interface $provider
  172. * @param Zend_Tool_Framework_Action_Interface $actionInterface
  173. * @param string $missingParameterName
  174. * @return string
  175. */
  176. public function getMissingParameterPromptString(Zend_Tool_Framework_Provider_Interface $provider, Zend_Tool_Framework_Action_Interface $actionInterface, $missingParameterName)
  177. {
  178. return 'Please provide a value for $' . $missingParameterName;
  179. }
  180. /**
  181. * convertToClientNaming()
  182. *
  183. * Convert words to client specific naming, in this case is lower, dash separated
  184. *
  185. * Filters are lazy-loaded.
  186. *
  187. * @param string $string
  188. * @return string
  189. */
  190. public function convertToClientNaming($string)
  191. {
  192. if (!$this->_filterToClientNaming) {
  193. require_once 'Zend/Filter.php';
  194. require_once 'Zend/Filter/Word/CamelCaseToDash.php';
  195. require_once 'Zend/Filter/StringToLower.php';
  196. $filter = new Zend_Filter();
  197. $filter->addFilter(new Zend_Filter_Word_CamelCaseToDash());
  198. $filter->addFilter(new Zend_Filter_StringToLower());
  199. $this->_filterToClientNaming = $filter;
  200. }
  201. return $this->_filterToClientNaming->filter($string);
  202. }
  203. /**
  204. * convertFromClientNaming()
  205. *
  206. * Convert words from client specific naming to code naming - camelcased
  207. *
  208. * Filters are lazy-loaded.
  209. *
  210. * @param string $string
  211. * @return string
  212. */
  213. public function convertFromClientNaming($string)
  214. {
  215. if (!$this->_filterFromClientNaming) {
  216. require_once 'Zend/Filter/Word/DashToCamelCase.php';
  217. $this->_filterFromClientNaming = new Zend_Filter_Word_DashToCamelCase();
  218. }
  219. return $this->_filterFromClientNaming->filter($string);
  220. }
  221. }