diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..6daa5db --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,34 @@ +name: Tests + +on: + push: + pull_request: + +jobs: + phpunit: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - php: '8.1' + symfony: '5.4.*' + config: '5.4.*' + - php: '8.1' + symfony: '6.4.*' + config: '6.4.*' + - php: '8.2' + symfony: '7.4.*' + config: '7.4.*' + - php: '8.4' + symfony: '8.0.*' + config: '7.4.*' + steps: + - uses: actions/checkout@v4 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + - run: composer update --no-interaction --prefer-dist --with "symfony/dependency-injection:${{ matrix.symfony }}" --with "symfony/config:${{ matrix.config }}" + - run: vendor/bin/phpunit diff --git a/README.md b/README.md index 392812b..2d02222 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,37 @@ [![StyleCI](https://styleci.io/repos/118474281/shield?branch=master)](https://styleci.io/repos/118474281) [![Total Downloads](https://img.shields.io/packagist/dt/bytic/container.svg?style=flat-square)](https://packagist.org/packages/bytic/container) +## Symfony integration + +The package supports PHP 8.1+ and Symfony DependencyInjection 5.3 through 8.x. +Symfony 8 requires PHP 8.4 or newer. Existing `new Container()`, service +providers, aliases and reflection-based resolution continue to work. + +For new services, configure a Symfony `ContainerBuilder` directly and pass it +to the compatibility container: + +```php +use Nip\Container\Container; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +$builder = new ContainerBuilder(); +$builder->register(MyService::class)->setAutowired(true)->setPublic(true); +$container = new Container($builder); + +// Configure and compile the builder with Symfony's normal APIs when needed. +$builder->compile(); +$service = $container->get(MyService::class); +``` + +`getSymfonyContainer()` exposes the underlying Symfony container; +`setSymfonyContainer()` remains available for existing integrations. A +Symfony container can also be supplied to the constructor. Symfony definitions, +aliases and compiler passes should be configured on a `ContainerBuilder` before +compilation. Symfony's normal visibility and compiled-container restrictions +apply. The legacy providers, reflection fallback and value wrapping belong to +the compatibility container and are not available through the Symfony +container directly. + #### Inspiration * https://github.com/container-interop/service-provider * https://github.com/thecodingmachine/service-provider-bridge-bundle/blob/1.0/src/ServiceProviderCompilationPass.php \ No newline at end of file diff --git a/composer.json b/composer.json index e9c3f7f..1de3ee9 100644 --- a/composer.json +++ b/composer.json @@ -30,12 +30,16 @@ }, "require": { "php": "^8.1", - "symfony/dependency-injection": "^5.3|^6.0|^7.0" + "bytic/utility": "^1.0", + "psr/container": "^1.1|^2.0", + "ramsey/collection": "^1.2|^2.0", + "symfony/config": "^5.3|^6.0|^7.0|^8.0", + "symfony/dependency-injection": "^5.3|^6.0|^7.0|^8.0" }, "require-dev": { "bytic/config": "^0.9|^1.0|^2.0", - "bytic/utility": "^1.0", - "bytic/phpqatools": "^1.0" + "mockery/mockery": "^1.6", + "phpunit/phpunit": "^10.5" }, "prefer-stable": true, "minimum-stability": "dev", diff --git a/src/Bridges/SymfonyContainer.php b/src/Bridges/SymfonyContainer.php index e843228..4c1fd51 100644 --- a/src/Bridges/SymfonyContainer.php +++ b/src/Bridges/SymfonyContainer.php @@ -8,6 +8,7 @@ use Nip\Container\Utility\BuildSymfonyContainer; use Nip\Utility\Stringable; use Ramsey\Collection\GenericArray; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; @@ -22,9 +23,9 @@ abstract class SymfonyContainer implements \Symfony\Component\DependencyInjecti protected $symfonyContainer; - public function __construct() + public function __construct(?ContainerInterface $container = null) { - $this->symfonyContainer = BuildSymfonyContainer::create(); + $this->symfonyContainer = $container ?? BuildSymfonyContainer::create(); } public function __call($name, $arguments) @@ -37,6 +38,11 @@ public function setSymfonyContainer(ContainerInterface $container): void $this->symfonyContainer = $container; } + public function getSymfonyContainer(): ContainerInterface + { + return $this->symfonyContainer; + } + /** * @param string $alias * @param $concrete @@ -57,7 +63,7 @@ public function alias($abstract, $alias) if ($abstract === null) { return; } - if ($this->symfonyContainer->isCompiled()) { + if (!$this->symfonyContainer instanceof ContainerBuilder || $this->symfonyContainer->isCompiled()) { $abstract = function () use ($abstract) { return $this->get($abstract); }; @@ -74,6 +80,11 @@ public function alias($abstract, $alias) public function remove($alias) { + if (!$this->symfonyContainer instanceof ContainerBuilder) { + $this->symfonyContainer->set($alias, null); + return; + } + $this->symfonyContainer->removeAlias($alias); } @@ -119,7 +130,7 @@ public function set(string $id, mixed $service): void { if (is_string($service)) { if (class_exists($service)) { - if ($this->symfonyContainer->isCompiled()) { + if (!$this->symfonyContainer instanceof ContainerBuilder || $this->symfonyContainer->isCompiled()) { $abstract = function () use ($service) { return $this->get($service); }; @@ -158,7 +169,7 @@ public function hasParameter(string $name): bool return $this->symfonyContainer->hasParameter($name); } - public function setParameter(string $name, UnitEnum|float|array|bool|int|string|null $value): void + public function setParameter(string $name, $value): void { $this->symfonyContainer->setParameter($name, $value); } diff --git a/src/Container.php b/src/Container.php index 8a3237c..6883f54 100644 --- a/src/Container.php +++ b/src/Container.php @@ -9,6 +9,7 @@ use Nip\Container\ReflectionContainer\ReflectionContainer; use Nip\Container\ServiceProviders\ProviderRepository; use Nip\Container\ServiceProviders\ServiceProviderAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; /** * Class Container @@ -23,9 +24,9 @@ class Container extends SymfonyContainer implements ContainerInterface use ServiceProviderAwareTrait; use DeprecatedMethodsTrait; - public function __construct() + public function __construct(?SymfonyContainerInterface $container = null) { - parent::__construct(); + parent::__construct($container); $providerContainer = new ServiceProvidersContainer(); $providerContainer->setContainer($this); diff --git a/src/ReflectionContainer/Argument/LiteralArgument.php b/src/ReflectionContainer/Argument/LiteralArgument.php index 326dc2e..4168282 100644 --- a/src/ReflectionContainer/Argument/LiteralArgument.php +++ b/src/ReflectionContainer/Argument/LiteralArgument.php @@ -24,7 +24,7 @@ class LiteralArgument implements LiteralArgumentInterface */ protected $value; - public function __construct($value, string $type = null) + public function __construct($value, ?string $type = null) { if ( null === $type diff --git a/src/Traits/BaseMethodsTrait.php b/src/Traits/BaseMethodsTrait.php index 5ead7dd..03d6467 100644 --- a/src/Traits/BaseMethodsTrait.php +++ b/src/Traits/BaseMethodsTrait.php @@ -50,6 +50,6 @@ public function has($id): bool public function remove($alias) { - $this->symfonyContainer->removeAlias($alias); + parent::remove($alias); } } \ No newline at end of file diff --git a/src/Traits/ContainerPersistenceTrait.php b/src/Traits/ContainerPersistenceTrait.php index 9410d49..313b726 100644 --- a/src/Traits/ContainerPersistenceTrait.php +++ b/src/Traits/ContainerPersistenceTrait.php @@ -37,7 +37,7 @@ public static function getInstance() * @param ContainerInterface|null $container * @return null|ContainerInterface */ - public static function setInstance(ContainerInterface $container = null) + public static function setInstance(?ContainerInterface $container = null) { return static::$instance = $container; } diff --git a/tests/src/AbstractTestCase.php b/tests/src/AbstractTestCase.php index 7f981fe..fa8d552 100644 --- a/tests/src/AbstractTestCase.php +++ b/tests/src/AbstractTestCase.php @@ -3,10 +3,9 @@ namespace Nip\Container\Tests; -use Bytic\Phpqa\PHPUnit\TestCase; use Mockery; -use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Nip\Container\Container; +use PHPUnit\Framework\TestCase; /** * Class AbstractTest diff --git a/tests/src/ContainerTest.php b/tests/src/ContainerTest.php index 08e27b8..c204a48 100644 --- a/tests/src/ContainerTest.php +++ b/tests/src/ContainerTest.php @@ -7,6 +7,9 @@ use Nip\Container\Tests\Fixtures\DemoServiceProvider; use Nip\Container\Tests\Fixtures\ModulesService; use Nip\Container\Utility\BuildSymfonyContainer; +use Symfony\Component\DependencyInjection\Container as NativeContainer; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; /** * Class ContainerTest @@ -14,6 +17,54 @@ */ class ContainerTest extends AbstractTestCase { + public function testSymfonyBuilderCanBeConfiguredAndCompiledDirectly(): void + { + $builder = new ContainerBuilder(); + $container = new Container($builder); + self::assertSame($builder, $container->getSymfonyContainer()); + + $builder->register(ModulesService::class, ModulesService::class)->setPublic(true); + $builder->register(Fixtures\ObjectHasConstructor::class, Fixtures\ObjectHasConstructor::class) + ->setArgument('$modulesService', new Reference(ModulesService::class)) + ->setPublic(true); + $builder->setAlias('modules', ModulesService::class)->setPublic(true); + $builder->setParameter('example', 'value'); + $builder->compile(); + + self::assertSame($builder->get(ModulesService::class), $container->get('modules')); + self::assertSame($container->get('modules'), $container->get(Fixtures\ObjectHasConstructor::class)->modulesService); + self::assertSame('value', $container->getParameter('example')); + $container->addServiceProvider(DemoServiceProvider::class); + self::assertEquals('value', $container->get(DemoServiceProvider::DUMMY_CONSTANT)); + } + + public function testNativeContainerAndLegacyAliasesCanBeUsedTogether(): void + { + $native = new NativeContainer(); + $container = new Container($native); + $native->set('native', $service = new \stdClass()); + + $container->alias('native', 'legacy-alias'); + $container->set('class-service', ModulesService::class); + + self::assertSame($service, $container->get('legacy-alias')); + self::assertInstanceOf(ModulesService::class, $container->get('class-service')); + self::assertSame($native, $container->getSymfonyContainer()); + $container->remove('legacy-alias'); + self::assertFalse($container->has('legacy-alias')); + self::assertSame($service, $container->get('native')); + } + + public function testSymfonyContainerCanStillBeReplaced(): void + { + $container = new Container(); + $native = new NativeContainer(); + $native->set('native', $service = new \stdClass()); + $container->setSymfonyContainer($native); + + self::assertSame($native, $container->getSymfonyContainer()); + self::assertSame($service, $container->get('native')); + } public function testSetsAndGetServiceShared() {