Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3 Symfony entries can reference PHP-DI entries
- Loading branch information
Showing
13 changed files
with
307 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# .gitattributes | ||
tests/ export-ignore | ||
.travis.yml export-ignore | ||
demo/ export-ignore | ||
|
||
# Auto detect text files and perform LF normalization | ||
* text=auto |
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?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 Interop\Container\ContainerInterface; | ||
use Symfony\Component\Debug\DebugClassLoader; | ||
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Customization of Symfony's kernel to setup PHP-DI. | ||
* | ||
* @author Matthieu Napoli <matthieu@mnapoli.fr> | ||
*/ | ||
abstract class Kernel extends \Symfony\Component\HttpKernel\Kernel | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $phpdiContainer; | ||
|
||
public function __construct($environment, $debug) | ||
{ | ||
parent::__construct($environment, $debug); | ||
$this->disableDebugClassLoader(); | ||
} | ||
|
||
/** | ||
* @return ContainerInterface | ||
*/ | ||
abstract protected function buildPHPDIContainer(\DI\ContainerBuilder $builder); | ||
|
||
protected function getContainerBaseClass() | ||
{ | ||
return 'DI\Bridge\Symfony\SymfonyContainerBridge'; | ||
} | ||
|
||
protected function buildContainer() | ||
{ | ||
$containerBuilder = parent::buildContainer(); | ||
|
||
$this->removeInvalidReferenceBehaviorPass($containerBuilder); | ||
|
||
return $containerBuilder; | ||
} | ||
|
||
protected function initializeContainer() | ||
{ | ||
parent::initializeContainer(); | ||
|
||
/** @var SymfonyContainerBridge $rootContainer */ | ||
$rootContainer = $this->getContainer(); | ||
|
||
$rootContainer->setFallbackContainer($this->getPHPDIContainer()); | ||
} | ||
|
||
/** | ||
* Remove the CheckExceptionOnInvalidReferenceBehaviorPass because | ||
* it was not looking into PHP-DI's entries and thus throwing exceptions. | ||
* | ||
* @todo Replace it by an alternative that can search into PHP-DI too | ||
* Problem: PHP-DI is not initialized when Symfony's container is compiled, because | ||
* it depends on Symfony's container for fallback (cycle…) | ||
* | ||
* @param ContainerBuilder $container | ||
*/ | ||
private function removeInvalidReferenceBehaviorPass(ContainerBuilder $container) | ||
{ | ||
$passConfig = $container->getCompilerPassConfig(); | ||
$compilationPasses = $passConfig->getRemovingPasses(); | ||
|
||
foreach ($compilationPasses as $i => $pass) { | ||
if ($pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass) { | ||
unset($compilationPasses[$i]); | ||
break; | ||
} | ||
} | ||
|
||
$passConfig->setRemovingPasses($compilationPasses); | ||
} | ||
|
||
private function disableDebugClassLoader() | ||
{ | ||
if (!class_exists('Symfony\Component\Debug\DebugClassLoader')) { | ||
return; | ||
} | ||
|
||
DebugClassLoader::disable(); | ||
} | ||
|
||
/** | ||
* @return ContainerInterface | ||
*/ | ||
private function getPHPDIContainer() | ||
{ | ||
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
22 changes: 22 additions & 0 deletions
22
tests/FunctionalTest/DI/Bridge/Symfony/AbstractFunctionalTest.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,22 @@ | ||
<?php | ||
|
||
|
||
namespace FunctionalTest\DI\Bridge\Symfony; | ||
|
||
use FunctionalTest\DI\Bridge\Symfony\Fixtures\AppKernel; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
abstract class AbstractFunctionalTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected function createKernel($configFile = 'empty.yml') | ||
{ | ||
// Clear the cache | ||
$fs = new Filesystem(); | ||
$fs->remove(__DIR__ . '/Fixtures/cache/dev'); | ||
|
||
$kernel = new AppKernel($configFile); | ||
$kernel->boot(); | ||
|
||
return $kernel; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
logs | ||
cache |
48 changes: 48 additions & 0 deletions
48
tests/FunctionalTest/DI/Bridge/Symfony/Fixtures/AppKernel.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,48 @@ | ||
<?php | ||
|
||
namespace FunctionalTest\DI\Bridge\Symfony\Fixtures; | ||
|
||
use DI\Bridge\Symfony\Kernel; | ||
use DI\ContainerBuilder; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
|
||
class AppKernel extends Kernel | ||
{ | ||
private $configFile; | ||
|
||
public function __construct($configFile) | ||
{ | ||
$this->configFile = $configFile; | ||
|
||
parent::__construct('dev', true); | ||
} | ||
|
||
protected function buildPHPDIContainer(ContainerBuilder $builder) | ||
{ | ||
return $builder->build(); | ||
} | ||
|
||
public function registerBundles() | ||
{ | ||
return array(); | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader) | ||
{ | ||
$loader->load(__DIR__ . '/config/' . $this->configFile); | ||
} | ||
|
||
protected function getContainerClass() | ||
{ | ||
return $this->randomName(); | ||
} | ||
|
||
private function randomName() { | ||
$characters = 'abcdefghijklmnopqrstuvwxyz'; | ||
$str = ''; | ||
for ($i = 0; $i < 10; $i++) { | ||
$str .= $characters[rand(0, strlen($characters) - 1)]; | ||
} | ||
return $str; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/FunctionalTest/DI/Bridge/Symfony/Fixtures/config/class1.yml
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,4 @@ | ||
services: | ||
class1: | ||
class: FunctionalTest\DI\Bridge\Symfony\Fixtures\Class1 | ||
arguments: [ '@FunctionalTest\\DI\\Bridge\\Symfony\\Fixtures\\Class2' ] |
3 changes: 3 additions & 0 deletions
3
tests/FunctionalTest/DI/Bridge/Symfony/Fixtures/config/class2.yml
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,3 @@ | ||
services: | ||
class2: | ||
class: FunctionalTest\DI\Bridge\Symfony\Fixtures\Class2 |
Empty file.
Oops, something went wrong.