Hoa central
Reflection.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Realdom\Bin;
38 
39 use Hoa\Console;
40 
50 {
56  protected $options = [
57  ['list', Console\GetOption::NO_ARGUMENT, 'l'],
58  ['help', Console\GetOption::NO_ARGUMENT, 'h'],
59  ['help', Console\GetOption::NO_ARGUMENT, '?']
60  ];
61 
62 
63 
69  public function main()
70  {
71  $list = false;
72 
73  while (false !== $c = $this->getOption($v)) {
74  switch ($c) {
75  case 'l':
76  $list = $v;
77 
78  break;
79 
80  case 'h':
81  case '?':
82  return $this->usage();
83 
84  case '__ambiguous':
85  $this->resolveOptionAmbiguity($v);
86 
87  break;
88  }
89  }
90 
91  $matches = [];
92 
93  from('Hoathis or Hoa')
94  -> foreachImport('Realdom.*', function ($classname) use (&$matches) {
95  $class = new \ReflectionClass($classname);
96 
97  if ($class->isSubclassOf('\Hoa\Realdom')) {
98  $matches[$classname::NAME] = $class;
99  }
100 
101  return;
102  });
103 
104  if (true === $list) {
105  echo implode("\n", array_keys($matches)), "\n";
106 
107  return;
108  }
109 
110  $this->parser->listInputs($realdom);
111 
112  if (empty($realdom)) {
113  return $this->usage();
114  }
115 
116  if (!isset($matches[$realdom])) {
117  throw new \Hoa\Console\Exception(
118  'The %s realistic domain does not exist.',
119  0,
120  $realdom
121  );
122  }
123 
124  $class = $matches[$realdom];
125  $object = $class->newInstanceWithoutConstructor();
126  $_arguments = $class->getProperty('_arguments');
127  $_arguments->setAccessible(true);
128  $arguments = $_arguments->getValue($object);
129 
130  echo
131  'Realdom ', $realdom, ' {', "\n\n",
132  ' Implementation ', $class->getName(), ';', "\n\n",
133  ' Parent ', $class->getParentClass()->getName(), ';', "\n\n",
134  ' Interfaces {', "\n\n";
135 
136  $interfaces = $class->getInterfaces();
137  usort($interfaces, function ($a, $b) {
138  if ('' === $a->getNamespaceName()) {
139  if ('' === $b->getNamespaceName()) {
140  return strcmp($a->getName(), $b->getName());
141  } else {
142  return -1;
143  }
144  }
145 
146  if ('' === $b->getNamespaceName()) {
147  return 1;
148  }
149 
150  return strcmp($a->getName(), $b->getName());
151  });
152 
153  foreach ($interfaces as $interface) {
154  echo ' ', $interface->getName(), ';', "\n";
155  }
156 
157  echo
158  ' }', "\n\n",
159  ' Parameters {', "\n\n";
160 
161  $i = 0;
162 
163  if (is_array($arguments)) {
164  foreach ($arguments as $typeAndName => $defaultValue) {
165  if (is_int($typeAndName)) {
166  $typeAndName = $defaultValue;
167  $defaultValue = null;
168  }
169 
170  echo
171  ' [#', $i++,
172  ' ', (null === $defaultValue ? 'required' : 'optional'), '] ',
173  $typeAndName;
174 
175  if (null !== $defaultValue) {
176  echo ' = ', var_export($defaultValue, true);
177  }
178 
179  echo ';', "\n";
180  }
181  } else {
182  echo ' …variadic', "\n";
183  }
184 
185  echo ' }', "\n", '}', "\n";
186 
187  return;
188  }
189 
195  public function usage()
196  {
197  echo
198  'Usage : realdom:reflection <options> [realdom]', "\n",
199  'Options :', "\n",
200  $this->makeUsageOptionsList([
201  'l' => 'List all realdoms.',
202  'help' => 'This help.'
203  ]), "\n";
204 
205  return;
206  }
207 }
208 
209 __halt_compiler();
210 Show informations about a realistic domain.
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190