PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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