-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Use OutputFormatter for show command (#147)
- Loading branch information
1 parent
4f33f3f
commit ac7666a
Showing
6 changed files
with
145 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/ListReporting/Contract/Output/ShowOutputFormatterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
|
||
namespace Rector\ListReporting\Contract\Output; | ||
|
||
|
||
use Rector\Core\Contract\Rector\RectorInterface; | ||
|
||
interface ShowOutputFormatterInterface | ||
{ | ||
/** | ||
* @param RectorInterface[] $rectors | ||
*/ | ||
public function list(array $rectors): void; | ||
|
||
public function getName(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
|
||
namespace Rector\ListReporting\Output; | ||
|
||
|
||
use Rector\Core\Contract\Console\OutputStyleInterface; | ||
use Rector\ListReporting\Contract\Output\ShowOutputFormatterInterface; | ||
|
||
final class ConsoleOutputFormatter implements ShowOutputFormatterInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const NAME = 'console'; | ||
|
||
public function __construct(private OutputStyleInterface $outputStyle) | ||
{ | ||
} | ||
|
||
public function list(array $rectors): void | ||
{ | ||
$rectorCount = count($rectors); | ||
$this->outputStyle->title('Loaded Rector rules'); | ||
|
||
foreach ($rectors as $rector) { | ||
$this->outputStyle->writeln(' * ' . get_class($rector)); | ||
} | ||
|
||
$message = sprintf('%d loaded Rectors', $rectorCount); | ||
$this->outputStyle->success($message); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return self::NAME; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
|
||
namespace Rector\Core\Console\Output; | ||
|
||
|
||
use Rector\ListReporting\Contract\Output\ShowOutputFormatterInterface; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
|
||
final class ShowOutputFormatterCollector | ||
{ | ||
/** | ||
* @var ShowOutputFormatterInterface[] | ||
*/ | ||
private array $outputFormatters = []; | ||
|
||
/** | ||
* @param ShowOutputFormatterInterface[] $showOutputFormatters | ||
*/ | ||
public function __construct(array $showOutputFormatters) | ||
{ | ||
foreach ($showOutputFormatters as $showOutputFormatter) { | ||
$this->outputFormatters[$showOutputFormatter->getName()] = $showOutputFormatter; | ||
} | ||
} | ||
|
||
public function getByName(string $name): ShowOutputFormatterInterface | ||
{ | ||
$this->ensureOutputFormatExists($name); | ||
|
||
return $this->outputFormatters[$name]; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNames(): array | ||
{ | ||
return array_keys($this->outputFormatters); | ||
} | ||
|
||
private function ensureOutputFormatExists(string $name): void | ||
{ | ||
if (isset($this->outputFormatters[$name])) { | ||
return; | ||
} | ||
|
||
throw new InvalidConfigurationException(sprintf( | ||
'Output formatter "%s" was not found. Pick one of "%s".', | ||
$name, | ||
implode('", "', $this->getNames()) | ||
)); | ||
} | ||
} |