Hoa central
Kit.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Console;
40 use Hoa\Dispatcher;
41 use Hoa\Router;
42 use Hoa\View;
43 
52 class Kit extends Dispatcher\Kit
53 {
59  public $parser = null;
60 
66  protected $options = null;
67 
73  protected $_options = null;
74 
75 
76 
85  public function __construct(
88  View\Viewable $view = null
89  ) {
90  parent::__construct($router, $dispatcher, $view);
91 
92  $this->parser = new Console\Parser();
93 
94  return;
95  }
96 
104  public function getOption(&$optionValue, $short = null)
105  {
106  if (null === $this->_options && !empty($this->options)) {
107  $this->setOptions($this->options);
108  }
109 
110  if (null === $this->_options) {
111  return false;
112  }
113 
114  return $this->_options->getOption($optionValue, $short);
115  }
116 
124  public function setOptions(Array $options)
125  {
126  $old = $this->options;
127  $this->options = $options;
128  $rule = $this->router->getTheRule();
129  $variables = $rule[Router::RULE_VARIABLES];
130 
131  if (isset($variables['_tail'])) {
132  $this->parser->parse($variables['_tail']);
133  $this->_options = new Console\GetOption(
134  $this->options,
135  $this->parser
136  );
137  }
138 
139  return $old;
140  }
141 
149  public function makeUsageOptionsList(Array $definitions = [])
150  {
151  $out = [];
152 
153  foreach ($this->options as $i => $options) {
154  $out[] = [
155  ' -' . $options[Console\GetOption::OPTION_VAL] . ', --' .
159  ? '='
162  ? '[=]'
163  : '')),
164  (isset($definitions[$options[Console\GetOption::OPTION_VAL]])
165  ? $definitions[$options[Console\GetOption::OPTION_VAL]]
166  : (isset($definitions[$options[0]])
167  ? $definitions[$options[Console\GetOption::OPTION_NAME]]
168  : null
169  )
170  )
171  ];
172  }
173 
175  $out,
176  Console\Chrome\Text::ALIGN_LEFT,
177  .5,
178  0,
179  '|: '
180  );
181  }
182 
190  public function resolveOptionAmbiguity(Array $solutions)
191  {
192  echo
193  'You have made a typo in the option ',
194  $solutions['option'], '; it can match the following options: ', "\n",
195  ' • ', implode(";\n • ", $solutions['solutions']), '.', "\n",
196  'Please, type the right option (empty to choose the first one):', "\n";
197  $new = $this->readLine('> ');
198 
199  if (empty($new)) {
200  $new = $solutions['solutions'][0];
201  }
202 
203  $solutions['solutions'] = [$new];
204 
205  $this->_options->resolveOptionAmbiguity($solutions);
206 
207  return;
208  }
209 
217  public function status($text, $status)
218  {
219  $window = Console\Window::getSize();
220  $out =
221  ' ' . Console\Chrome\Text::colorize('*', 'foreground(yellow)') . ' ' .
222  $text . str_pad(
223  ' ',
224  $window['x']
225  - strlen(preg_replace('#' . "\033" . '\[[0-9]+m#', '', $text))
226  - 8
227  ) .
228  ($status === true
229  ? '[' . Console\Chrome\Text::colorize('ok', 'foreground(green)') . ']'
230  : '[' . Console\Chrome\Text::colorize('!!', 'foreground(white) background(red)') . ']');
231 
232  echo $out, "\n";
233 
234  return;
235  }
236 
243  public function readLine($prefix = null)
244  {
245  static $_rl = null;
246 
247  if (null === $_rl) {
248  $_rl = new Console\Readline();
249  }
250 
251  return $_rl->readLine($prefix);
252  }
253 
260  public function readPassword($prefix = null)
261  {
262  static $_rl = null;
263 
264  if (null === $_rl) {
265  $_rl = new Console\Readline\Password();
266  }
267 
268  return $_rl->readLine($prefix);
269  }
270 }
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
const RULE_VARIABLES
Definition: Router.php:112
status($text, $status)
Definition: Kit.php:217
setOptions(Array $options)
Definition: Kit.php:124
static getSize()
Definition: Window.php:118
readPassword($prefix=null)
Definition: Kit.php:260
static columnize(Array $line, $alignement=self::ALIGN_LEFT, $horizontalPadding=2, $verticalPadding=0, $separator=null)
Definition: Text.php:126
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190
readLine($prefix=null)
Definition: Kit.php:243
static colorize($text, $attributesBefore, $attributesAfter= 'normal')
Definition: Text.php:83
__construct(Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
Definition: Kit.php:85