Skip to content

Commit ebdce9f

Browse files
committed
minor #5095 Reviewed the Bundles cookbook articles (javiereguiluz)
This PR was merged into the 2.3 branch. Discussion ---------- Reviewed the Bundles cookbook articles | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | - Commits ------- 8008ac6 Implemented the suggestions made by Christian and Wouter 8861215 Replace phpDocumentor by the standard PHPDoc c0637b6 Implemented the changes suggested by reviewers 3dd40b2 Fixed an internal link reference 8254761 Reviewed the Bundles cookbook articles
2 parents 5d304d9 + 8008ac6 commit ebdce9f

File tree

5 files changed

+78
-59
lines changed

5 files changed

+78
-59
lines changed

cookbook/bundles/best_practices.rst

+19-28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Best Practices for Reusable Bundles
55
===================================
66

7-
There are 2 types of bundles:
7+
There are two types of bundles:
88

99
* Application-specific bundles: only used to build your application;
1010
* Reusable bundles: meant to be shared across many projects.
@@ -13,12 +13,8 @@ This article is all about how to structure your **reusable bundles** so that
1313
they're easy to configure and extend. Many of these recommendations do not
1414
apply to application bundles because you'll want to keep those as simple
1515
as possible. For application bundles, just follow the practices shown throughout
16-
the book and cookbook.
17-
18-
.. seealso::
19-
20-
The best practices for application-specific bundles are discussed in
21-
:doc:`/best_practices/introduction`.
16+
the :doc:`book </book/index>`, the :doc:`cookbook </cookbook/index>` and the
17+
:doc:`best practices </best_practices/index>` book.
2218

2319
.. index::
2420
pair: Bundle; Naming conventions
@@ -38,7 +34,7 @@ bundle class name must follow these simple rules:
3834

3935
* Use only alphanumeric characters and underscores;
4036
* Use a CamelCased name;
41-
* Use a descriptive and short name (no more than 2 words);
37+
* Use a descriptive and short name (no more than two words);
4238
* Prefix the name with the concatenation of the vendor (and optionally the
4339
category namespaces);
4440
* Suffix the name with ``Bundle``.
@@ -112,7 +108,7 @@ The following files are mandatory:
112108
structure to work.
113109

114110
The depth of sub-directories should be kept to the minimal for most used
115-
classes and files (2 levels at a maximum). More levels can be defined for
111+
classes and files (two levels at a maximum). More levels can be defined for
116112
non-strategic, less-used files.
117113

118114
The bundle directory is read-only. If you need to write temporary files, store
@@ -158,7 +154,7 @@ instance, a ``HelloController`` controller is stored in
158154
``Bundle/HelloBundle/Controller/HelloController.php`` and the fully qualified
159155
class name is ``Bundle\HelloBundle\Controller\HelloController``.
160156

161-
All classes and files must follow the Symfony coding :doc:`standards </contributing/code/standards>`.
157+
All classes and files must follow the :doc:`Symfony coding standards </contributing/code/standards>`.
162158

163159
Some classes should be seen as facades and should be as short as possible, like
164160
Commands, Helpers, Listeners, and Controllers.
@@ -181,7 +177,7 @@ Tests
181177
-----
182178

183179
A bundle should come with a test suite written with PHPUnit and stored under
184-
the ``Tests/`` directory. Tests should follow the following principles:
180+
the ``Tests/`` directory. Tests should follow these principles:
185181

186182
* The test suite must be executable with a simple ``phpunit`` command run from
187183
a sample application;
@@ -190,13 +186,14 @@ the ``Tests/`` directory. Tests should follow the following principles:
190186
* The tests should cover at least 95% of the code base.
191187

192188
.. note::
189+
193190
A test suite must not contain ``AllTests.php`` scripts, but must rely on the
194191
existence of a ``phpunit.xml.dist`` file.
195192

196193
Documentation
197194
-------------
198195

199-
All classes and functions must come with full PHPDoc.
196+
All classes and functions must be fully documented using `PHPDoc`_ tags.
200197

201198
Extensive documentation should also be provided in the
202199
:doc:`reStructuredText </contributing/documentation/format>` format, under
@@ -306,8 +303,11 @@ following standardized instructions in your ``README.md`` file.
306303
307304
.. _`installation chapter`: https://getcomposer.org/doc/00-intro.md
308305
309-
This template assumes that your bundle is in its ``1.x`` version. If not, change
310-
the ``"~1"`` installation version accordingly (``"~2"``, ``"~3"``, etc.)
306+
The example above assumes that you are installing the latest stable version of
307+
the bundle, where you don't have to provide the package version number
308+
(e.g. ``composer require friendsofsymfony/user-bundle``). If the installation
309+
instructions refer to some past bundle version or to some inestable version,
310+
include the version constraint (e.g. ``composer require friendsofsymfony/user-bundle "~2.0@dev"``).
311311

