comparison vendor/psy/psysh/src/Command/DocCommand.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 /*
4 * This file is part of Psy Shell.
5 *
6 * (c) 2012-2018 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Psy\Command;
13
14 use Psy\Formatter\DocblockFormatter;
15 use Psy\Formatter\SignatureFormatter;
16 use Psy\Input\CodeArgument;
17 use Psy\Reflection\ReflectionClassConstant;
18 use Psy\Reflection\ReflectionLanguageConstruct;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Output\OutputInterface;
21
22 /**
23 * Read the documentation for an object, class, constant, method or property.
24 */
25 class DocCommand extends ReflectingCommand
26 {
27 /**
28 * {@inheritdoc}
29 */
30 protected function configure()
31 {
32 $this
33 ->setName('doc')
34 ->setAliases(['rtfm', 'man'])
35 ->setDefinition([
36 new CodeArgument('target', CodeArgument::REQUIRED, 'Function, class, instance, constant, method or property to document.'),
37 ])
38 ->setDescription('Read the documentation for an object, class, constant, method or property.')
39 ->setHelp(
40 <<<HELP
41 Read the documentation for an object, class, constant, method or property.
42
43 It's awesome for well-documented code, not quite as awesome for poorly documented code.
44
45 e.g.
46 <return>>>> doc preg_replace</return>
47 <return>>>> doc Psy\Shell</return>
48 <return>>>> doc Psy\Shell::debug</return>
49 <return>>>> \$s = new Psy\Shell</return>
50 <return>>>> doc \$s->run</return>
51 HELP
52 );
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 protected function execute(InputInterface $input, OutputInterface $output)
59 {
60 $value = $input->getArgument('target');
61 if (ReflectionLanguageConstruct::isLanguageConstruct($value)) {
62 $reflector = new ReflectionLanguageConstruct($value);
63 $doc = $this->getManualDocById($value);
64 } else {
65 list($target, $reflector) = $this->getTargetAndReflector($value);
66 $doc = $this->getManualDoc($reflector) ?: DocblockFormatter::format($reflector);
67 }
68
69 $db = $this->getApplication()->getManualDb();
70
71 $output->page(function ($output) use ($reflector, $doc, $db) {
72 $output->writeln(SignatureFormatter::format($reflector));
73 $output->writeln('');
74
75 if (empty($doc) && !$db) {
76 $output->writeln('<warning>PHP manual not found</warning>');
77 $output->writeln(' To document core PHP functionality, download the PHP reference manual:');
78 $output->writeln(' https://github.com/bobthecow/psysh/wiki/PHP-manual');
79 } else {
80 $output->writeln($doc);
81 }
82 });
83
84 // Set some magic local variables
85 $this->setCommandScopeVariables($reflector);
86 }
87
88 private function getManualDoc($reflector)
89 {
90 switch (get_class($reflector)) {
91 case 'ReflectionClass':
92 case 'ReflectionObject':
93 case 'ReflectionFunction':
94 $id = $reflector->name;
95 break;
96
97 case 'ReflectionMethod':
98 $id = $reflector->class . '::' . $reflector->name;
99 break;
100
101 case 'ReflectionProperty':
102 $id = $reflector->class . '::$' . $reflector->name;
103 break;
104
105 case 'ReflectionClassConstant':
106 case 'Psy\Reflection\ReflectionClassConstant':
107 // @todo this is going to collide with ReflectionMethod ids
108 // someday... start running the query by id + type if the DB
109 // supports it.
110 $id = $reflector->class . '::' . $reflector->name;
111 break;
112
113 case 'Psy\Reflection\ReflectionConstant_':
114 $id = $reflector->name;
115 break;
116
117 default:
118 return false;
119 }
120
121 return $this->getManualDocById($id);
122 }
123
124 private function getManualDocById($id)
125 {
126 if ($db = $this->getApplication()->getManualDb()) {
127 return $db
128 ->query(sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)))
129 ->fetchColumn(0);
130 }
131 }
132 }