Skip to content

Commit 6b27f8e

Browse files
committed
[HttpKernel] Simplify Bundle dependencies by moving *optional* to doc
1 parent 393c60b commit 6b27f8e

File tree

10 files changed

+23
-64
lines changed

10 files changed

+23
-64
lines changed

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
/**
1515
* Class BundleDependenciesInterface.
1616
*
17-
* Adds capability to let Bundles specify other bundles they need to load (before this one), both required and optional
18-
* dependencies.
17+
* Adds capability to let Bundles specify other bundles they need to load (before this one).
1918
*
2019
* This allows you to define only the bundle you want to use in registerBundles(), but don't need to take care about
2120
* registering the dependencies it uses, and you won't need to make any changes in your kernel if those dependencies
@@ -33,17 +32,6 @@
3332
*/
3433
interface BundleDependenciesInterface
3534
{
36-
/**
37-
* @const Flag a Bundle Dependency as required, if missing throw exception
38-
* @link \Symfony\Component\HttpKernel\Exception\DependencyMismatchException
39-
*/
40-
const DEP_REQUIRED = 'req';
41-
42-
/**
43-
* @const Flag a Bundle Dependency as optional, if missing silently ignore it
44-
*/
45-
const DEP_OPTIONAL = 'opt';
46-
4735
/**
4836
* Returns an array of bundle dependencies Kernel should register on boot.
4937
*
@@ -56,20 +44,24 @@ interface BundleDependenciesInterface
5644
* {
5745
* public function getBundleDependencies()
5846
* {
59-
* return array(
47+
* // If you need to specify some bundle dependencies as optional you can achieve this using class_exits:
48+
* // $dependencies = array();
49+
* // if (class_exists('FOS\UserBundle\FOSUserBundle'))
50+
* // $dependencies[] = 'FOS\UserBundle\FOSUserBundle';
6051
*
61-
* // All keys must be FQN strings to avoid bundles being loaded several times
62-
* 'FOS\HttpCacheBundle\FOSHttpCacheBundle' => self::DEP_OPTIONAL,
52+
* return array(
53+
* // All values must be FQN strings to avoid bundles being loaded several times
54+
* 'FOS\HttpCacheBundle\FOSHttpCacheBundle',
6355
*
64-
* // If you require PHP 5.5+ it is possible to use `::class` constant for required dependencies:
65-
* Oneup\FlysystemBundle\OneupFlysystemBundle::class => self::DEP_REQUIRED,
56+
* // If you require PHP 5.5 or higher it is better to use `::class` constant:
57+
* Oneup\FlysystemBundle\OneupFlysystemBundle::class,
6658
* );
6759
* }
6860
* }
6961
* ```
7062
*
7163
*
72-
* @return mixed[string] An array where key is bundle class (FQN) names as strings, and value DEP_* constants
64+
* @return string[] An array of bundle class (FQN) names as strings.
7365
*
7466
* @api
7567
*/

src/Symfony/Component/HttpKernel/Exception/DependencyMismatchException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* DependencyMismatchException.
1616
*
17-
* For exceptions related to dependency issues, like missing required dependency or recursive dependencies.
17+
* For exceptions related to dependency issues, like missing or recursive dependencies.
1818
*
1919
* @author André Roemcke <andre.romcke@ez.no>
2020
*
@@ -25,7 +25,7 @@ class DependencyMismatchException extends \RuntimeException
2525
/**
2626
* Constructor.
2727
*
28-
* @param string $msg The message; for recursion issues, missing required dependency, ..
28+
* @param string $msg The message; for recursion issues, missing dependency, ..
2929
* @param array $stack The Bundle dependency stack trace up until the mismatch using bundle name or FQN
3030
* @param \Exception $previous The previous exception if there was one
3131
*/

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,7 @@ protected function registeredDependencies()
526526

527527
// Build up bundles as a hash with FQN for basis to rebuild again in correct order given dependencies
528528
foreach ($bundles as $bundle) {
529-
$bundleFQN = get_class($bundle);
530-
$children[$bundleFQN] = BundleDependenciesInterface::DEP_REQUIRED;
529+
$children[] = $bundleFQN = get_class($bundle);
531530
$rootBundles[$bundleFQN] = $bundle;
532531

533532
if ($bundle instanceof BundleDependenciesInterface) {
@@ -559,9 +558,7 @@ protected function registeredDependencies()
559558
* this string must be in exactly same format as returned by get_class() and PHP 5.5's CLASS constant.
560559
* Example of FQN string: "Symfony\Component\HttpKernel\Bundle\BundleInterface"
561560
*
562-
* @link BundleDependenciesInterface For further details on dependencies and DEP_* constants.
563-
*
564-
* @param mixed[string] $children Dependencies to apply, key FQN, value {@see BundleDependenciesInterface} constants
561+
* @param string[] $children Dependencies to apply
565562
* @param BundleInterface[string] $bundles Ordered bundles to append dependencies to
566563
* @param BundleInterface[string] $rootBundles Loaded Bundles from registerRootBundles()
567564
* @param bool[string] $stack For recursion protection, and for debug use on exceptions
@@ -570,7 +567,8 @@ protected function registeredDependencies()
570567
*/
571568
protected function appendDependenciesRecursively(array $children, array &$bundles, array $rootBundles, array $stack)
572569
{
573-
foreach ($children as $dependencyFQN => $requiredFlag) {
570+
while (!empty($children)) {
571+
$dependencyFQN = array_shift($children);
574572
if (isset($bundles[$dependencyFQN])) {
575573
continue;
576574
} elseif (isset($stack[$dependencyFQN])) {
@@ -582,9 +580,6 @@ protected function appendDependenciesRecursively(array $children, array &$bundle
582580
$dependency = $rootBundles[$dependencyFQN];
583581
} else {
584582
if (!class_exists($dependencyFQN)) {
585-
if ($requiredFlag === BundleDependenciesInterface::DEP_OPTIONAL) {
586-
continue;
587-
}
588583
throw new DependencyMismatchException("Could not find '{$dependencyFQN}'", array_keys($stack));
589584
}
590585
$dependency = new $dependencyFQN();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class BundleBDependenciesA implements BundleDependenciesInterface
1717
{
1818
public function getBundleDependencies()
1919
{
20-
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon' => self::DEP_REQUIRED);
20+
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon');
2121
}
2222
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class BundleCDependenciesBA implements BundleDependenciesInterface
1818
public function getBundleDependencies()
1919
{
2020
return array(
21-
'Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleBDependenciesA' => self::DEP_REQUIRED,
22-
'Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon' => self::DEP_OPTIONAL,
21+
'Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleBDependenciesA',
22+
'Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleADependenciesNon',
2323
);
2424
}
2525
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class BundleDDependenciesE implements BundleDependenciesInterface
1717
{
1818
public function getBundleDependencies()
1919
{
20-
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleEDependenciesD' => self::DEP_REQUIRED);
20+
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleEDependenciesD');
2121
}
2222
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class BundleEDependenciesD implements BundleDependenciesInterface
1717
{
1818
public function getBundleDependencies()
1919
{
20-
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleDDependenciesE' => self::DEP_REQUIRED);
20+
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleDDependenciesE');
2121
}
2222
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class BundleFDependenciesMissing implements BundleDependenciesInterface
1717
{
1818
public function getBundleDependencies()
1919
{
20-
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleMissing' => self::DEP_REQUIRED);
20+
return array('Symfony\Component\HttpKernel\Tests\Fixtures\BundleDependencies\BundleMissing');
2121
}
2222
}

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ public function getRegisteredDependenciesData()
726726
$aNon = new BundleDependencies\BundleADependenciesNon();
727727
$bA = new BundleDependencies\BundleBDependenciesA();
728728
$cBA = new BundleDependencies\BundleCDependenciesBA();
729-
$g = new BundleDependencies\BundleGDependenciesMissingOptional();
730729

731730
return array(
732731
array(
@@ -769,11 +768,6 @@ public function getRegisteredDependenciesData()
769768
array($bA, $cBA),
770769
array($aNon, $bA, $cBA),
771770
),
772-
// optional
773-
array(
774-
array($g),
775-
array($g),
776-
),
777771
);
778772
}
779773

0 commit comments

Comments
 (0)