Skip to content

Commit

Permalink
Merge pull request #29 from symplify/tv-di-trim
Browse files Browse the repository at this point in the history
[DI] Add ConfigTransformerContainerFactory to drop http-kernel dependency
  • Loading branch information
TomasVotruba authored Dec 16, 2023
2 parents 6265583 + d0edc1d commit df0d63c
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 51 deletions.
12 changes: 6 additions & 6 deletions bin/config-transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel;
use Symplify\ConfigTransformer\Console\ConfigTransformerApplication;
use Symplify\ConfigTransformer\Kernel\ConfigTransformerContainerFactory;

$possibleAutoloadPaths = [
// dependency
Expand All @@ -27,12 +28,11 @@
require_once $scoperAutoloadFilepath;
}

$configTransformerKernel = new ConfigTransformerKernel();
$configTransformerKernel->boot();
$configTransformerContainerFactory = new ConfigTransformerContainerFactory();
$container = $configTransformerContainerFactory->create();

$container = $configTransformerKernel->getContainer();

$configTransformerApplication = $container->get(\Symplify\ConfigTransformer\Console\ConfigTransformerApplication::class);
/** @var ConfigTransformerApplication $configTransformerApplication */
$configTransformerApplication = $container->get(ConfigTransformerApplication::class);

$input = new ArgvInput();
$output = new ConsoleOutput();
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"symfony/expression-language": "^6.4",
"symfony/filesystem": "^6.4",
"symfony/finder": "^6.4",
"symfony/http-kernel": "^6.1",
"symfony/yaml": "^6.4",
"symplify/php-config-printer": "^11.3.6",
"webmozart/assert": "^1.11"
Expand Down Expand Up @@ -58,6 +57,13 @@
},
"enable-patching": true
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-intl-grapheme": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*"
},
"scripts": {
"check-cs": "vendor/bin/ecs check --ansi",
"fix-cs": "vendor/bin/ecs check --fix --ansi",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
level: 8

paths:
- bin
- src
- config
- tests
Expand Down
5 changes: 3 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
PHPUnitSetList::PHPUNIT_100,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_81,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::NAMING,
SetList::PRIVATIZATION,
SetList::EARLY_RETURN,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
]);

