Hoa central
Welcome.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Core\Bin;
38 
39 use Hoa\Console;
40 use Hoa\Core;
41 
51 {
57  protected $options = [
58  ['library', Console\GetOption::REQUIRED_ARGUMENT, 'l'],
59  ['no-verbose', Console\GetOption::NO_ARGUMENT, 'V'],
60  ['help', Console\GetOption::NO_ARGUMENT, 'h'],
61  ['help', Console\GetOption::NO_ARGUMENT, '?']
62  ];
63 
64 
65 
71  public function main()
72  {
73  $library = null;
74  $verbose = Console::isDirect(STDOUT);
75 
76  while (false !== $c = $this->getOption($v)) {
77  switch ($c) {
78  case 'l':
79  $library = $this->parser->parseSpecialValue($v);
80 
81  break;
82 
83  case 'V':
84  $verbose = false;
85 
86  break;
87 
88  case 'h':
89  case '?':
90  return $this->usage();
91 
92  case '__ambiguous':
93  $this->resolveOptionAmbiguity($v);
94 
95  break;
96  }
97  }
98 
99  if (true === $verbose) {
100  echo
102  ' _ _' . "\n" .
103  ' | | | | ___ __ _' . "\n" .
104  ' | |_| |/ _ \ / _` |' . "\n" .
105  ' | _ | (_) | (_| |' . "\n" .
106  ' |_| |_|\___/ \__,_|' . "\n",
107  'foreground(yellow)'
108  ), "\n\n",
109  'Welcome in the command-line interface of Hoa :-).', "\n\n",
111  'List of available commands',
112  'foreground(green)'
113  ), "\n\n";
114  }
115 
116  if (null !== $library) {
117  $library = array_map('mb_strtolower', $library);
118  }
119 
120  $locations = resolve('hoa://Library', true, true);
121  $iterator = new \AppendIterator();
122 
123  foreach ($locations as $location) {
124  $iterator->append(new \GlobIterator(
125  $location . '*' . DS . 'Bin' . DS . '*.php'
126  ));
127  }
128 
129  if (WITH_COMPOSER) {
130  $iterator->append(new \GlobIterator(
131  dirname(dirname(dirname(dirname(__DIR__)))) . DS . 'Bin' . DS . '*.php'
132  ));
133  }
134 
135  $binaries = [];
136 
137  foreach ($iterator as $entry) {
138  $pathname = $entry->getPathname();
139  $lib = mb_strtolower(basename(dirname(dirname($pathname))));
140  $bin = mb_strtolower(
141  mb_substr($entry->getBasename(), 0, -4)
142  );
143 
144  if (null !== $library &&
145  false === in_array($lib, $library)) {
146  continue;
147  }
148 
149  if ('core' === $lib && 'hoa' === $bin) {
150  continue;
151  }
152 
153  if (!isset($binaries[$lib])) {
154  $binaries[$lib] = [];
155  }
156 
157  $description = '';
158 
159  if (true === $verbose) {
160  $lines = file($pathname);
161 
162  // Berkā€¦
163  for ($i = count($lines) - 1; $i >= 0; --$i) {
164  if ('__halt_compiler();' . "\n" === $lines[$i]) {
165  $description = trim(implode(
166  '',
167  array_slice($lines, $i + 1)
168  ));
169 
170  break;
171  }
172  }
173 
174  unset($lines);
175  }
176 
177  $binaries[$lib][] = [
178  'name' => $bin,
179  'description' => $description
180  ];
181  }
182 
183  if (true === $verbose) {
184  $out = [];
185 
186  foreach ($binaries as $group => $commands) {
187  $out[] = [mb_convert_case($group, MB_CASE_TITLE)];
188 
189  foreach ($commands as $binary) {
190  $out[] = [
191  ' ' .
193  $binary['name'],
194  'foreground(blue)'
195  ),
196  $binary['description']
197  ];
198  }
199  }
200 
202  } else {
203  $out = null;
204 
205  foreach ($binaries as $group => $commands) {
206  foreach ($commands as $binary) {
207  $out .= $group . ':' . $binary['name'] . "\n";
208  }
209  }
210 
211  echo $out;
212  }
213 
214  return;
215  }
216 
222  public function usage()
223  {
224  echo
225  'Usage : core:welcome <options>', "\n",
226  'Options :', "\n",
227  $this->makeUsageOptionsList([
228  'l' => 'Filter libraries to list (comma-separated).',
229  'V' => 'No-verbose, i.e. be as quiet as possible, just ' .
230  'print essential informations.',
231  'help' => 'This help.'
232  ]);
233 
234  return;
235  }
236 }
237 
238 __halt_compiler();
239 This page.
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
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
static colorize($text, $attributesBefore, $attributesAfter= 'normal')
Definition: Text.php:83
static isDirect($pipe)
Definition: Console.php:261