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