-
Notifications
You must be signed in to change notification settings - Fork 11
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -62,7 +50,7 @@ protected function initializeContainer() | |
/** @var SymfonyContainerBridge $rootContainer */ | ||
$rootContainer = $this->getContainer(); | ||
|
||
$rootContainer->setFallbackContainer($this->getPHPDIContainer()); | ||
$rootContainer->setFallbackContainer($this->getFallbackContainer()); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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
andException
to be thrown.ContainerExceptionInterface
can be thrown by$this->fallbackContainer->get($id)
.But
ContainerExceptionInterface
does not extend or implement anything expected by Symfony.There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe :)
There was a problem hiding this comment.
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