Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR-11 friendly kernels #21

Closed
wants to merge 4 commits into from
Closed
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
49 changes: 49 additions & 0 deletions src/KernelWithPhpDiContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* PHP-DI
*
* @link http://php-di.org/
* @copyright Matthieu Napoli (http://mnapoli.fr/)
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/

namespace DI\Bridge\Symfony;

use Psr\Container\ContainerInterface;

/**
* Customization of Symfony's kernel to setup PHP-DI.
*
* Extend this class instead of Symfony's base kernel.
*
* @author Matthieu Napoli <[email protected]>
*/
abstract class KernelWithPhpDiContainer extends KernelWithPsr11Container
{
/**
* @var ContainerInterface
*/
private $phpdiContainer;

/**
* Implement this method to configure PHP-DI.
*
* @return ContainerInterface
*/
abstract protected function buildPHPDIContainer(\DI\ContainerBuilder $builder);

/**
* @return ContainerInterface
*/
protected function getFallbackContainer()
{
if ($this->phpdiContainer === null) {
$builder = new \DI\ContainerBuilder();
$builder->wrapContainer($this->getContainer());

$this->phpdiContainer = $this->buildPHPDIContainer($builder);
}

return $this->phpdiContainer;
}
}
32 changes: 5 additions & 27 deletions src/Kernel.php → src/KernelWithPsr11Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,20 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Customization of Symfony's kernel to setup PHP-DI.
* Customization of Symfony's kernel to setup any PSR-11 container.
*
* Extend this class instead of Symfony's base kernel.
*
* @author Matthieu Napoli <[email protected]>
* @author Julien Janvier <[email protected]>
*/
abstract class Kernel extends \Symfony\Component\HttpKernel\Kernel
abstract class KernelWithPsr11Container extends \Symfony\Component\HttpKernel\Kernel
{
/**
* @var ContainerInterface
*/
private $phpdiContainer;

public function __construct($environment, $debug)
{
parent::__construct($environment, $debug);
$this->disableDebugClassLoader();
}

/**
* Implement this method to configure PHP-DI.
*
* @return ContainerInterface
*/
abstract protected function buildPHPDIContainer(\DI\ContainerBuilder $builder);

protected function getContainerBaseClass()
{
return SymfonyContainerBridge::class;
Expand All @@ -62,7 +50,7 @@ protected function initializeContainer()
/** @var SymfonyContainerBridge $rootContainer */
$rootContainer = $this->getContainer();

$rootContainer->setFallbackContainer($this->getPHPDIContainer());
$rootContainer->setFallbackContainer($this->getFallbackContainer());
}

/**
Expand Down Expand Up @@ -102,15 +90,5 @@ private function disableDebugClassLoader()
/**
* @return ContainerInterface
*/
protected function getPHPDIContainer()
{
if ($this->phpdiContainer === null) {
$builder = new \DI\ContainerBuilder();
$builder->wrapContainer($this->getContainer());

$this->phpdiContainer = $this->buildPHPDIContainer($builder);
}

return $this->phpdiContainer;
}
abstract protected function getFallbackContainer();
}
7 changes: 5 additions & 2 deletions src/SymfonyContainerBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

namespace DI\Bridge\Symfony;

use DI\NotFoundException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
Expand Down Expand Up @@ -85,10 +86,12 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
}

return $entry;
} catch (NotFoundException $e) {
} catch (NotFoundExceptionInterface $e) {
if ($invalidBehavior === self::EXCEPTION_ON_INVALID_REFERENCE) {
throw new ServiceNotFoundException($id, null, $e);
}
} catch (ContainerExceptionInterface $e) {
throw new \Exception('Error while retrieving the entry.', $e->getCode(), $e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why catch a more specific exception to rethrow it with a less precise one? You could just let the original exception bubble (by not catching it)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because here you don't really respect Symfony's contract (which itself doesn't respect Psr/Container's contract).
Symfony's contract expects ServiceCircularReferenceException, ServiceNotFoundException and Exception to be thrown.

ContainerExceptionInterface can be thrown by $this->fallbackContainer->get($id).
But ContainerExceptionInterface does not extend or implement anything expected by Symfony.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the problem: ContainerExceptionInterface doesn't extend/implement \Exception.

That's correct, but it's impossible to throw anything that doesn't extend exception so I think catching + rethrowing isn't necessary 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But honestly, I don't if it would work in more "structured" language. For instance, I'm not sure it would compile in Java

}

return null;
Expand Down
4 changes: 2 additions & 2 deletions tests/FunctionalTest/Fixtures/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace DI\Bridge\Symfony\Test\FunctionalTest\Fixtures;

use DI\Bridge\Symfony\Kernel;
use DI\Bridge\Symfony\KernelWithPhpDiContainer;
use DI\ContainerBuilder;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
class AppKernel extends KernelWithPhpDiContainer
{
private $configFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @coversNothing
*/
class KernelTest extends AbstractFunctionalTest
class KernelWithPhpDiContainerTest extends AbstractFunctionalTest
{
/**
* @test
Expand Down