Skip to content

Commit 59aae70

Browse files
committed
[HttpKernel] Added env & debug to Bundle dependencies method
1 parent c262111 commit 59aae70

10 files changed

+51
-20
lines changed

src/Symfony/Component/HttpKernel/Bundle/BundleDependenciesInterface.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,35 @@ interface BundleDependenciesInterface
5454
* ```php
5555
* class AcmeBundle extends Bundle implements BundleDependenciesInterface
5656
* {
57-
* public function getBundleDependencies()
57+
* public function getBundleDependencies($environment, $debug)
5858
* {
59-
* return array(
59+
* $dependencies = array();
6060
*
61-
* // All keys must be FQN strings to avoid bundles being loaded several times
61+
* // If you need to load some bundles only in dev using $environment (or in $debug)
62+
* if ($environment === 'dev')
63+
* $dependencies['Egulias\SecurityDebugCommandBundle\EguliasSecurityDebugCommandBundle'] = self::DEP_REQUIRED;
64+
*
65+
* return $dependencies + array(
66+
* // Keys must be Fully Qualified Name (FQN) for the class to avoid bundles being loaded several times
67+
* // Note: Currently, use of DEP_OPTIONAL causes a *uncached* file system call on boot (every request)
68+
* // if dependency is missing, in a future versions this information might be cached.
6269
* 'FOS\HttpCacheBundle\FOSHttpCacheBundle' => self::DEP_OPTIONAL,
6370
*
6471
* // If you require PHP 5.5+ it is possible to use `::class` constant for required dependencies:
72+
* // Note: But doing so will make PHP throw error as opposed to the slightly more understandable
73+
* // one thrown by Symfony itself on missing dependencies.
6574
* Oneup\FlysystemBundle\OneupFlysystemBundle::class => self::DEP_REQUIRED,
6675
* );
6776
* }
6877
* }
6978
* ```
7079
*
80+
* @param string $environment The current environment
81+
* @param bool $debug Whether to debugging is enabled or not
7182
*
7283
* @return mixed[string] An array where key is bundle class (FQN) names as strings, and value DEP_* constants
7384
*
7485
* @api
7586
*/
76-
public function getBundleDependencies();
87+
public function getBundleDependencies($environment, $debug);
7788
}

src/Symfony/Component/HttpKernel/Kernel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ protected function appendDependenciesRecursively(array $children, array &$bundle
599599
if ($dependency instanceof BundleDependenciesInterface) {
600600
$stack[$dependencyFQN] = true;
601601
$this->appendDependenciesRecursively(
602-
$dependency->getBundleDependencies(),
602+
$dependency->getBundleDependencies($this->environment, $this->debug),
603603
$bundles,
604604
$rootBundles,
605605
$stack

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleADependenciesNon.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BundleADependenciesNon implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
2020
return array();
2121
}

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleBDependenciesA.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BundleBDependenciesA implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
2020
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon' => self::DEP_REQUIRED);
2121
}

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleCDependenciesBA.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515

1616
class BundleCDependenciesBA implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
20+
if ('prod_test' === $environment) {
21+
return array();
22+
} elseif ($debug) {
23+
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon' => self::DEP_REQUIRED);
24+
}
25+
2026
return array(
2127
'Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleBDependenciesA' => self::DEP_REQUIRED,
2228
'Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon' => self::DEP_OPTIONAL,

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleDDependenciesE.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BundleDDependenciesE implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
2020
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleEDependenciesD' => self::DEP_REQUIRED);
2121
}

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleEDependenciesD.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BundleEDependenciesD implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
2020
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleDDependenciesE' => self::DEP_REQUIRED);
2121
}

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleFDependenciesMissing.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BundleFDependenciesMissing implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
2020
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleMissing' => self::DEP_REQUIRED);
2121
}

src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleDependencies/BundleGDependenciesMissingOptional.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BundleGDependenciesMissingOptional implements BundleDependenciesInterface
1717
{
18-
public function getBundleDependencies()
18+
public function getBundleDependencies($environment, $debug)
1919
{
2020
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleMissing' => self::DEP_OPTIONAL);
2121
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,28 @@ public function getRegisteredDependenciesData()
772772
array($g),
773773
array($g),
774774
),
775+
// params
776+
array(
777+
array($cBA),
778+
array($cBA),
779+
'prod_test',
780+
),
781+
array(
782+
array($cBA),
783+
array($aNon, $cBA),
784+
'test',
785+
true,
786+
),
775787
);
776788
}
777789

778790
/**
779791
* @dataProvider getRegisteredDependenciesData
780792
*/
781-
public function testRegisteredDependencies(array $dependencies, array $expected)
793+
public function testRegisteredDependencies(array $dependencies, array $expected, $environment = 'test', $debug = false)
782794
{
783795
// use test kernel so we can test registeredDependencies() directly
784-
$kernel = $this->getKernelForTest(array('registerBundles'));
796+
$kernel = $this->getKernelForTest(array('registerBundles'), $environment, $debug);
785797
$kernel
786798
->expects($this->once())
787799
->method('registerBundles')
@@ -801,15 +813,15 @@ public function getRegisteredDependenciesExceptionData()
801813
return array(
802814
array(
803815
array($dE),
804-
"Recursive dependency for '".get_class($dE),
816+
"Recursive dependency for \"".get_class($dE),
805817
),
806818
array(
807819
array($eD),
808-
"Recursive dependency for '".get_class($eD),
820+
"Recursive dependency for \"".get_class($eD),
809821
),
810822
array(
811823
array($f),
812-
"Could not find '",
824+
"Could not find \"",
813825
),
814826
);
815827
}
@@ -953,14 +965,16 @@ protected function getKernel(array $methods = array(), array $bundles = array())
953965
/**
954966
* Returns a mock for the abstract kernel.
955967
*
956-
* @param array $methods Additional methods to mock (besides the abstract ones)
968+
* @param array $methods Additional methods to mock (besides the abstract ones)
969+
* @param string $environment The current environment
970+
* @param bool $debug Whether to debugging is enabled or not
957971
*
958972
* @return KernelForTest|\PHPUnit_Framework_MockObject_MockObject
959973
*/
960-
protected function getKernelForTest(array $methods = array())
974+
protected function getKernelForTest(array $methods = array(), $environment = 'test', $debug = false)
961975
{
962976
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
963-
->setConstructorArgs(array('test', false))
977+
->setConstructorArgs(array($environment, $debug))
964978
->setMethods($methods)
965979
->getMock();
966980
$p = new \ReflectionProperty($kernel, 'rootDir');

0 commit comments

Comments
 (0)