refactor reporting runner to event subscriber

This commit is contained in:
TomasVotruba 2020-05-26 14:45:32 +02:00
parent ebb8e882fb
commit 21f67be9c3
5 changed files with 35 additions and 57 deletions

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Extension\Contract;
interface ReportingExtensionInterface
{
public function run(): void;
}

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Extension\Runner;
use Rector\Extension\Contract\ReportingExtensionInterface;
/**
* @todo refactor to event system
*/
final class ReportingExtensionRunner
{
/**
* @var ReportingExtensionInterface[]
*/
private $reportingExtensions = [];
/**
* @param ReportingExtensionInterface[] $reportingExtensions
*/
public function __construct(array $reportingExtensions = [])
{
$this->reportingExtensions = $reportingExtensions;
}
public function run(): void
{
foreach ($this->reportingExtensions as $reportingExtension) {
$reportingExtension->run();
}
}
}

View File

@ -2,15 +2,16 @@
declare(strict_types=1);
namespace Rector\Reporting\Extension;
namespace Rector\Reporting\EventSibscriber;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Configuration\Configuration;
use Rector\Extension\Contract\ReportingExtensionInterface;
use Rector\Core\EventDispatcher\Event\AfterReportEvent;
use Rector\Reporting\DataCollector\ReportCollector;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class GenericReportMessageReportingExtension implements ReportingExtensionInterface
final class PrintReportCollectorEventSubscriber implements EventSubscriberInterface
{
/**
* @var ReportCollector
@ -37,7 +38,15 @@ final class GenericReportMessageReportingExtension implements ReportingExtension
$this->symfonyStyle = $symfonyStyle;
}
public function run(): void
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [AfterReportEvent::class => 'printReportCollector'];
}
public function printReportCollector(): void
{
if ($this->shouldSkip()) {
return;

View File

@ -13,17 +13,18 @@ use Rector\Core\Autoloading\AdditionalAutoloader;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\Output\OutputFormatterCollector;
use Rector\Core\EventDispatcher\Event\AfterReportEvent;
use Rector\Core\FileSystem\FilesFinder;
use Rector\Core\Guard\RectorGuard;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Core\Stubs\StubLoader;
use Rector\Core\Yaml\YamlProcessor;
use Rector\Extension\Runner\ReportingExtensionRunner;
use Symfony\Component\Console\Input\InputArgument;
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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -65,11 +66,6 @@ final class ProcessCommand extends AbstractCommand
*/
private $outputFormatterCollector;
/**
* @var ReportingExtensionRunner
*/
private $reportingExtensionRunner;
/**
* @var RectorNodeTraverser
*/
@ -100,6 +96,11 @@ final class ProcessCommand extends AbstractCommand
*/
private $symfonyStyle;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct(
FilesFinder $phpFilesFinder,
AdditionalAutoloader $additionalAutoloader,
@ -108,13 +109,13 @@ final class ProcessCommand extends AbstractCommand
Configuration $configuration,
RectorApplication $rectorApplication,
OutputFormatterCollector $outputFormatterCollector,
ReportingExtensionRunner $reportingExtensionRunner,
RectorNodeTraverser $rectorNodeTraverser,
StubLoader $stubLoader,
YamlProcessor $yamlProcessor,
ChangedFilesDetector $changedFilesDetector,
UnchangedFilesFilter $unchangedFilesFilter,
SymfonyStyle $symfonyStyle
SymfonyStyle $symfonyStyle,
EventDispatcherInterface $eventDispatcher
) {
$this->filesFinder = $phpFilesFinder;
$this->additionalAutoloader = $additionalAutoloader;
@ -123,7 +124,6 @@ final class ProcessCommand extends AbstractCommand
$this->configuration = $configuration;
$this->rectorApplication = $rectorApplication;
$this->outputFormatterCollector = $outputFormatterCollector;
$this->reportingExtensionRunner = $reportingExtensionRunner;
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->stubLoader = $stubLoader;
$this->yamlProcessor = $yamlProcessor;
@ -133,6 +133,7 @@ final class ProcessCommand extends AbstractCommand
$this->changedFilesDetector = $changedFilesDetector;
$this->symfonyStyle = $symfonyStyle;
$this->eventDispatcher = $eventDispatcher;
}
protected function configure(): void
@ -234,7 +235,7 @@ final class ProcessCommand extends AbstractCommand
$outputFormatter = $this->outputFormatterCollector->getByName($outputFormat);
$outputFormatter->report($this->errorAndDiffCollector);
$this->reportingExtensionRunner->run();
$this->eventDispatcher->dispatch(new AfterReportEvent());
// invalidate affected files
$this->invalidateAffectedCacheFiles();

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Rector\Core\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;
final class AfterReportEvent extends Event
{
}