312312
Optionally, you can add more installation steps (*Step 3*, *Step 4*, etc.) to
313313
explain other required installation tasks, such as registering routes or
@@ -330,7 +330,8 @@ Translation Files
330330
-----------------
331331

332332
If a bundle provides message translations, they must be defined in the XLIFF
333-
format; the domain should be named after the bundle name (``bundle.hello``).
333+
format; the :ref:`translation domain <using-message-domains>` should be named
334+
after the bundle name (``bundle.hello``).
334335

335336
A bundle must not override existing messages from another bundle.
336337

@@ -369,27 +370,17 @@ The end user can provide values in any configuration file:
369370
// app/config/config.php
370371
$container->setParameter('acme_hello.email.from', '[email protected]');
371372
372-
.. code-block:: ini
373-
374-
; app/config/config.ini
375-
[parameters]
376-
acme_hello.email.from = [email protected]
377-
378373
Retrieve the configuration parameters in your code from the container::
379374

380375
$container->getParameter('acme_hello.email.from');
381376

382377
Even if this mechanism is simple enough, you are highly encouraged to use the
383-
semantic configuration described in the cookbook.
378+
:doc:`semantic bundle configuration </cookbook/bundles/extension>` instead.
384379

385380
.. note::
386381

387382
If you are defining services, they should also be prefixed with the bundle
388383
alias.
389384

390-
Learn more from the Cookbook
391-
----------------------------
392-
393-
* :doc:`/cookbook/bundles/extension`
394-
395-
.. _standards: http://www.php-fig.org/psr/psr-4/
385+
.. _`standards`: http://www.php-fig.org/psr/psr-0/
386+
.. _`PHPDoc`: https://en.wikipedia.org/wiki/PHPDoc

cookbook/bundles/configuration.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ How to Create Friendly Configuration for a Bundle
66
=================================================
77

88
If you open your application configuration file (usually ``app/config/config.yml``),
9-
you'll see a number of different configuration "namespaces", such as ``framework``,
9+
you'll see a number of different configuration sections, such as ``framework``,
1010
``twig`` and ``doctrine``. Each of these configures a specific bundle, allowing
11-
you to configure things at a high level and then let the bundle make all the
11+
you to define options at a high level and then let the bundle make all the
1212
low-level, complex changes based on your settings.
1313

14-
For example, the following tells the FrameworkBundle to enable the form
15-
integration, which involves the definition of quite a few services as well
14+
For example, the following configuration tells the FrameworkBundle to enable the
15+
form integration, which involves the definition of quite a few services as well
1616
as integration of other related components:
1717

1818
.. configuration-block::

cookbook/bundles/extension.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ How to Load Service Configuration inside a Bundle
66
=================================================
77

88
In Symfony, you'll find yourself using many services. These services can be
9-
registered in the `app/config` directory of your application. But when you
9+
registered in the ``app/config/`` directory of your application. But when you
1010
want to decouple the bundle for use in other projects, you want to include the
1111
service configuration in the bundle itself. This article will teach you how to
1212
do that.
@@ -15,7 +15,7 @@ Creating an Extension Class
1515
---------------------------
1616

1717
In order to load service configuration, you have to create a Dependency
18-
Injection Extension for your bundle. This class has some conventions in order
18+
Injection (DI) Extension for your bundle. This class has some conventions in order
1919
to be detected automatically. But you'll later see how you can change it to
2020
your own preferences. By default, the Extension has to comply with the
2121
following conventions:

cookbook/bundles/inheritance.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ things like controllers, templates, and other files in a bundle's
1212

1313
For example, suppose that you're installing the `FOSUserBundle`_, but you
1414
want to override its base ``layout.html.twig`` template, as well as one of
15-
its controllers. Suppose also that you have your own AcmeUserBundle
16-
where you want the overridden files to live. Start by registering the FOSUserBundle
17-
as the "parent" of your bundle::
15+
its controllers. Suppose also that you have your own UserBundle where you want
16+
the overridden files to live. Start by registering the FOSUserBundle as the
17+
"parent" of your bundle::
1818

19-
// src/Acme/UserBundle/AcmeUserBundle.php
20-
namespace Acme\UserBundle;
19+
// src/UserBundle/UserBundle.php
20+
namespace UserBundle;
2121

2222
use Symfony\Component\HttpKernel\Bundle\Bundle;
2323

