Console.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 string
  61. */
  62. protected $_configOptions = null;
  63. /**
  64. * @var Zend_Filter_Word_CamelCaseToDash
  65. */
  66. protected $_filterToClientNaming = null;
  67. /**
  68. * @var Zend_Filter_Word_DashToCamelCase
  69. */
  70. protected $_filterFromClientNaming = null;
  71. /**
  72. * main() - This is typically called from zf.php. This method is a
  73. * self contained main() function.
  74. *
  75. */
  76. public static function main($options = array())
  77. {
  78. ini_set('display_errors', true);
  79. $cliClient = new self($options);
  80. $cliClient->dispatch();
  81. }
  82. public function setConfigOptions($configOptions)
  83. {
  84. $this->_configOptions = $configOptions;
  85. return $this;
  86. }
  87. /**
  88. * getName() - return the name of the client, in this case 'console'
  89. *
  90. * @return string
  91. */
  92. public function getName()
  93. {
  94. return 'console';
  95. }
  96. /**
  97. * _init() - Tasks processed before the constructor, generally setting up objects to use
  98. *
  99. */
  100. protected function _preInit()
  101. {
  102. $config = $this->_registry->getConfig();
  103. if ($this->_configOptions != null) {
  104. $config->setOptions($this->_configOptions);
  105. }
  106. // support the changing of the current working directory, necessary for some providers
  107. if (isset($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY'])) {
  108. chdir($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY']);
  109. }
  110. // support setting the loader from the environment
  111. if (isset($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])) {
  112. if (class_exists($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])
  113. || Zend_Loader::loadClass($_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS'])
  114. ) {
  115. $this->_registry->setLoader(new $_ENV['ZEND_TOOL_FRAMEWORK_LOADER_CLASS']);
  116. }
  117. }
  118. return;
  119. }
  120. /**
  121. * _preDispatch() - Tasks handed after initialization but before dispatching
  122. *
  123. */
  124. protected function _preDispatch()
  125. {
  126. $response = $this->_registry->getResponse();
  127. if (function_exists('posix_isatty')) {
  128. require_once 'Zend/Tool/Framework/Client/Console/ResponseDecorator/Colorizer.php';
  129. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer());
  130. }
  131. $response->addContentDecorator(new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator())
  132. ->setDefaultDecoratorOptions(array('separator' => true));
  133. $optParser = new Zend_Tool_Framework_Client_Console_ArgumentParser();
  134. $optParser->setArguments($_SERVER['argv'])
  135. ->setRegistry($this->_registry)
  136. ->parse();
  137. return;
  138. }
  139. /**
  140. * _postDispatch() - Tasks handled after dispatching
  141. *
  142. */
  143. protected function _postDispatch()
  144. {
  145. $request = $this->_registry->getRequest();
  146. $response = $this->_registry->getResponse();
  147. if ($response->isException()) {
  148. require_once 'Zend/Tool/Framework/Client/Console/HelpSystem.php';
  149. $helpSystem = new Zend_Tool_Framework_Client_Console_HelpSystem();
  150. $helpSystem->setRegistry($this->_registry)
  151. ->respondWithErrorMessage($response->getException()->getMessage(), $response->getException())
  152. ->respondWithSpecialtyAndParamHelp(
  153. $request->getProviderName(),
  154. $request->getActionName()
  155. );
  156. }
  157. echo PHP_EOL;
  158. return;
  159. }
  160. /**
  161. * handleInteractiveInputRequest() is required by the Interactive InputInterface
  162. *
  163. *
  164. * @param Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest
  165. * @return string
  166. */
  167. public function handleInteractiveInputRequest(Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest)
  168. {
  169. fwrite(STDOUT, $inputRequest->getContent() . PHP_EOL . 'zf> ');
  170. $inputContent = fgets(STDIN);
  171. return rtrim($inputContent); // remove the return from the end of the string
  172. }
  173. /**
  174. * handleInteractiveOutput() is required by the Interactive OutputInterface
  175. *
  176. * This allows us to display output immediately from providers, rather
  177. * than displaying it after the provider is done.
  178. *
  179. * @param string $output
  180. */
  181. public function handleInteractiveOutput($output)
  182. {
  183. echo $output;
  184. }
  185. /**
  186. * getMissingParameterPromptString()
  187. *
  188. * @param Zend_Tool_Framework_Provider_Interface $provider
  189. * @param Zend_Tool_Framework_Action_Interface $actionInterface
  190. * @param string $missingParameterName
  191. * @return string
  192. */
  193. public function getMissingParameterPromptString(Zend_Tool_Framework_Provider_Interface $provider, Zend_Tool_Framework_Action_Interface $actionInterface, $missingParameterName)
  194. {
  195. return 'Please provide a value for $' . $missingParameterName;
  196. }
  197. /**
  198. * convertToClientNaming()
  199. *
  200. * Convert words to client specific naming, in this case is lower, dash separated
  201. *
  202. * Filters are lazy-loaded.
  203. *
  204. * @param string $string
  205. * @return string
  206. */
  207. public function convertToClientNaming($string)
  208. {
  209. if (!$this->_filterToClientNaming) {
  210. require_once 'Zend/Filter.php';
  211. require_once 'Zend/Filter/Word/CamelCaseToDash.php';
  212. require_once 'Zend/Filter/StringToLower.php';
  213. $filter = new Zend_Filter();
  214. $filter->addFilter(new Zend_Filter_Word_CamelCaseToDash());
  215. $filter->addFilter(new Zend_Filter_StringToLower());
  216. $this->_filterToClientNaming = $filter;
  217. }
  218. return $this->_filterToClientNaming->filter($string);
  219. }
  220. /**
  221. * convertFromClientNaming()
  222. *
  223. * Convert words from client specific naming to code naming - camelcased
  224. *
  225. * Filters are lazy-loaded.
  226. *
  227. * @param string $string
  228. * @return string
  229. */
  230. public function convertFromClientNaming($string)
  231. {
  232. if (!$this->_filterFromClientNaming) {
  233. require_once 'Zend/Filter/Word/DashToCamelCase.php';
  234. $this->_filterFromClientNaming = new Zend_Filter_Word_DashToCamelCase();
  235. }
  236. return $this->_filterFromClientNaming->filter($string);
  237. }
  238. }