Skip to content

Commit

Permalink
Reorganize input/output responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosas committed Jan 19, 2020
1 parent 0838d4b commit a6fa811
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 52 deletions.
24 changes: 18 additions & 6 deletions phpat
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ declare(strict_types=1);

use PhpAT\App\Provider;
use PhpAT\Input\ArgvInput;
use PhpAT\Output\StdOutput;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Yaml;

try {
$autoload = is_file(__DIR__.'/../../autoload.php')
? __DIR__.'/../../autoload.php'
: __DIR__.'/vendor/autoload.php';
require $autoload;

$input = new ArgvInput();
$isDryRun = $input->getOption('dry-run', false);
$input = new ArgvInput($_SERVER['argv']);
$file = $input->getArgument('config-file', 'phpat.yaml');
$file = file_exists($file) ? $file : 'phpat.yml';
if (!(file_exists($file))) {
throw new \LogicException('Create a configuration file `phpat.yaml` first.');
}
$config = Yaml::parse(file_get_contents($file));
$config['options'] = array_merge($config['options'] ?? [], $input->getOptions());

$isDryRun = (bool) ($config['options']['dry-run'] ?? false);
$errStream = $isDryRun ? STDOUT : STDERR;
$errCode = $isDryRun ? 0 : 1;
$verbosity = (int) ($config['options']['verbosity'] ?? 1);

if (PHP_VERSION_ID < 70200) {
fwrite($errStream, 'Required at least PHP version 7.2.0 but your version is '.PHP_VERSION.PHP_EOL);
Expand All @@ -28,7 +39,8 @@ try {
gc_disable();

$container = new ContainerBuilder();
$provider = new Provider($container, $input);
$output = new StdOutput($verbosity, $isDryRun);
$provider = new Provider($container, $config, $output);
$container = $provider->register();

$app = $container->get('app');
Expand All @@ -38,18 +50,18 @@ try {
$redBgWhiteText = "\033[41m\033[1;37m";
$formattingReset = "\033[0m";

fwrite($errStream, sprintf(
fwrite($errStream ?? STDERR, sprintf(
"%s\n\n\tAn error occurred while running phpat :(
Please consider opening an issue: http://github.com/carlosas/phpat/issues\n%s\n",
$redBgWhiteText,
$formattingReset
));
do {
fwrite($errStream, sprintf(
fwrite($errStream ?? STDERR, sprintf(
"\n%s\n\n%s",
$e->getMessage(),
$e->getTraceAsString()
));
} while ($e = $e->getPrevious());
exit($errCode);
exit($errCode ?? 1);
}
23 changes: 14 additions & 9 deletions src/App/ListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,50 @@
class ListenerProvider
{
private $builder;
/**
* @var OutputInterface
*/
private $output;

public function __construct(ContainerBuilder $builder)
public function __construct(ContainerBuilder $builder, OutputInterface $output)
{
$this->builder = $builder;
$this->output = $output;
}

public function register(): ContainerBuilder
{
$this->builder
->register(SuiteStartListener::class, SuiteStartListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(SuiteEndListener::class, SuiteEndListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(WarningListener::class, WarningListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(FatalErrorListener::class, FatalErrorListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(RuleValidationStartListener::class, RuleValidationStartListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(RuleValidationEndListener::class, RuleValidationEndListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(StatementValidListener::class, StatementValidListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

$this->builder
->register(StatementNotValidListener::class, StatementNotValidListener::class)
->addArgument(new Reference(OutputInterface::class));
->addArgument($this->output);

return $this->builder;
}
Expand Down
38 changes: 8 additions & 30 deletions src/App/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
use PHPAT\EventDispatcher\ListenerProvider;
use PhpAT\File\FileFinder;
use PhpAT\File\SymfonyFinderAdapter;
use PhpAT\Input\InputInterface;
use PhpAT\Output\OutputInterface;
use PhpAT\Output\StdOutput;
use PhpAT\Parser\MapBuilder;
use PhpAT\Rule\RuleBuilder;
use PhpAT\Rule\Type\Composition;
Expand All @@ -32,7 +30,6 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;

/**
* Class Provider
Expand All @@ -47,29 +44,28 @@ class Provider
private $builder;

/**
* @var array
* @var OutputInterface
*/
private $config;
private $output;

/**
* Provider constructor.
*
* @param ContainerBuilder $builder
* @param InputInterface $input
* @param array $config
* @param OutputInterface $output
*/
public function __construct(ContainerBuilder $builder, InputInterface $input)
public function __construct(ContainerBuilder $builder, array $config, OutputInterface $output)
{
Configuration::init($config);
$this->builder = $builder;
$this->config = Yaml::parse($this->getConfigFilePath($input));
$this->config['options'] = array_merge($this->config['options'] ?? [], $input->getOptions());
$this->output = $output;
}

/**
* @return ContainerBuilder
*/
public function register(): ContainerBuilder
{
Configuration::init($this->config);
$this->builder->set(Parser::class, (new ParserFactory())->create(ParserFactory::ONLY_PHP7));
$phpDocParser = new PhpDocParser(new TypeParser(), new ConstExprParser());
$this->builder->set(Parser::class, (new ParserFactory())->create(ParserFactory::ONLY_PHP7));
Expand All @@ -93,9 +89,6 @@ public function register(): ContainerBuilder
->addArgument(new Reference(NodeTraverserInterface::class))
->addArgument($phpDocParser);

$this->builder
->register(OutputInterface::class, StdOutput::class);

$this->builder
->register(RuleBuilder::class, RuleBuilder::class)
->addArgument($this->builder);
Expand Down Expand Up @@ -165,24 +158,9 @@ public function register(): ContainerBuilder
->addArgument(new Reference(StatementBuilder::class))
->addArgument(new Reference(EventDispatcher::class));

$listenerProvider = new \PhpAT\App\ListenerProvider($this->builder);
$listenerProvider = new \PhpAT\App\ListenerProvider($this->builder, $this->output);
$this->builder->merge($listenerProvider->register());

return $this->builder;
}

private function getConfigFilePath(InputInterface $input): string
{
$path = getcwd() . '/' . ($input->getArgument('config-file', 'phpat.yaml'));
if (file_exists($path)) {
return file_get_contents($path);
}

$path = getcwd() . '/phpat.yml';
if (file_exists($path)) {
return file_get_contents($path);
}

throw new \LogicException('Create a configuration file `phpat.yaml` first.');
}
}
4 changes: 2 additions & 2 deletions src/Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class ArgvInput implements InputInterface

private $currentArgument = 0;

public function __construct()
public function __construct(array $args)
{
$this->args = $_SERVER['argv'];
$this->args = $args;
$this->parse();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Input/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ interface InputInterface
{
public function getArgument(string $name, $default = null): ?string;

public function getOption(string $name, $default = null);

public function getOptions(): array;
}
8 changes: 3 additions & 5 deletions src/Output/StdOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace PhpAT\Output;

use PhpAT\App\Configuration;

class StdOutput implements OutputInterface
{
/**
Expand All @@ -20,10 +18,10 @@ class StdOutput implements OutputInterface
*/
private $verbose;

public function __construct()
public function __construct(int $verbosity, bool $dryRun)
{
$this->verbose = Configuration::getVerbosity();
$this->errStream = Configuration::getDryRun() ? \STDOUT : \STDERR;
$this->verbose = $verbosity;
$this->errStream = $dryRun ? \STDOUT : \STDERR;
}

public function write(string $message, int $level = OutputLevel::DEFAULT): void
Expand Down

0 comments on commit a6fa811

Please sign in to comment.