4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
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;
22 * Read the documentation for an object, class, constant, method or property.
24 class DocCommand extends ReflectingCommand
29 protected function configure()
33 ->setAliases(array('rtfm', 'man'))
34 ->setDefinition(array(
35 new InputArgument('value', InputArgument::REQUIRED, 'Function, class, instance, constant, method or property to document.'),
37 ->setDescription('Read the documentation for an object, class, constant, method or property.')
40 Read the documentation for an object, class, constant, method or property.
42 It's awesome for well-documented code, not quite as awesome for poorly documented code.
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>
57 protected function execute(InputInterface $input, OutputInterface $output)
59 $value = $input->getArgument('value');
60 if (ReflectionLanguageConstruct::isLanguageConstruct($value)) {
61 $reflector = new ReflectionLanguageConstruct($value);
62 $doc = $this->getManualDocById($value);
64 list($target, $reflector) = $this->getTargetAndReflector($value);
65 $doc = $this->getManualDoc($reflector) ?: DocblockFormatter::format($reflector);
68 $db = $this->getApplication()->getManualDb();
70 $output->page(function ($output) use ($reflector, $doc, $db) {
71 $output->writeln(SignatureFormatter::format($reflector));
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');
79 $output->writeln($doc);
83 // Set some magic local variables
84 $this->setCommandScopeVariables($reflector);
87 private function getManualDoc($reflector)
89 switch (get_class($reflector)) {
90 case 'ReflectionFunction':
91 $id = $reflector->name;
94 case 'ReflectionMethod':
95 $id = $reflector->class . '::' . $reflector->name;
102 return $this->getManualDocById($id);
105 private function getManualDocById($id)
107 if ($db = $this->getApplication()->getManualDb()) {
109 ->query(sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)))