Skip to content

Commit

Permalink
refactor: Pass progress display as parameter
Browse files Browse the repository at this point in the history
- Instead of injecting progress display to the application, inject the
  display to the processor
  • Loading branch information
MontealegreLuis committed Jul 9, 2021
1 parent 3773f14 commit 1f61024
Show file tree
Hide file tree
Showing 25 changed files with 101 additions and 276 deletions.
8 changes: 2 additions & 6 deletions bin/phuml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ if (!$installed) {
}

use PhUml\Console\PhUmlApplication;
use PhUml\Console\ProgressDisplay;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

$output = new ConsoleOutput();
$application = new PhUmlApplication(new ProgressDisplay($output));
$application->run(new ArgvInput(), $output);
$application = new PhUmlApplication();
$application->run();
7 changes: 4 additions & 3 deletions src/Console/Commands/GenerateClassDiagramCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use PhUml\Configuration\ClassDiagramConfiguration;
use PhUml\Configuration\DigraphBuilder;
use PhUml\Configuration\DigraphConfiguration;
use PhUml\Console\ConsoleProgressDisplay;
use PhUml\Generators\ClassDiagramGenerator;
use PhUml\Processors\DotProcessor;
use PhUml\Processors\NeatoProcessor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -34,7 +36,7 @@
*
* @see WithDigraphConfiguration::addDigraphOptions() for more details about the rest of the options
*/
final class GenerateClassDiagramCommand extends GeneratorCommand
final class GenerateClassDiagramCommand extends Command
{
use WithDigraphConfiguration;

Expand Down Expand Up @@ -91,9 +93,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$builder->digraphProcessor(),
$configuration->isDotProcessor() ? new DotProcessor() : new NeatoProcessor()
);
$classDiagramGenerator->attach($this->display);

$classDiagramGenerator->generate($codeFinder, $classDiagramPath);
$classDiagramGenerator->generate($codeFinder, $classDiagramPath, new ConsoleProgressDisplay($output));

return self::SUCCESS;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Console/Commands/GenerateDotFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

