mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
add dump-nodes output formatters
This commit is contained in:
parent
62fb154906
commit
ad5f09cb24
@ -93,6 +93,7 @@ use PhpParser\Node\Stmt\While_;
|
||||
use PhpParser\Node\VarLikeIdentifier;
|
||||
use Rector\Console\Command\AbstractCommand;
|
||||
use Rector\Console\Shell;
|
||||
use Rector\ContributorTools\Contract\OutputFormatter\DumpNodesOutputFormatterInterface;
|
||||
use Rector\ContributorTools\Node\NodeClassProvider;
|
||||
use Rector\ContributorTools\Node\NodeInfo;
|
||||
use Rector\ContributorTools\Node\NodeInfoResult;
|
||||
@ -100,8 +101,8 @@ use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
||||
use ReflectionClass;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symplify\PackageBuilder\Console\Command\CommandNaming;
|
||||
|
||||
final class DumpNodesCommand extends AbstractCommand
|
||||
@ -116,26 +117,29 @@ final class DumpNodesCommand extends AbstractCommand
|
||||
*/
|
||||
private $nodeClassProvider;
|
||||
|
||||
/**
|
||||
* @var SymfonyStyle
|
||||
*/
|
||||
private $symfonyStyle;
|
||||
|
||||
/**
|
||||
* @var NodeInfoResult
|
||||
*/
|
||||
private $nodeInfoResult;
|
||||
|
||||
/**
|
||||
* @var DumpNodesOutputFormatterInterface[]
|
||||
*/
|
||||
private $outputFormatters = [];
|
||||
|
||||
/**
|
||||
* @param DumpNodesOutputFormatterInterface[] $outputFormatters
|
||||
*/
|
||||
public function __construct(
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
NodeClassProvider $nodeClassProvider,
|
||||
SymfonyStyle $symfonyStyle,
|
||||
NodeInfoResult $nodeInfoResult
|
||||
NodeInfoResult $nodeInfoResult,
|
||||
array $outputFormatters
|
||||
) {
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->nodeClassProvider = $nodeClassProvider;
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->nodeInfoResult = $nodeInfoResult;
|
||||
$this->outputFormatters = $outputFormatters;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
@ -144,6 +148,13 @@ final class DumpNodesCommand extends AbstractCommand
|
||||
{
|
||||
$this->setName(CommandNaming::classToName(self::class));
|
||||
$this->setDescription('Dump overview of all Nodes');
|
||||
$this->addOption(
|
||||
'output-format',
|
||||
'o',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Output format for Nodes [json, console]',
|
||||
'console'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
@ -383,6 +394,7 @@ final class DumpNodesCommand extends AbstractCommand
|
||||
}
|
||||
|
||||
$category = $this->resolveCategoryByNodeClass($nodeClass);
|
||||
|
||||
$this->nodeInfoResult->addNodeInfo($category, new NodeInfo(
|
||||
$nodeClass,
|
||||
$this->betterStandardPrinter->print($node),
|
||||
@ -394,35 +406,17 @@ final class DumpNodesCommand extends AbstractCommand
|
||||
}
|
||||
}
|
||||
|
||||
$this->printInfoTable($this->nodeInfoResult);
|
||||
foreach ($this->outputFormatters as $outputFormatter) {
|
||||
if ($outputFormatter->getName() !== $input->getOption('output-format')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$outputFormatter->format($this->nodeInfoResult);
|
||||
}
|
||||
|
||||
return Shell::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
private function printInfoTable(NodeInfoResult $nodeInfoResult): void
|
||||
{
|
||||
foreach ($nodeInfoResult as $category => $nodeInfos) {
|
||||
if (class_exists($category)) {
|
||||
$this->symfonyStyle->title(sprintf(' Children of "%s"', $category));
|
||||
} else {
|
||||
$this->symfonyStyle->title(' ' . $category);
|
||||
}
|
||||
|
||||
$tableData = [];
|
||||
foreach ($nodeInfos as $nodeInfo) {
|
||||
$tableData[] = [
|
||||
/** @var NodeInfo $nodeInfo */
|
||||
$nodeInfo->getClass(),
|
||||
$nodeInfo->getPrintedContent(),
|
||||
$nodeInfo->hasRequiredArguments() ? 'yes' : 'no',
|
||||
];
|
||||
}
|
||||
|
||||
$this->symfonyStyle->table(['Node class', 'Content', 'Needs Args'], $tableData);
|
||||
$this->symfonyStyle->newLine();
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveCategoryByNodeClass(string $nodeClass): string
|
||||
{
|
||||
if (Strings::contains($nodeClass, '\\Scalar\\')) {
|
||||
|
@ -37,7 +37,7 @@ final class DumpRectorsCommand extends AbstractCommand
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setName(CommandNaming::classToName(self::class));
|
||||
$this->setDescription('Dump overview of all Rectors in desired format');
|
||||
$this->setDescription('Dump overview of all Rectors');
|
||||
$this->addOption(
|
||||
'output-format',
|
||||
'o',
|
||||
|
@ -0,0 +1,12 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\ContributorTools\Contract\OutputFormatter;
|
||||
|
||||
use Rector\ContributorTools\Node\NodeInfoResult;
|
||||
|
||||
interface DumpNodesOutputFormatterInterface
|
||||
{
|
||||
public function getName(): string;
|
||||
|
||||
public function format(NodeInfoResult $nodeInfoResult): void;
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\ContributorTools\OutputFormatter\DumpNodes;
|
||||
|
||||
use Rector\ContributorTools\Contract\OutputFormatter\DumpNodesOutputFormatterInterface;
|
||||
use Rector\ContributorTools\Node\NodeInfo;
|
||||
use Rector\ContributorTools\Node\NodeInfoResult;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class ConsoleDumpNodesOutputFormatter implements DumpNodesOutputFormatterInterface
|
||||
{
|
||||
/**
|
||||
* @var SymfonyStyle
|
||||
*/
|
||||
private $symfonyStyle;
|
||||
|
||||
public function __construct(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'console';
|
||||
}
|
||||
|
||||
public function format(NodeInfoResult $nodeInfoResult): void
|
||||
{
|
||||
foreach ($nodeInfoResult->getNodeInfos() as $category => $nodeInfos) {
|
||||
if (class_exists($category)) {
|
||||
$this->symfonyStyle->title(sprintf(' Children of "%s"', $category));
|
||||
} else {
|
||||
$this->symfonyStyle->title(' ' . $category);
|
||||
}
|
||||
|
||||
$tableData = [];
|
||||
foreach ($nodeInfos as $nodeInfo) {
|
||||
$tableData[] = [
|
||||
/** @var NodeInfo $nodeInfo */
|
||||
$nodeInfo->getClass(),
|
||||
$nodeInfo->getPrintedContent(),
|
||||
$nodeInfo->hasRequiredArguments() ? 'yes' : 'no',
|
||||
];
|
||||
}
|
||||
|
||||
$this->symfonyStyle->table(['Node class', 'Content', 'Needs Args'], $tableData);
|
||||
$this->symfonyStyle->newLine();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\ContributorTools\OutputFormatter\DumpNodes;
|
||||
|
||||
use Nette\Utils\Json;
|
||||
use Rector\ContributorTools\Contract\OutputFormatter\DumpNodesOutputFormatterInterface;
|
||||
use Rector\ContributorTools\Node\NodeInfo;
|
||||
use Rector\ContributorTools\Node\NodeInfoResult;
|
||||
|
||||
final class JsonDumpNodesOutputFormatter implements DumpNodesOutputFormatterInterface
|
||||
{
|
||||
public function getName(): string
|
||||
{
|
||||
return 'json';
|
||||
}
|
||||
|
||||
public function format(NodeInfoResult $nodeInfoResult): void
|
||||
{
|
||||
$nodeCategoryData = [];
|
||||
|
||||
foreach ($nodeInfoResult->getNodeInfos() as $category => $nodeInfos) {
|
||||
$nodeData = [];
|
||||
|
||||
foreach ($nodeInfos as $nodeInfo) {
|
||||
$nodeData[] = [
|
||||
/** @var NodeInfo $nodeInfo */
|
||||
'class' => $nodeInfo->getClass(),
|
||||
'printed_content' => $nodeInfo->getPrintedContent(),
|
||||
'has_required_arguments' => $nodeInfo->hasRequiredArguments(),
|
||||
];
|
||||
}
|
||||
|
||||
$nodeCategoryData[] = [
|
||||
'category' => $category,
|
||||
'nodes' => $nodeData,
|
||||
];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'node_categories' => $nodeCategoryData,
|
||||
];
|
||||
|
||||
echo Json::encode($data, Json::PRETTY);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user