Hoa central
Diagnostic.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Devtools\Bin;
38 
39 use Hoa\Console;
40 use Hoa\Core;
41 
51 {
57  protected $options = [
58  ['section', Console\GetOption::REQUIRED_ARGUMENT, 's'],
60  ['help', Console\GetOption::NO_ARGUMENT, 'h'],
61  ['help', Console\GetOption::NO_ARGUMENT, '?']
62  ];
63 
64 
65 
71  public function main()
72  {
73  $sections = [];
74  $mail = null;
75  $diagnostic = [];
76 
77  while (false !== $c = $this->getOption($v)) {
78  switch ($c) {
79  case 's':
80  $sections = $this->parser->parseSpecialValue($v);
81 
82  break;
83 
84  case 'm':
85  $mail = $v;
86 
87  break;
88 
89  case 'h':
90  case '?':
91  return $this->usage();
92 
93  case '__ambiguous':
94  $this->resolveOptionAmbiguity($v);
95 
96  break;
97  }
98  }
99 
100  $store = function ($sections, $key, $value = null) use (&$diagnostic) {
101  if (is_array($key) && null === $value) {
102  foreach ($key as $i => $name) {
103  $diagnostic[$sections][$i] = $name;
104  }
105  } else {
106  $diagnostic[$sections][$key] = $value;
107  }
108 
109  return;
110  };
111 
112  $store(
113  'version',
114  'php',
115  phpversion()
116  );
117  $store(
118  'version',
119  'zend_engine',
120  zend_version()
121  );
122  $store(
123  'system',
124  'platform',
125  php_uname()
126  );
127  $store(
128  'system',
129  'architecture',
130  (true === S_32_BITS) ? '32bits' : '64bits'
131  );
132  $store(
133  'system',
134  'lang',
135  isset($_SERVER['LANG']) ? $_SERVER['LANG'] : 'unknown'
136  );
137  $store(
138  'bin',
139  'self',
140  $_SERVER['PHP_SELF']
141  );
142  $store(
143  'bin',
144  'hoa',
145  Core::getInstance()->getParameters()->getFormattedParameter('root.hoa')
146  );
147  $store(
148  'bin',
149  'php_dir',
150  PHP_BINDIR
151  );
152  $store(
153  'bin',
154  'php',
155  defined('PHP_BINARY') ? PHP_BINARY : 'unknown'
156  );
157 
158  foreach (get_loaded_extensions() as $extension) {
159  $reflection = new \ReflectionExtension($extension);
160  $entry = 'extension-' . strtolower($extension);
161 
162  if ('extension-standard' !== $entry &&
163  'extension-core' !== $entry) {
164  $entries = [];
165 
166  foreach ($reflection->getINIEntries() as $key => $value) {
167  $entries[substr($key, strpos($key, '.') + 1)] = $value;
168  }
169  } else {
170  $entries = $reflection->getINIEntries();
171  }
172 
173  $store(
174  $entry,
175  'version',
176  $reflection->getVersion() ?: 'unknown'
177  );
178  $store(
179  $entry,
180  $entries
181  );
182  }
183 
184  if (empty($sections) || in_array('all', $sections)) {
185  $ini = $this->arrayToIni($diagnostic);
186  } else {
187  $handle = [];
188 
189  foreach ($sections as $section) {
190  if (false === array_key_exists($section, $diagnostic)) {
191  return 1;
192  }
193 
194  $handle[$section] = $diagnostic[$section];
195  }
196 
197  $ini = $this->arrayToIni($handle);
198  }
199 
200  echo $ini, "\n";
201 
202  if (null !== $mail) {
203  $subject = 'Diagnostic from ' . get_current_user();
204 
205  return mail($mail, $subject, $ini) ? 0 : 1;
206  }
207 
208  return;
209  }
210 
216  public function usage()
217  {
218  echo
219  'Usage : devtools:diagnostic <options>', "\n",
220  'Options :', "\n",
221  $this->makeUsageOptionsList([
222  's' => 'Sections (comma separated) to display, among:' . "\n" .
223  ' • all;' . "\n" .
224  ' • version;' . "\n" .
225  ' • system;' . "\n" .
226  ' • bin;' . "\n" .
227  ' • extension-<name in lowercase> (see `php -m`).',
228  'm' => 'Email address where to send the diagnostic.',
229  'help' => 'This help.'
230  ]), "\n";
231 
232  return;
233  }
234 
241  private function arrayToIni(Array $array)
242  {
243  $out = null;
244 
245  foreach ($array as $section => $entries) {
246  if (null !== $out) {
247  $out .= "\n\n";
248  }
249 
250  $out .= '[' . $section . ']';
251 
252  foreach ($entries as $key => $value) {
253  if (is_array($value)) {
254  $value = implode(' ', $value);
255  }
256 
257  $out .= "\n" . $key . ' = "' . $value . '"';
258  }
259  }
260 
261  return $out;
262  }
263 }
264 
265 __halt_compiler();
266 Generate a diagnostic for help.
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
static getInstance()
Definition: Core.php:193
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190
getParameters()
Definition: Core.php:266