Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 16 additions & 5 deletions src/Bridges/SymfonyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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);
};
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionContainer/Argument/LiteralArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/BaseMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public function has($id): bool

public function remove($alias)
{
$this->symfonyContainer->removeAlias($alias);
parent::remove($alias);
}
}
2 changes: 1 addition & 1 deletion src/Traits/ContainerPersistenceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/src/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions tests/src/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,64 @@
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
* @package Nip\Tests\Container
*/
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()
{
Expand Down
Loading