use PhUml\Configuration\DigraphBuilder;
use PhUml\Configuration\DigraphConfiguration;
use PhUml\Console\ConsoleProgressDisplay;
use PhUml\Generators\DotFileGenerator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -25,7 +27,7 @@
*
* @see WithDigraphConfiguration::addDigraphOptions() for more details about all the available options
*/
final class GenerateDotFileCommand extends GeneratorCommand
final class GenerateDotFileCommand extends Command
{
use WithDigraphConfiguration;

Expand Down Expand Up @@ -67,11 +69,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$builder = new DigraphBuilder(new DigraphConfiguration($generatorInput->options()));

$dotFileGenerator = new DotFileGenerator($builder->codeParser(), $builder->digraphProcessor());
$dotFileGenerator->attach($this->display);

$codeFinder = $builder->codeFinder($codebasePath);

$dotFileGenerator->generate($codeFinder, $dotFilePath);
$dotFileGenerator->generate($codeFinder, $dotFilePath, new ConsoleProgressDisplay($output));

return self::SUCCESS;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Console/Commands/GenerateStatisticsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace PhUml\Console\Commands;

use PhUml\Console\ConsoleProgressDisplay;
use PhUml\Generators\StatisticsGenerator;
use PhUml\Parser\Code\PhpCodeParser;
use PhUml\Parser\CodeParser;
use PhUml\Processors\StatisticsProcessor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -29,7 +31,7 @@
*
* 1. `recursive`. If present it will look recursively within the `directory` provided
*/
final class GenerateStatisticsCommand extends GeneratorCommand
final class GenerateStatisticsCommand extends Command
{
/**
* @throws InvalidArgumentException
Expand Down Expand Up @@ -73,11 +75,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$statisticsFilePath = $statisticsInput->outputFile();

$statisticsGenerator = new StatisticsGenerator(new CodeParser(new PhpCodeParser()), new StatisticsProcessor());
$statisticsGenerator->attach($this->display);

$codeFinder = $statisticsInput->codeFinder();

$statisticsGenerator->generate($codeFinder, $statisticsFilePath);
$statisticsGenerator->generate($codeFinder, $statisticsFilePath, new ConsoleProgressDisplay($output));

return self::SUCCESS;
}
Expand Down
27 changes: 0 additions & 27 deletions src/Console/Commands/GeneratorCommand.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace PhUml\Console;

use PhUml\Generators\ProcessorProgressDisplay;
use PhUml\Generators\ProgressDisplay;
use PhUml\Processors\Processor;
use Symfony\Component\Console\Output\OutputInterface;

/**
* It provides visual feedback to the use about the progress of the current command
*
* @see ProcessorProgressDisplay for more details about the things that are reported by this display
* @see ProgressDisplay for more details about the things that are reported by this display
*/
final class ProgressDisplay implements ProcessorProgressDisplay
final class ConsoleProgressDisplay implements ProgressDisplay
{
private OutputInterface $output;

Expand Down
8 changes: 4 additions & 4 deletions src/Console/PhUmlApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
final class PhUmlApplication extends Application
{
public function __construct(ProgressDisplay $display)
public function __construct()
{
// This will be replaced by Box with a version number if it's a PHAR
// 1.6.1 for instance
Expand All @@ -34,8 +34,8 @@ public function __construct(ProgressDisplay $display)
$version = '1.6-dev';
}
parent::__construct('phUML', $version);
$this->add(new GenerateClassDiagramCommand($display));
$this->add(new GenerateStatisticsCommand($display));
$this->add(new GenerateDotFileCommand($display));
$this->add(new GenerateClassDiagramCommand());
$this->add(new GenerateStatisticsCommand());
$this->add(new GenerateDotFileCommand());
}
}
22 changes: 10 additions & 12 deletions src/Generators/ClassDiagramGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace PhUml\Generators;

use LogicException;
use PhUml\Parser\CodeFinder;
use PhUml\Parser\CodeParser;
use PhUml\Processors\GraphvizProcessor;
Expand Down Expand Up @@ -36,24 +35,23 @@ public function __construct(
/**
* The process to generate a class diagram is as follows
*
* 1. The parser produces a collection of classes and interfaces
* 1. The parser produces a collection of classes, interfaces and traits
* 2. The `graphviz` processor takes this collection and creates a digraph using the DOT language
* 3. Either the `neato` or `dot` will produce a `.png` class diagram from the digraph
* 3. Either the `neato` or `dot` processor will produce a `.png` class diagram from the digraph
* 4. The image is saved to the given path
*
* @throws LogicException If either the image processor or the command are missing
*/
public function generate(CodeFinder $finder, OutputFilePath $imagePath): void
public function generate(CodeFinder $finder, OutputFilePath $imagePath, ProgressDisplay $display): void
{
$this->display()->start();
$image = $this->generateClassDiagram($this->generateDigraph($this->parseCode($finder)));
$this->save($this->imageProcessor, $image, $imagePath);
$display->start();
$codebase = $this->parseCode($finder, $display);
$digraph = $this->generateDigraph($codebase, $display);
$image = $this->generateClassDiagram($digraph, $display);
$this->save($this->imageProcessor, $image, $imagePath, $display);
}

/** @throws LogicException If no command or image processor is provided */
private function generateClassDiagram(OutputContent $digraph): OutputContent
private function generateClassDiagram(OutputContent $digraph, ProgressDisplay $display): OutputContent
{
$this->display()->runningProcessor($this->imageProcessor);
$display->runningProcessor($this->imageProcessor);
return $this->imageProcessor->process($digraph);
}
}
4 changes: 2 additions & 2 deletions src/Generators/DigraphGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function __construct(CodeParser $parser, GraphvizProcessor $digraphProces
$this->digraphProcessor = $digraphProcessor;
}

protected function generateDigraph(Codebase $codebase): OutputContent
protected function generateDigraph(Codebase $codebase, ProgressDisplay $display): OutputContent
{
$this->display()->runningProcessor($this->digraphProcessor);
$display->runningProcessor($this->digraphProcessor);
return $this->digraphProcessor->process($codebase);
}
}
12 changes: 5 additions & 7 deletions src/Generators/DotFileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace PhUml\Generators;

use LogicException;
use PhUml\Parser\CodeFinder;
use PhUml\Processors\OutputFilePath;

Expand All @@ -30,15 +29,14 @@ final class DotFileGenerator extends DigraphGenerator
* 1. The parser produces a collection of classes and interfaces
* 2. The `graphviz` processor takes this collection and creates a digraph using the DOT language
* 4. The DOT file is saved to the given path
*
* @throws LogicException If the command is missing
*/
public function generate(CodeFinder $finder, OutputFilePath $dotFilePath): void
public function generate(CodeFinder $finder, OutputFilePath $dotFilePath, ProgressDisplay $display): void
{
$this->display()->start();
$display->start();

$digraph = $this->generateDigraph($this->parseCode($finder));
$codebase = $this->parseCode($finder, $display);
$digraph = $this->generateDigraph($codebase, $display);

$this->save($this->digraphProcessor, $digraph, $dotFilePath);
$this->save($this->digraphProcessor, $digraph, $dotFilePath, $display);
}
}
36 changes: 10 additions & 26 deletions src/Generators/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace PhUml\Generators;

