Kit.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Console\Dispatcher;
  36. use Hoa\Console;
  37. use Hoa\Dispatcher;
  38. use Hoa\Router;
  39. use Hoa\View;
  40. /**
  41. * Class \Hoa\Console\Dispatcher\Kit.
  42. *
  43. * A structure, given to action, that holds some important data.
  44. *
  45. * @copyright Copyright © 2007-2017 Hoa community
  46. * @license New BSD License
  47. */
  48. class Kit extends Dispatcher\Kit
  49. {
  50. /**
  51. * CLI parser.
  52. *
  53. * @var \Hoa\Console\Parser
  54. */
  55. public $parser = null;
  56. /**
  57. * Options (as described in \Hoa\Console\GetOption).
  58. *
  59. * @var array
  60. */
  61. protected $options = null;
  62. /**
  63. * Options analyzer.
  64. *
  65. * @var \Hoa\Console\GetOption
  66. */
  67. protected $_options = null;
  68. /**
  69. * Build a dispatcher kit.
  70. *
  71. * @param \Hoa\Router $router The router.
  72. * @param \Hoa\Dispatcher $dispatcher The dispatcher.
  73. * @param \Hoa\View\Viewable $view The view.
  74. */
  75. public function __construct(
  76. Router $router,
  77. Dispatcher $dispatcher,
  78. View\Viewable $view = null
  79. ) {
  80. parent::__construct($router, $dispatcher, $view);
  81. $this->parser = new Console\Parser();
  82. return;
  83. }
  84. /**
  85. * Alias of \Hoa\Console\GetOption::getOptions().
  86. *
  87. * @param string &$optionValue Please, see original API.
  88. * @param string $short Please, see original API.
  89. * @return mixed
  90. */
  91. public function getOption(&$optionValue, $short = null)
  92. {
  93. if (null === $this->_options && !empty($this->options)) {
  94. $this->setOptions($this->options);
  95. }
  96. if (null === $this->_options) {
  97. return false;
  98. }
  99. return $this->_options->getOption($optionValue, $short);
  100. }
  101. /**
  102. * Initialize options.
  103. *
  104. * @param array $options Options, as described in
  105. * \Hoa\Console\GetOption.
  106. * @return array
  107. */
  108. public function setOptions(array $options)
  109. {
  110. $old = $this->options;
  111. $this->options = $options;
  112. $rule = $this->router->getTheRule();
  113. $variables = $rule[Router::RULE_VARIABLES];
  114. if (isset($variables['_tail'])) {
  115. $this->parser->parse($variables['_tail']);
  116. $this->_options = new Console\GetOption(
  117. $this->options,
  118. $this->parser
  119. );
  120. }
  121. return $old;
  122. }
  123. /**
  124. * It is a helper to make the usage options list.
  125. *
  126. * @param array $definitions An associative arry: short or long option
  127. * associated to the definition.
  128. * @return string
  129. */
  130. public function makeUsageOptionsList(array $definitions = [])
  131. {
  132. $out = [];
  133. foreach ($this->options as $i => $options) {
  134. $out[] = [
  135. ' -' . $options[Console\GetOption::OPTION_VAL] . ', --' .
  136. $options[Console\GetOption::OPTION_NAME] .
  137. ($options[Console\GetOption::OPTION_HAS_ARG] ===
  138. Console\GetOption::REQUIRED_ARGUMENT
  139. ? '='
  140. : ($options[Console\GetOption::OPTION_HAS_ARG] ===
  141. Console\GetOption::OPTIONAL_ARGUMENT
  142. ? '[=]'
  143. : '')),
  144. (isset($definitions[$options[Console\GetOption::OPTION_VAL]])
  145. ? $definitions[$options[Console\GetOption::OPTION_VAL]]
  146. : (isset($definitions[$options[0]])
  147. ? $definitions[$options[Console\GetOption::OPTION_NAME]]
  148. : null
  149. )
  150. )
  151. ];
  152. }
  153. return Console\Chrome\Text::columnize(
  154. $out,
  155. Console\Chrome\Text::ALIGN_LEFT,
  156. .5,
  157. 0,
  158. '|: '
  159. );
  160. }
  161. /**
  162. * Resolve option ambiguity by asking the user to choose amongst some
  163. * appropriated solutions.
  164. *
  165. * @param array $solutions Solutions.
  166. * @return void
  167. */
  168. public function resolveOptionAmbiguity(array $solutions)
  169. {
  170. echo
  171. 'You have made a typo in the option ',
  172. $solutions['option'], '; it can match the following options: ', "\n",
  173. ' • ', implode(";\n • ", $solutions['solutions']), '.', "\n",
  174. 'Please, type the right option (empty to choose the first one):', "\n";
  175. $new = $this->readLine('> ');
  176. if (empty($new)) {
  177. $new = $solutions['solutions'][0];
  178. }
  179. $solutions['solutions'] = [$new];
  180. $this->_options->resolveOptionAmbiguity($solutions);
  181. return;
  182. }
  183. /**
  184. * Make a render of an operation.
  185. *
  186. * @param string $text The operation text.
  187. * @param bool $status The operation status.
  188. * @return void
  189. */
  190. public function status($text, $status)
  191. {
  192. $window = Console\Window::getSize();
  193. $out =
  194. ' ' . Console\Chrome\Text::colorize('*', 'foreground(yellow)') . ' ' .
  195. $text . str_pad(
  196. ' ',
  197. $window['x']
  198. - strlen(preg_replace('#' . "\033" . '\[[0-9]+m#', '', $text))
  199. - 8
  200. ) .
  201. ($status === true
  202. ? '[' . Console\Chrome\Text::colorize('ok', 'foreground(green)') . ']'
  203. : '[' . Console\Chrome\Text::colorize('!!', 'foreground(white) background(red)') . ']');
  204. Console::getOutput()->writeAll($out . "\n");
  205. return;
  206. }
  207. /**
  208. * Read, edit, bind… a line from STDIN.
  209. *
  210. * @param string $prefix Prefix.
  211. * @return string
  212. */
  213. public function readLine($prefix = null)
  214. {
  215. static $_rl = null;
  216. if (null === $_rl) {
  217. $_rl = new Console\Readline();
  218. }
  219. return $_rl->readLine($prefix);
  220. }
  221. /**
  222. * Read, edit, bind… a password from STDIN.
  223. *
  224. * @param string $prefix Prefix.
  225. * @return string
  226. */
  227. public function readPassword($prefix = null)
  228. {
  229. static $_rl = null;
  230. if (null === $_rl) {
  231. $_rl = new Console\Readline\Password();
  232. }
  233. return $_rl->readLine($prefix);
  234. }
  235. }