PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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