Skip to content

Commit

Permalink
Merge pull request #11 from andreaswolf/task/use-container-configurator
Browse files Browse the repository at this point in the history
[TASK] Simplify container configuration
  • Loading branch information
sabbelasichon authored Apr 18, 2024
2 parents 1622c54 + fa0fdb3 commit 3285e97
Show file tree
Hide file tree
Showing 57 changed files with 764 additions and 488 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.phpunit.result.cache
run-test.sh
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,19 @@ To utilize Fractor effectively, follow these steps:
```

2. **Configuration**:
- Create a PHP configuration file (e.g., `fractor.php`) where you define the rules to be applied.
- At minimum, a configuration file must specify the paths to process, the file processor(s) to use and the rule(s) to apply:
- Create a PHP configuration file (e.g., `fractor.php`) where you define the paths to your files.
- At minimum, a configuration file must specify the paths to process:
```php
<?php

use a9f\Fractor\Configuration\FractorConfig;
use a9f\Fractor\Fractor\DummyRule;
use a9f\FractorXml\XmlFileProcessor;
use a9f\Typo3Fractor\Rules\FlexForm\AddRenderTypeToFlexFormFractor;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (FractorConfig $config) {
$config->import(__DIR__ . '/vendor/a9f/fractor-xml/config/fractor.php');

$config->setPaths([
__DIR__ . '/output/',
]);

$config->withFileProcessor(XmlFileProcessor::class);

$config->withRule(AddRenderTypeToFlexFormFractor::class);
$config->withRule(DummyRule::class);
return static function (ContainerConfigurator $containerConfigurator) {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [__DIR__ . '/output/']);
};
```

Expand Down
3 changes: 2 additions & 1 deletion fractor-xml/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
/composer.lock
/composer.lock
.phpunit.cache
2 changes: 1 addition & 1 deletion fractor-xml/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
"analyze:php": "phpstan analyze",
"style:php:check": "ecs",
"style:php:fix": "ecs --fix",
"test:php": "phpunit tests/"
"test:php": "phpunit"
}
}
10 changes: 9 additions & 1 deletion fractor-xml/config/application.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?php

use a9f\FractorXml\Contract\XmlFractor;
use a9f\FractorXml\XmlFileProcessor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

return static function (ContainerConfigurator $containerConfigurator): void {
return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void {
$services = $containerConfigurator->services();
$services->defaults()
->autowire()
->autoconfigure();

$services->load('a9f\\FractorXml\\', __DIR__ . '/../src/');

$services->set(XmlFileProcessor::class)->arg('$rules', tagged_iterator('fractor.xml_rule'));

$containerBuilder->registerForAutoconfiguration(XmlFractor::class)->addTag('fractor.xml_rule');
};
8 changes: 0 additions & 8 deletions fractor-xml/config/fractor.php

This file was deleted.

13 changes: 13 additions & 0 deletions fractor-xml/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="fractor-xml">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
3 changes: 1 addition & 2 deletions fractor-xml/src/AbstractXmlFractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace a9f\FractorXml;

use a9f\Fractor\Contract\FractorRule;
use a9f\FractorXml\Contract\DomNodeVisitor;
use a9f\FractorXml\Contract\XmlFractor;

abstract class AbstractXmlFractor implements DomNodeVisitor, XmlFractor, FractorRule
abstract class AbstractXmlFractor implements DomNodeVisitor, XmlFractor
{
public function beforeTraversal(\DOMNode $rootNode): void
{
Expand Down
34 changes: 0 additions & 34 deletions fractor-xml/src/DependencyInjection/XmlFractorCompilerPass.php

This file was deleted.

2 changes: 1 addition & 1 deletion fractor-xml/src/DomDocumentIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class DomDocumentIterator
/**
* @param list<DomNodeVisitor> $visitors
*/
public function __construct(private readonly array $visitors)
public function __construct(private readonly iterable $visitors)
{
}

Expand Down
21 changes: 16 additions & 5 deletions fractor-xml/src/XmlFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace a9f\FractorXml;

use a9f\Fractor\Contract\FileProcessor;
use a9f\Fractor\ValueObject\File;
use a9f\FractorXml\Contract\XmlFractor;

final class XmlFileProcessor implements FileProcessor
{
/**
* @param list<XmlFractor> $rules
*/
public function __construct(private readonly array $rules)
public function __construct(private readonly iterable $rules)
{
}

Expand All @@ -19,18 +20,28 @@ public function canHandle(\SplFileInfo $file): bool
return $file->getExtension() === 'xml';
}

public function handle(\SplFileInfo $file): void
public function handle(File $file): void
{
$doc = new \DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->load($file->getPathname());
$doc->load($file->getFilePath());

// TODO we need a way to detect if there were changes (and probably also collect changes here)
$iterator = new DomDocumentIterator($this->rules);
$iterator->traverseDocument($doc);

// TODO only update file if changed
file_put_contents($file->getPathname(), $doc->saveXML());
$newFileContent = $doc->saveXML();

if ($newFileContent === false) {
throw new \UnexpectedValueException('New file content should be a string');
}

$file->changeFileContent($newFileContent);
}

public function allowedFileExtensions(): array
{
return ['xml'];
}
}
3 changes: 2 additions & 1 deletion fractor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
/composer.lock
/composer.lock
.phpunit.cache
4 changes: 2 additions & 2 deletions fractor/bin/fractor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use a9f\Fractor\Configuration\ConfigResolver;
use a9f\Fractor\DependencyInjection\ContainerBuilder;
use a9f\Fractor\DependencyInjection\ContainerContainerBuilder;
use a9f\Fractor\FractorApplication;
use Symfony\Component\Console\Input\ArgvInput;

Expand All @@ -26,7 +26,7 @@

$configFile = ConfigResolver::resolveConfigsFromInput(new ArgvInput());

$container = (new ContainerBuilder())->createDependencyInjectionContainer($configFile);
$container = (new ContainerContainerBuilder())->createDependencyInjectionContainer([$configFile]);

/** @var FractorApplication $application */
$application = $container->get(FractorApplication::class);
Expand Down
7 changes: 5 additions & 2 deletions fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
],
"require": {
"php": "^8.2",
"nette/utils": "^4.0",
"symfony/config": "^6.4",
"symfony/console": "^6.4",
"symfony/dependency-injection": "^6.4",
"symfony/filesystem": "^6.4",
"symfony/finder": "^6.4"
"symfony/finder": "^6.4",
"webmozart/assert": "^1.11"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10.5",
"symplify/easy-coding-standard": "^12.1"
},
Expand All @@ -47,6 +50,6 @@
"analyze:php": "phpstan analyze",
"style:php:check": "ecs",
"style:php:fix": "ecs --fix",
"test:php": "phpunit tests/"
"test:php": "phpunit"
}
}
43 changes: 32 additions & 11 deletions fractor/config/application.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
<?php

use a9f\Fractor\Configuration\FractorConfig;
use a9f\Fractor\FileSystem\FileFinder;
use a9f\Fractor\Configuration\Option;
use a9f\Fractor\Contract\FileProcessor;
use a9f\Fractor\Factory\ConfigurationFactory;
use a9f\Fractor\Fractor\FractorRunner;
use a9f\Fractor\FractorApplication;
use a9f\Fractor\ValueObject\Configuration;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

return static function (ContainerConfigurator $containerConfigurator): void {
return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [__DIR__]);
$services = $containerConfigurator->services();
$services->defaults()
->autowire()
->public()
->autoconfigure();

$services->load('a9f\\Fractor\\', __DIR__ . '/../src/')
->exclude('../src/Configuration/');
->exclude(
[
__DIR__ . '/../src/Configuration',
__DIR__ . '/../src/Console',
__DIR__ . '/../src/ValueObject',
__DIR__ . '/../src/Testing',
]
);

$services->set(FractorApplication::class)
->public();
$services->set('parameter_bag', ContainerBag::class)
->args([
service('service_container'),
])
->alias(ContainerBagInterface::class, 'parameter_bag')
->alias(ParameterBagInterface::class, 'parameter_bag');

$services->set(FileFinder::class);
$services->set(FractorRunner::class);
$services->set(Configuration::class)->factory([service(ConfigurationFactory::class), 'create']);
$services->set(FractorRunner::class)->arg('$processors', tagged_iterator('fractor.file_processor'));
$services->set(ConfigurationFactory::class)->arg('$processors', tagged_iterator('fractor.file_processor'));

$services->set(FractorConfig::class)
->lazy(true);
$containerBuilder->registerForAutoconfiguration(FileProcessor::class)->addTag('fractor.file_processor');
};
13 changes: 13 additions & 0 deletions fractor/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="fractor">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
12 changes: 5 additions & 7 deletions fractor/src/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace a9f\Fractor\Command;

use a9f\Fractor\Configuration\FractorConfig;
use a9f\Fractor\Console\SymfonyConsoleOutput;
use a9f\Fractor\Fractor\FractorRunner;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -13,15 +13,13 @@
#[AsCommand('process', 'Runs Fractor with the given configuration file')]
class ProcessCommand extends Command
{
public function __construct(private readonly FractorConfig $config, private readonly FractorRunner $runner)
public function __construct(private readonly FractorRunner $runner)
{
parent::__construct();
}

protected function configure()
protected function configure(): void
{
parent::configure();

$this->addOption(
'config',
'c',
Expand All @@ -32,8 +30,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->runner->run($this->config);
$this->runner->run(new SymfonyConsoleOutput($output));

return 0;
return Command::SUCCESS;
}
}
Loading

0 comments on commit 3285e97

Please sign in to comment.