24-
class AcmeUserBundle extends Bundle
24+
class UserBundle extends Bundle
2525
{
2626
public function getParent()
2727
{
@@ -45,8 +45,8 @@ Suppose you want to add some functionality to the ``registerAction`` of a
4545
just create your own ``RegistrationController.php`` file, override the bundle's
4646
original method, and change its functionality::
4747

48-
// src/Acme/UserBundle/Controller/RegistrationController.php
49-
namespace Acme\UserBundle\Controller;
48+
// src/UserBundle/Controller/RegistrationController.php
49+
namespace UserBundle\Controller;
5050

5151
use FOS\UserBundle\Controller\RegistrationController as BaseController;
5252

@@ -82,17 +82,17 @@ location as your parent bundle.
8282
For example, it's very common to need to override the FOSUserBundle's
8383
``layout.html.twig`` template so that it uses your application's base layout.
8484
Since the file lives at ``Resources/views/layout.html.twig`` in the FOSUserBundle,
85-
you can create your own file in the same location of AcmeUserBundle.
86-
Symfony will ignore the file that lives inside the FOSUserBundle entirely,
87-
and use your file instead.
85+
you can create your own file in the same location of UserBundle. Symfony will
86+
ignore the file that lives inside the FOSUserBundle entirely, and use your file
87+
instead.
8888

8989
The same goes for routing files and some other resources.
9090

9191
.. note::
9292

9393
The overriding of resources only works when you refer to resources with
9494
the ``@FOSUserBundle/Resources/config/routing/security.xml`` method.
95-
If you refer to resources without using the @BundleName shortcut, they
95+
If you refer to resources without using the ``@BundleName`` shortcut, they
9696
can't be overridden in this way.
9797

9898
.. caution::

cookbook/bundles/installation.rst

+41-13
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ A) Add Composer Dependencies
1515
----------------------------
1616

1717
Dependencies are managed with Composer, so if Composer is new to you, learn
18-
some basics in `their documentation`_. This has 2 steps:
18+
some basics in `their documentation`_. This involves two steps:
1919

2020
1) Find out the Name of the Bundle on Packagist
2121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2222

2323
The README for a bundle (e.g. `FOSUserBundle`_) usually tells you its name
2424
(e.g. ``friendsofsymfony/user-bundle``). If it doesn't, you can search for
25-
the library on the `Packagist.org`_ site.
25+
the bundle on the `Packagist.org`_ site.
2626

27-
.. note::
27+
.. tip::
2828

2929
Looking for bundles? Try searching at `KnpBundles.com`_: the unofficial
3030
archive of Symfony Bundles.
@@ -39,9 +39,12 @@ Now that you know the package name, you can install it via Composer:
3939
$ composer require friendsofsymfony/user-bundle
4040
4141
This will choose the best version for your project, add it to ``composer.json``
42-
and download the library into the ``vendor/`` directory. If you need a specific
43-
version, add a ``:`` and the version right after the library name (see
44-
`composer require`_).
42+
and download its code into the ``vendor/`` directory. If you need a specific
43+
version, include it as the second argument of the `composer require`_ command:
44+
45+
.. code-block:: bash
46+
47+
$ composer require friendsofsymfony/user-bundle "~2.0"
4548
4649
B) Enable the Bundle
4750
--------------------
@@ -60,30 +63,55 @@ The only thing you need to do now is register the bundle in ``AppKernel``::
6063
public function registerBundles()
6164
{
6265
$bundles = array(
63-
// ...,
66+
// ...
6467
new FOS\UserBundle\FOSUserBundle(),
6568
);
6669

6770
// ...
6871
}
6972
}
7073

74+
By default, Symfony bundles are registered in all the application
75+
:doc:`execution environments </cookbook/configuration/environments>`. If the bundle
76+
is meant to be used only in some environment, register it within an ``if`` statement,
77+
like the following example, where the FOSUserBundle is only enabled for the
78+
``dev`` and ``test`` environments::
79+
80+
// app/AppKernel.php
81+
82+
// ...
83+
class AppKernel extends Kernel
84+
{
85+
// ...
86+
87+
public function registerBundles()
88+
{
89+
$bundles = array(
90+
// ...
91+
);
92+
93+
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
94+
$bundles[] = new FOS\UserBundle\FOSUserBundle();
95+
}
96+
97+
// ...
98+
}
99+
}
100+
71101
C) Configure the Bundle
72102
-----------------------
73103

74104
It's pretty common for a bundle to need some additional setup or configuration
75105
in ``app/config/config.yml``. The bundle's documentation will tell you about
76-
the configuration, but you can also get a reference of the bundle's config
77-
via the ``config:dump-reference`` command.
78-
79-
For instance, in order to look the reference of the ``assetic`` config you
80-
can use this:
106+
the configuration, but you can also get a reference of the bundle's configuration
107+
via the ``config:dump-reference`` command:
81108

82109
.. code-block:: bash
83110
84111
$ app/console config:dump-reference AsseticBundle
85112
86-
or this:
113+
Instead of the full bundle name, you can also pass the short name used as the root
114+
of the bundle's configuration:
87115

88116
.. code-block:: bash
89117

0 commit comments

Comments
 (0)