$rectorConfig->paths([
__DIR__ . '/bin',
__DIR__ . '/config',
__DIR__ . '/src',
__DIR__ . '/tests',
Expand Down
5 changes: 2 additions & 3 deletions src/Console/Command/SwitchFormatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$configuration = $this->configurationFactory->createFromInput($input);

$totalFileCount = $this->getTotalFileCount($configuration);

$fileInfos = $this->configFileFinder->findFileInfos($configuration->getSources());
Expand All @@ -70,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$convertedContent = new ConvertedContent($convertedFileContent, $fileInfo);

$this->configFileDumper->dumpFile($convertedContent, $configuration);
$this->removeFileInfo($configuration, $fileInfo);
$this->removeOriginalYamlFileInfo($configuration, $fileInfo);

$this->symfonyStyle->newLine();
}
Expand All @@ -89,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

private function removeFileInfo(Configuration $configuration, SplFileInfo $fileInfo): void
private function removeOriginalYamlFileInfo(Configuration $configuration, SplFileInfo $fileInfo): void
{
// only dry run, nothing to remove
if ($configuration->isDryRun()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Mimics @see \Symfony\Component\DependencyInjection\MergeExtensionConfigurationPass without dependency on
* symfony/http-kernel
*/
final class LoadExtensionConfigsCompilerPass extends MergeExtensionConfigurationPass
{
public function process(ContainerBuilder $containerBuilder): void
{
$extensionNames = array_keys($containerBuilder->getExtensions());

foreach ($extensionNames as $extensionName) {
$containerBuilder->loadFromExtension($extensionName, []);
}

parent::process($containerBuilder);
}
}
53 changes: 53 additions & 0 deletions src/DependencyInjection/Loader/ParameterMergingPhpFileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\DependencyInjection\Loader;

use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symplify\ConfigTransformer\Utils\ParametersMerger;

/**
* @api
*
* The need:
* - https://github.com/symfony/symfony/issues/26713
* - https://github.com/symfony/symfony/pull/21313#issuecomment-372037445
*
* @property ContainerBuilder $container
*/
final class ParameterMergingPhpFileLoader extends PhpFileLoader
{
private readonly ParametersMerger $parametersMerger;

public function __construct(ContainerBuilder $containerBuilder, FileLocatorInterface $fileLocator)
{
$this->parametersMerger = new ParametersMerger();
parent::__construct($containerBuilder, $fileLocator);
}

/**
* Same as parent, just merging parameters instead overriding them
*
* @see https://github.com/symplify/symplify/pull/697
*/
public function load(mixed $resource, string $type = null): mixed
{
// get old parameters
$parameterBag = $this->container->getParameterBag();
$oldParameters = $parameterBag->all();

parent::load($resource);

foreach ($oldParameters as $key => $oldValue) {
$currentParameterValue = $this->container->getParameter($key);
$newValue = $this->parametersMerger->merge($oldValue, $currentParameterValue);

$this->container->setParameter($key, $newValue);
}

return null;
}
}
51 changes: 51 additions & 0 deletions src/Kernel/ConfigTransformerContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\Kernel;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\GlobFileLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symplify\ConfigTransformer\DependencyInjection\Compiler\LoadExtensionConfigsCompilerPass;
use Symplify\ConfigTransformer\DependencyInjection\Loader\ParameterMergingPhpFileLoader;
use Symplify\PhpConfigPrinter\ValueObject\PhpConfigPrinterConfig;

/**
* @api used in tests and bin
*/
final class ConfigTransformerContainerFactory
{
public function create(): ContainerInterface
{
$containerBuilder = new ContainerBuilder();

$delegatingLoader = $this->createDelegatingLoader($containerBuilder, getcwd());
$delegatingLoader->load(__DIR__ . '/../../config/config.php');
$delegatingLoader->load(PhpConfigPrinterConfig::FILE_PATH);

$compilerPassConfig = $containerBuilder->getCompilerPassConfig();
$compilerPassConfig->setMergePass(new LoadExtensionConfigsCompilerPass());

$containerBuilder->compile();

return $containerBuilder;
}

private function createDelegatingLoader(ContainerBuilder $containerBuilder, string $currentWorkingDirectory): DelegatingLoader
{
$fileLocator = new FileLocator([$currentWorkingDirectory]);

$loaders = [
new GlobFileLoader($fileLocator),
new ParameterMergingPhpFileLoader($containerBuilder, $fileLocator),
];

$loaderResolver = new LoaderResolver($loaders);

return new DelegatingLoader($loaderResolver);
}
}
31 changes: 0 additions & 31 deletions src/Kernel/ConfigTransformerKernel.php

This file was deleted.

60 changes: 60 additions & 0 deletions src/Utils/ParametersMerger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\Utils;

final class ParametersMerger
{
/**
* Merges configurations. Left has higher priority than right one.
*
* @autor David Grudl (https://davidgrudl.com)
* @source https://github.com/nette/di/blob/8eb90721a131262f17663e50aee0032a62d0ef08/src/DI/Config/Helpers.php#L31
*/
public function merge(mixed $left, mixed $right): mixed
{
if (\is_array($left) && \is_array($right)) {
return $this->mergeLeftToRightWithCallable(
$left,
$right,
fn (mixed $leftValue, mixed $rightValue): mixed => $this->merge($leftValue, $rightValue)
);
}

if ($left !== null) {
return $left;
}

if (! \is_array($right)) {
return $left;
}

return $right;
}

/**
* @param array<int|string, mixed> $left
* @param array<int|string, mixed> $right
* @return mixed[]
*/
private function mergeLeftToRightWithCallable(array $left, array $right, callable $mergeCallback): array
{
foreach ($left as $key => $val) {
if (\is_int($key)) {
// prevent duplicated values in unindexed arrays
if (! \in_array($val, $right, \true)) {
$right[] = $val;
}
} else {
if (isset($right[$key])) {
$val = $mergeCallback($val, $right[$key]);
}

$right[$key] = $val;
}
}

return $right;
}
}
8 changes: 3 additions & 5 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel;
use Symplify\ConfigTransformer\Kernel\ConfigTransformerContainerFactory;

abstract class AbstractTestCase extends TestCase
{
private ContainerInterface $container;

protected function setUp(): void
{
$configTransformerKernel = new ConfigTransformerKernel();
$configTransformerKernel->boot();

$this->container = $configTransformerKernel->getContainer();
$configTransformerContainerFactory = new ConfigTransformerContainerFactory();
$this->container = $configTransformerContainerFactory->create();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ services:
declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->alias(EventDispatcherInterface::class, EventDispatcher::class);
$services->alias('Symfony\Component\EventDispatcher\EventDispatcherInterface', 'Symfony\Component\EventDispatcher\EventDispatcher');
};

0 comments on commit df0d63c

Please sign in to comment.