use LogicException;
use PhUml\Code\Codebase;
use PhUml\Parser\CodeFinder;
use PhUml\Parser\CodeParser;
Expand All @@ -19,45 +18,30 @@
* All generators will see the console commands as listeners that will provide feedback
* to the end users about their progress
*
* @see ProcessorProgressDisplay for the details about the events that are tracked
* @see ProgressDisplay for the details about the events that are tracked
*/
abstract class Generator
{
private ?ProcessorProgressDisplay $display = null;

private CodeParser $parser;

public function __construct(CodeParser $parser)
{
$this->parser = $parser;
}

public function attach(ProcessorProgressDisplay $display): void
{
$this->display = $display;
}

/** @throws LogicException */
protected function display(): ProcessorProgressDisplay
protected function parseCode(CodeFinder $finder, ProgressDisplay $display): Codebase
{
if ($this->display === null) {
throw new LogicException('No display was attached');
}
return $this->display;
}

/**
* @throws LogicException If the command is missing
*/
protected function parseCode(CodeFinder $finder): Codebase
{
$this->display()->runningParser();
$display->runningParser();
return $this->parser->parse($finder);
}

protected function save(Processor $processor, OutputContent $content, OutputFilePath $path): void
{
$this->display()->savingResult();
protected function save(
Processor $processor,
OutputContent $content,
OutputFilePath $path,
ProgressDisplay $display
): void {
$display->savingResult();
$processor->saveToFile($content, $path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use PhUml\Processors\Processor;

/**
* Listener for the Action classes. It provides feedback before:
* It provides feedback before:
*
* - Running the parser
* - Running a processor
* - Saving the results produced by the processors
*/
interface ProcessorProgressDisplay
interface ProgressDisplay
{
public function start(): void;

Expand Down
15 changes: 7 additions & 8 deletions src/Generators/StatisticsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace PhUml\Generators;

use LogicException;
use PhUml\Code\Codebase;
use PhUml\Parser\CodeFinder;
use PhUml\Parser\CodeParser;
Expand Down Expand Up @@ -40,18 +39,18 @@ public function __construct(CodeParser $parser, StatisticsProcessor $statisticsP
* 4. The text file with the statistics is saved to the given path
*
* @throws TemplateFailure If Twig fails
* @throws LogicException If the command is missing
*/
public function generate(CodeFinder $finder, OutputFilePath $statisticsFilePath): void
public function generate(CodeFinder $finder, OutputFilePath $statisticsFilePath, ProgressDisplay $display): void
{
$this->display()->start();
$statistics = $this->generateStatistics($this->parseCode($finder));
$this->save($this->statisticsProcessor, $statistics, $statisticsFilePath);
$display->start();
$codebase = $this->parseCode($finder, $display);
$statistics = $this->generateStatistics($codebase, $display);
$this->save($this->statisticsProcessor, $statistics, $statisticsFilePath, $display);
}

private function generateStatistics(Codebase $codebase): OutputContent
private function generateStatistics(Codebase $codebase, ProgressDisplay $display): OutputContent
{
$this->display()->runningProcessor($this->statisticsProcessor);
$display->runningProcessor($this->statisticsProcessor);
return $this->statisticsProcessor->process($codebase);
}
}
Loading

0 comments on commit 1f61024

Please sign in to comment.