PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Tool/Framework/Client/Console.php

https://github.com/luisbraschi/ursp
PHP | 307 lines | 133 code | 42 blank | 132 comment | 13 complexity | 49e39fcd262610089268550c2325f5c4 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Console.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Tool_Framework_Client_Abstract
  24. */
  25. #require_once 'Zend/Tool/Framework/Client/Abstract.php';
  26. /**
  27. * @see Zend_Tool_Framework_Client_Interactive_InputInterface
  28. */
  29. #require_once 'Zend/Tool/Framework/Client/Interactive/InputInterface.php';
  30. /**
  31. * @see Zend_Tool_Framework_Client_Interactive_OutputInterface
  32. */
  33. #require_once 'Zend/Tool/Framework/Client/Interactive/OutputInterface.php';
  34. /**
  35. * Zend_Tool_Framework_Client_Console - the CLI Client implementation for Zend_Tool_Framework
  36. *
  37. * @category Zend
  38. * @package Zend_Tool
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Tool_Framework_Client_Console
  43. extends Zend_Tool_Framework_Client_Abstract
  44. implements Zend_Tool_Framework_Client_Interactive_InputInterface,
  45. Zend_Tool_Framework_Client_Interactive_OutputInterface
  46. {
  47. /**
  48. * @var array
  49. */
  50. protected $_configOptions = null;
  51. /**
  52. * @var array
  53. */
  54. protected $_storageOptions = null;
  55. /**
  56. * @var Zend_Filter_Word_CamelCaseToDash
  57. */
  58. protected $_filterToClientNaming = null;
  59. /**
  60. * @var Zend_Filter_Word_DashToCamelCase
  61. */
  62. protected $_filterFromClientNaming = null;
  63. /**
  64. * @var array
  65. */
  66. protected $_classesToLoad = array();
  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($options = array())
  73. {
  74. $cliClient = new self($options);
  75. $cliClient->dispatch();
  76. }
  77. /**
  78. * getName() - return the name of the client, in this case 'console'
  79. *
  80. * @return string
  81. */
  82. public function getName()
  83. {
  84. return 'console';
  85. }
  86. /**
  87. * setConfigOptions()
  88. *
  89. * @param $configOptions
  90. */
  91. public function setConfigOptions($configOptions)
  92. {
  93. $this->_configOptions = $configOptions;
  94. return $this;
  95. }
  96. /**
  97. * setStorageOptions()
  98. *
  99. * @param $storageOptions
  100. */
  101. public function setStorageOptions($storageOptions)
  102. {
  103. $this->_storageOptions = $storageOptions;
  104. return $this;
  105. }
  106. public function setClassesToLoad($classesToLoad)
  107. {
  108. $this->_classesToLoad = $classesToLoad;
  109. return $this;
  110. }
  111. /**
  112. * _init() - Tasks processed before the constructor, generally setting up objects to use
  113. *
  114. */
  115. protected function _preInit()
  116. {
  117. $config = $this->_registry->getConfig();
  118. if ($this->_configOptions != null) {
  119. $config->setOptions($this->_configOptions);
  120. }
  121. $storage = $this->_registry->getStorage();
  122. if ($this->_storageOptions != null && isset($this->_storageOptions['directory'])) {
  123. $storage->setAdapter(
  124. new Zend_Tool_Framework_Client_Storage_Directory($this->_storageOptions['directory'])
  125. );
  126. }
  127. // which classes are essential to initializing Zend_Tool_Framework_Client_Console
  128. $classesToLoad = array(
  129. 'Zend_Tool_Framework_Client_Console_Manifest',
  130. 'Zend_Tool_Framework_System_Manifest'
  131. );
  132. if ($this->_classesToLoad) {
  133. if (is_string($this->_classesToLoad)) {
  134. $classesToLoad[] = $this->_classesToLoad;
  135. } elseif (is_array($this->_classesToLoad)) {
  136. $classesToLoad = array_merge($classesToLoad, $this->_classesToLoad);
  137. }
  138. }
  139. // add classes to the basic loader from the config file basicloader.classes.1 ..
  140. if (isset($config->basicloader) && isset($config->basicloader->classes)) {
  141. foreach ($config->basicloader->classes as $classKey => $className) {
  142. array_push($classesToLoad, $className);
  143. }
  144. }
  145. $this->_registry->setLoader(
  146. new Zend_Tool_Framework_Loader_BasicLoader(array('classesToLoad' => $classesToLoad))
  147. );
  148. return;
  149. }
  150. /**
  151. * _preDispatch() - Tasks handed after initialization but before dispatching
  152. *
  153. */
  154. protected function _preDispatch()
  155. {
  156. $response = $this->_registry->getResponse();
  157. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_AlignCenter());
  158. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Indention());
  159. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Blockize());
  160. if (function_exists('posix_isatty')) {
  161. $response->addContentDecorator(new Zend_Tool_Framework_Client_Console_ResponseDecorator_Colorizer());
  162. }
  163. $response->addContentDecorator(new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator())
  164. ->setDefaultDecoratorOptions(array('separator' => true));
  165. $optParser = new Zend_Tool_Framework_Client_Console_ArgumentParser();
  166. $optParser->setArguments($_SERVER['argv'])
  167. ->setRegistry($this->_registry)
  168. ->parse();
  169. return;
  170. }
  171. /**
  172. * _postDispatch() - Tasks handled after dispatching
  173. *
  174. */
  175. protected function _postDispatch()
  176. {
  177. $request = $this->_registry->getRequest();
  178. $response = $this->_registry->getResponse();
  179. if ($response->isException()) {
  180. $helpSystem = new Zend_Tool_Framework_Client_Console_HelpSystem();
  181. $helpSystem->setRegistry($this->_registry)
  182. ->respondWithErrorMessage($response->getException()->getMessage(), $response->getException())
  183. ->respondWithSpecialtyAndParamHelp(
  184. $request->getProviderName(),
  185. $request->getActionName()
  186. );
  187. }
  188. echo PHP_EOL;
  189. return;
  190. }
  191. /**
  192. * handleInteractiveInputRequest() is required by the Interactive InputInterface
  193. *
  194. *
  195. * @param Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest
  196. * @return string
  197. */
  198. public function handleInteractiveInputRequest(Zend_Tool_Framework_Client_Interactive_InputRequest $inputRequest)
  199. {
  200. fwrite(STDOUT, $inputRequest->getContent() . PHP_EOL . 'zf> ');
  201. $inputContent = fgets(STDIN);
  202. return rtrim($inputContent); // remove the return from the end of the string
  203. }
  204. /**
  205. * handleInteractiveOutput() is required by the Interactive OutputInterface
  206. *
  207. * This allows us to display output immediately from providers, rather
  208. * than displaying it after the provider is done.
  209. *
  210. * @param string $output
  211. */
  212. public function handleInteractiveOutput($output)
  213. {
  214. echo $output;
  215. }
  216. /**
  217. * getMissingParameterPromptString()
  218. *
  219. * @param Zend_Tool_Framework_Provider_Interface $provider
  220. * @param Zend_Tool_Framework_Action_Interface $actionInterface
  221. * @param string $missingParameterName
  222. * @return string
  223. */
  224. public function getMissingParameterPromptString(Zend_Tool_Framework_Provider_Interface $provider, Zend_Tool_Framework_Action_Interface $actionInterface, $missingParameterName)
  225. {
  226. return 'Please provide a value for $' . $missingParameterName;
  227. }
  228. /**
  229. * convertToClientNaming()
  230. *
  231. * Convert words to client specific naming, in this case is lower, dash separated
  232. *
  233. * Filters are lazy-loaded.
  234. *
  235. * @param string $string
  236. * @return string
  237. */
  238. public function convertToClientNaming($string)
  239. {
  240. if (!$this->_filterToClientNaming) {
  241. $filter = new Zend_Filter();
  242. $filter->addFilter(new Zend_Filter_Word_CamelCaseToDash());
  243. $filter->addFilter(new Zend_Filter_StringToLower());
  244. $this->_filterToClientNaming = $filter;
  245. }
  246. return $this->_filterToClientNaming->filter($string);
  247. }
  248. /**
  249. * convertFromClientNaming()
  250. *
  251. * Convert words from client specific naming to code naming - camelcased
  252. *
  253. * Filters are lazy-loaded.
  254. *
  255. * @param string $string
  256. * @return string
  257. */
  258. public function convertFromClientNaming($string)
  259. {
  260. if (!$this->_filterFromClientNaming) {
  261. $this->_filterFromClientNaming = new Zend_Filter_Word_DashToCamelCase();
  262. }
  263. return $this->_filterFromClientNaming->filter($string);
  264. }
  265. }