/*
* This file is part of Psy Shell.
*
- * (c) 2012-2017 Justin Hileman
+ * (c) 2012-2018 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
use PhpParser\Node;
use PhpParser\Parser;
+use Psy\Context;
+use Psy\ContextAware;
use Psy\Input\CodeArgument;
use Psy\ParserFactory;
use Psy\VarDumper\Presenter;
use Psy\VarDumper\PresenterAware;
-use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Parse PHP code and show the abstract syntax tree.
*/
-class ParseCommand extends Command implements PresenterAware
+class ParseCommand extends Command implements ContextAware, PresenterAware
{
+ /**
+ * Context instance (for ContextAware interface).
+ *
+ * @var Context
+ */
+ protected $context;
+
private $presenter;
private $parserFactory;
private $parsers;
public function __construct($name = null)
{
$this->parserFactory = new ParserFactory();
- $this->parsers = array();
+ $this->parsers = [];
parent::__construct($name);
}
+ /**
+ * ContextAware interface.
+ *
+ * @param Context $context
+ */
+ public function setContext(Context $context)
+ {
+ $this->context = $context;
+ }
+
/**
* PresenterAware interface.
*
public function setPresenter(Presenter $presenter)
{
$this->presenter = clone $presenter;
- $this->presenter->addCasters(array(
+ $this->presenter->addCasters([
'PhpParser\Node' => function (Node $node, array $a) {
- $a = array(
+ $a = [
Caster::PREFIX_VIRTUAL . 'type' => $node->getType(),
Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
- );
+ ];
foreach ($node->getSubNodeNames() as $name) {
$a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
return $a;
},
- ));
+ ]);
}
/**
*/
protected function configure()
{
- $definition = array(
- new CodeArgument('code', InputArgument::REQUIRED, 'PHP code to parse.'),
- new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse', 10),
- );
+ $definition = [
+ new CodeArgument('code', CodeArgument::REQUIRED, 'PHP code to parse.'),
+ new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10),
+ ];
if ($this->parserFactory->hasKindsSupport()) {
$msg = 'One of PhpParser\\ParserFactory constants: '
. implode(', ', ParserFactory::getPossibleKinds())
- . " (default is based on current interpreter's version)";
+ . " (default is based on current interpreter's version).";
$defaultKind = $this->parserFactory->getDefaultKind();
$definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
$depth = $input->getOption('depth');
$nodes = $this->parse($this->getParser($parserKind), $code);
$output->page($this->presenter->present($nodes, $depth));
+
+ $this->context->setReturnValue($nodes);
}
/**