Version 1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ShowCommand.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\Configuration;
15 use Psy\Exception\RuntimeException;
16 use Psy\Formatter\CodeFormatter;
17 use Psy\Formatter\SignatureFormatter;
18 use Psy\Output\ShellOutput;
19 use Symfony\Component\Console\Input\InputArgument;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Output\OutputInterface;
22
23 /**
24  * Show the code for an object, class, constant, method or property.
25  */
26 class ShowCommand extends ReflectingCommand
27 {
28     private $colorMode;
29
30     /**
31      * @param null|string $colorMode (default: null)
32      */
33     public function __construct($colorMode = null)
34     {
35         $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO;
36
37         return parent::__construct();
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     protected function configure()
44     {
45         $this
46             ->setName('show')
47             ->setDefinition(array(
48                 new InputArgument('value', InputArgument::REQUIRED, 'Function, class, instance, constant, method or property to show.'),
49             ))
50             ->setDescription('Show the code for an object, class, constant, method or property.')
51             ->setHelp(
52                 <<<HELP
53 Show the code for an object, class, constant, method or property.
54
55 e.g.
56 <return>>>> show \$myObject</return>
57 <return>>>> show Psy\Shell::debug</return>
58 HELP
59             );
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     protected function execute(InputInterface $input, OutputInterface $output)
66     {
67         list($value, $reflector) = $this->getTargetAndReflector($input->getArgument('value'));
68
69         // Set some magic local variables
70         $this->setCommandScopeVariables($reflector);
71
72         try {
73             $output->page(CodeFormatter::format($reflector, $this->colorMode), ShellOutput::OUTPUT_RAW);
74         } catch (RuntimeException $e) {
75             $output->writeln(SignatureFormatter::format($reflector));
76             throw $e;
77         }
78     }
79 }