Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / DocCommand.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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\Reflection\ReflectionLanguageConstruct;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20
21 /**
22  * Read the documentation for an object, class, constant, method or property.
23  */
24 class DocCommand extends ReflectingCommand
25 {
26     /**
27      * {@inheritdoc}
28      */
29     protected function configure()
30     {
31         $this
32             ->setName('doc')
33             ->setAliases(array('rtfm', 'man'))
34             ->setDefinition(array(
35                 new InputArgument('value', InputArgument::REQUIRED, 'Function, class, instance, constant, method or property to document.'),
36             ))
37             ->setDescription('Read the documentation for an object, class, constant, method or property.')
38             ->setHelp(
39                 <<<HELP
40 Read the documentation for an object, class, constant, method or property.
41
42 It's awesome for well-documented code, not quite as awesome for poorly documented code.
43
44 e.g.
45 <return>>>> doc preg_replace</return>
46 <return>>>> doc Psy\Shell</return>
47 <return>>>> doc Psy\Shell::debug</return>
48 <return>>>> \$s = new Psy\Shell</return>
49 <return>>>> doc \$s->run</return>
50 HELP
51             );
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function execute(InputInterface $input, OutputInterface $output)
58     {
59         $value = $input->getArgument('value');
60         if (ReflectionLanguageConstruct::isLanguageConstruct($value)) {
61             $reflector = new ReflectionLanguageConstruct($value);
62             $doc = $this->getManualDocById($value);
63         } else {
64             list($target, $reflector) = $this->getTargetAndReflector($value);
65             $doc = $this->getManualDoc($reflector) ?: DocblockFormatter::format($reflector);
66         }
67
68         $db = $this->getApplication()->getManualDb();
69
70         $output->page(function ($output) use ($reflector, $doc, $db) {
71             $output->writeln(SignatureFormatter::format($reflector));
72             $output->writeln('');
73
74             if (empty($doc) && !$db) {
75                 $output->writeln('<warning>PHP manual not found</warning>');
76                 $output->writeln('    To document core PHP functionality, download the PHP reference manual:');
77                 $output->writeln('    https://github.com/bobthecow/dotfiles/wiki/PHP-manual');
78             } else {
79                 $output->writeln($doc);
80             }
81         });
82
83         // Set some magic local variables
84         $this->setCommandScopeVariables($reflector);
85     }
86
87     private function getManualDoc($reflector)
88     {
89         switch (get_class($reflector)) {
90             case 'ReflectionFunction':
91                 $id = $reflector->name;
92                 break;
93
94             case 'ReflectionMethod':
95                 $id = $reflector->class . '::' . $reflector->name;
96                 break;
97
98             default:
99                 return false;
100         }
101
102         return $this->getManualDocById($id);
103     }
104
105     private function getManualDocById($id)
106     {
107         if ($db = $this->getApplication()->getManualDb()) {
108             return $db
109                 ->query(sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)))
110                 ->fetchColumn(0);
111         }
112     }
113 }