Skip to content

Commit fad2060

Browse files
committed
[HttpKernel] Added support for registering Bundle dependencies
Doc for : symfony/symfony#15011
1 parent eafdb8c commit fad2060

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

cookbook/bundles/dependencies.rst

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. index::
2+
single: Bundle; Dependencies
3+
4+
How to Use Bundle Dependencies to Load other Bundles
5+
====================================================
6+
7+
.. versionadded:: 2.8
8+
Support for bundle dependencies was introduced in Symfony 2.8.
9+
10+
When working on your own bundle(s), you'll sometimes have the need to reuse
11+
other bundles, either by just requiring, overriding or inheriting from them.
12+
13+
While Composer takes care about making sure these dependencies are loaded,
14+
you'll also need to enable these bundles in the kernel.
15+
16+
If your bundle is meant to be reused, bundle dependencies will complicate
17+
your installation documentation. This makes installation and upgrading your
18+
bundle more tedious for your users.
19+
20+
You can avoid this by specifying your dependencies. This will make sure that
21+
they are loaded in the kernel. It'll also make sure they are loaded *before*
22+
your own bundle, to make sure you can extend them.
23+
24+
Additional use case for this is for distribution bundles, where one
25+
bundle is in fact bundling several others.
26+
27+
Specifying Dependencies
28+
-----------------------
29+
30+
Dependencies are specified using FQN_ (`Fully Qualified Name`) for the bundle class in
31+
same format as PHP uses for get_class_, and for class_ constant introduced in PHP 5.5.
32+
33+
.. _FQN: https://en.wikipedia.org/wiki/Fully_qualified_name
34+
.. _get_class: http://php.net/manual/en/function.get-class.php
35+
.. _class: http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class
36+
37+
This implies you can only specify bundles that does not take arguments in it's constructor.
38+
This is in-line with Symfony best practice, avoids same bundle being loaded several times,
39+
and allows this information to be easily cached in future versions of Symfony.
40+
41+
Specifying dependencies is accomplished by implementing the
42+
:class:`Symfony\\Component\\HttpKernel\\Bundle\\BundleDependenciesInterface`::
43+
44+
// src/CacheBundle/CacheBundle.php
45+
namespace CacheBundle;
46+
47+
use Symfony\Component\HttpKernel\Bundle\Bundle;
48+
use Symfony\Component\HttpKernel\Bundle\BundleDependenciesInterface;
49+
50+
class CacheBundle extends Bundle implements BundleDependenciesInterface
51+
{
52+
public function getBundleDependencies($environment, $debug)
53+
{
54+
return array('FOS\HttpCacheBundle\FOSHttpCacheBundle' => self::DEP_REQUIRED);
55+
}
56+
}
57+
58+
.. tip::
59+
60+
If your bundle requires PHP 5.5 or higher, you can also take advantage of
61+
the ``class`` constant::
62+
63+
use FOS\HttpCacheBundle\FOSHttpCacheBundle;
64+
65+
// ...
66+
public function getBundleDependencies($environment, $debug)
67+
{
68+
return array(FOSHttpCacheBundle::class => self::DEP_REQUIRED);
69+
}
70+
71+
.. tip::
72+
73+
If your dependency is only to be loaded in ``dev`` or when debugging, use the provided arguments::
74+
75+
use Egulias\SecurityDebugCommandBundle\EguliasSecurityDebugCommandBundle;
76+
77+
// ...
78+
public function getBundleDependencies($environment, $debug)
79+
{
80+
if ($environment !== 'dev') {
81+
return array();
82+
}
83+
84+
return array('Egulias\SecurityDebugCommandBundle\EguliasSecurityDebugCommandBundle' => self::DEP_REQUIRED);
85+
}
86+
87+
88+
Specifying Optional Dependencies
89+
--------------------------------
90+
91+
Specifying a optional dependency follows the same format but with a different constant::
92+
93+
// ...
94+
public function getBundleDependencies($environment, $debug)
95+
{
96+
return array('Oneup\FlysystemBundle\OneupFlysystemBundle' => self::DEP_OPTIONAL);
97+
}

cookbook/bundles/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Bundles
77
installation
88
best_practices
99
inheritance
10+
dependencies
1011
override
1112
remove
1213
extension

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* :doc:`/cookbook/bundles/installation`
1313
* :doc:`/cookbook/bundles/best_practices`
1414
* :doc:`/cookbook/bundles/inheritance`
15+
* :doc:`/cookbook/bundles/dependencies`
1516
* :doc:`/cookbook/bundles/override`
1617
* :doc:`/cookbook/bundles/remove`
1718
* :doc:`/cookbook/bundles/extension`

0 commit comments

Comments
 (0)