-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from symplify/tv-di-trim
[DI] Add ConfigTransformerContainerFactory to drop http-kernel dependency
- Loading branch information
Showing
12 changed files
with
213 additions
and
51 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ parameters: | |
level: 8 | ||
|
||
paths: | ||
- bin | ||
- src | ||
- config | ||
- tests | ||
|
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
26 changes: 26 additions & 0 deletions
26
src/DependencyInjection/Compiler/LoadExtensionConfigsCompilerPass.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,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
53
src/DependencyInjection/Loader/ParameterMergingPhpFileLoader.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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} |
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