annotate vendor/psy/psysh/src/Command/DocCommand.php @ 4:a9cd425dd02b

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