Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Xml\Bin\Shell Class Reference
Inheritance diagram for Hoa\Xml\Bin\Shell:

Public Member Functions

 main ()
 
 usage ()
 
 cout (\DOMNode $element)
 
 color ($text, $token)
 
- Public Member Functions inherited from Hoa\Console\Dispatcher\Kit
 __construct (Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
 
 getOption (&$optionValue, $short=null)
 
 setOptions (Array $options)
 
 makeUsageOptionsList (Array $definitions=[])
 
 resolveOptionAmbiguity (Array $solutions)
 
 status ($text, $status)
 
 readLine ($prefix=null)
 
 readPassword ($prefix=null)
 
- Public Member Functions inherited from Hoa\Dispatcher\Kit
 __construct (Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
 
 construct ()
 

Protected Attributes

 $options
 
 $_color = 1
 
 $_colors
 
- Protected Attributes inherited from Hoa\Console\Dispatcher\Kit
 $options = null
 
 $_options = null
 

Additional Inherited Members

- Public Attributes inherited from Hoa\Console\Dispatcher\Kit
 $parser = null
 
- Public Attributes inherited from Hoa\Dispatcher\Kit
 $router = null
 
 $dispatcher = null
 
 $view = null
 
 $data = null
 

Detailed Description

Class .

Interactive XML shell.

Definition at line 51 of file Shell.php.

Member Function Documentation

Hoa\Xml\Bin\Shell::color (   $text,
  $token 
)

Use colors.

Parameters
string$textText.
string$tokenToken.
Returns
string

Definition at line 369 of file Shell.php.

370  {
371  return
372  "\033[" . $this->_colors[$this->_color][$token] . 'm' .
373  $text .
374  "\033[0m";
375  }

Here is the caller graph for this function:

Hoa\Xml\Bin\Shell::cout ( \DOMNode  $element)

Pretty print XML tree.

Parameters
\DOMNode$elementElement.
Returns
string

Definition at line 285 of file Shell.php.

286  {
287  if (1 === $this->_color) {
288  return $element->C14N();
289  }
290 
291  $out = null;
292  $nodes = $element->childNodes;
293 
294  for ($i = 0, $max = $nodes->length; $i < $max; ++$i) {
295  $node = $nodes->item($i);
296 
297  switch ($node->nodeType) {
298  case XML_ELEMENT_NODE:
299  $out .=
300  $this->color('<', 'oc') .
301  $this->color($node->tagName, 'tagname');
302 
303  foreach ($node->attributes as $attr) {
304  $out .=
305  ' ' .
306  $this->color($attr->name, 'attrname') .
307  $this->color('=', '=') .
308  $this->color('"', 'q') .
309  $this->color($attr->value, 'attrvalue') .
310  $this->color('"', 'q');
311  }
312 
313  $out .=
314  $this->color('>', 'oc') .
315  $this->cout($node) .
316  $this->color('</', 'oc') .
317  $this->color($node->tagName, 'tagname') .
318  $this->color('>', 'oc');
319 
320  break;
321 
322  case XML_TEXT_NODE:
323  $out .= $this->color($node->wholeText, 'text');
324 
325  break;
326 
327  case XML_CDATA_SECTION_NODE:
328  break;
329 
330  case XML_ENTITY_REF_NODE:
331  $out .= $this->color('&' . $node->name . ';', 'entity');
332 
333  break;
334 
335  case XML_ENTITY_NODE:
336  break;
337 
338  case XML_PI_NODE:
339  $out .= $this->color(
340  '<?' . $node->target . ' ' . $node->data . '?>',
341  'pi'
342  );
343 
344  break;
345 
346  case XML_COMMENT_NODE:
347  $out .= $this->color(
348  '<!--' . $node->data . '-->',
349  'comment'
350  );
351 
352  break;
353 
354  default:
355  var_dump($node->nodeType);
356  }
357  }
358 
359  return $out;
360  }
color($text, $token)
Definition: Shell.php:369
cout(\DOMNode $element)
Definition: Shell.php:285

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Bin\Shell::main ( )

The entry method.

Returns
int

Definition at line 110 of file Shell.php.

111  {
112  $filename = null;
113 
114  while (false !== $c = $this->getOption($v)) {
115  switch ($c) {
116  case 'c':
117  $v = true === $v ? 256 : intval($v);
118 
119  switch ($v) {
120  case 1:
121  case 8:
122  case 256:
123  $this->_color = $v;
124 
125  break;
126  }
127 
128  break;
129 
130  case 'h':
131  case '?':
132  return $this->usage();
133 
134  case '__ambiguous':
135  $this->resolveOptionAmbiguity($v);
136 
137  break;
138  }
139  }
140 
141  $this->parser->listInputs($filename);
142 
143  if (null === $filename) {
144  return $this->usage();
145  }
146 
147  $line = 'load ' . $filename;
148 
149  do {
150  try {
151  @list($command, $argument) = preg_split('#\s+#', $line, 2);
152 
153  switch ($command) {
154  case 'load':
155  if ('%' === $argument) {
156  $root = new \Hoa\Xml\Read(
157  new \Hoa\File\Read(
158  $root->getInnerStream()->getStreamName()
159  ),
160  false
161  );
162  echo 'Reloaded!';
163  } else {
164  $root = new \Hoa\Xml\Read(
165  new \Hoa\File\Read($argument),
166  false
167  );
168  echo 'Loaded!';
169  }
170 
171  $current = $root;
172  echo "\n";
173 
174  case 'h':
175  case 'help':
176  echo
177  'Usage:', "\n",
178  ' h[elp] to print this help;', "\n",
179  ' % to print loaded filename;', "\n",
180  ' load to load a file (`load %` to reload);', "\n",
181  ' ls to print current tree;', "\n",
182  ' cd to move in the tree with XPath;', "\n",
183  ' pwd to print the current path;', "\n",
184  ' color to change color (among 1, 8 and 256);', "\n",
185  ' q[uit] to quit.', "\n";
186 
187  break;
188 
189  case '%':
190  echo $root->getInnerStream()->getStreamName(), "\n";
191 
192  break;
193 
194  case 'ls':
195  echo $this->cout($current->readDOM()), "\n";
196 
197  break;
198 
199  case 'cd':
200  if (null === $argument) {
201  echo 'Need an argument.', "\n";
202 
203  break;
204  }
205 
206  $handle = $current->xpath($argument);
207 
208  if (empty($handle)) {
209  echo $argument, ' is not found.', "\n";
210 
211  break;
212  }
213 
214  $current = $handle[0];
215 
216  break;
217 
218  case 'pwd':
219  echo $current->readDOM()->getNodePath(), "\n";
220 
221  break;
222 
223  case 'color':
224  $argument = intval($argument);
225 
226  if (1 !== $argument &&
227  8 !== $argument &&
228  256 !== $argument) {
229  echo
230  $argument,
231  ' is not valid color (1, 8 or 256).', "\n";
232 
233  break;
234  }
235 
236  $this->_color = $argument;
237 
238  break;
239 
240  case 'q':
241  case 'quit':
242  break 2;
243 
244  default:
245  if (!empty($command)) {
246  echo 'Command ', $command, ' not found.', "\n";
247  }
248  }
249  } catch (Core\Exception $e) {
250  echo $e->getMessage(), "\n";
251 
252  continue;
253  }
254 
255  echo "\n";
256  } while (false !== $line = $this->readLine('> '));
257 
258  return;
259  }
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190
readLine($prefix=null)
Definition: Kit.php:243
cout(\DOMNode $element)
Definition: Shell.php:285

Here is the call graph for this function:

Hoa\Xml\Bin\Shell::usage ( )

The command usage.

Returns
int

Definition at line 266 of file Shell.php.

267  {
268  echo
269  'Usage : xml:shell <options> [filename]', "\n",
270  'Options :', "\n",
271  $this->makeUsageOptionsList([
272  'c' => 'Allowed colors number among 1, 8 and 256 (default).',
273  'help' => 'This help.'
274  ]), "\n";
275 
276  return;
277  }
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Xml\Bin\Shell::$_color = 1
protected

Definition at line 69 of file Shell.php.

Hoa\Xml\Bin\Shell::$_colors
protected
Initial value:
= [
8 => [
'oc' => '34'

Definition at line 76 of file Shell.php.

Hoa\Xml\Bin\Shell::$options
protected
Initial value:

Definition at line 58 of file Shell.php.


The documentation for this class was generated from the following file: