4
4
Best Practices for Reusable Bundles
5
5
===================================
6
6
7
- There are 2 types of bundles:
7
+ There are two types of bundles:
8
8
9
9
* Application-specific bundles: only used to build your application;
10
10
* 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
13
13
they're easy to configure and extend. Many of these recommendations do not
14
14
apply to application bundles because you'll want to keep those as simple
15
15
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.
22
18
23
19
.. index ::
24
20
pair: Bundle; Naming conventions
@@ -38,7 +34,7 @@ bundle class name must follow these simple rules:
38
34
39
35
* Use only alphanumeric characters and underscores;
40
36
* 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);
42
38
* Prefix the name with the concatenation of the vendor (and optionally the
43
39
category namespaces);
44
40
* Suffix the name with ``Bundle ``.
@@ -112,7 +108,7 @@ The following files are mandatory:
112
108
structure to work.
113
109
114
110
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
116
112
non-strategic, less-used files.
117
113
118
114
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
158
154
``Bundle/HelloBundle/Controller/HelloController.php `` and the fully qualified
159
155
class name is ``Bundle\HelloBundle\Controller\HelloController ``.
160
156
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 >`.
162
158
163
159
Some classes should be seen as facades and should be as short as possible, like
164
160
Commands, Helpers, Listeners, and Controllers.
@@ -181,7 +177,7 @@ Tests
181
177
-----
182
178
183
179
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:
185
181
186
182
* The test suite must be executable with a simple ``phpunit `` command run from
187
183
a sample application;
@@ -190,13 +186,14 @@ the ``Tests/`` directory. Tests should follow the following principles:
190
186
* The tests should cover at least 95% of the code base.
191
187
192
188
.. note ::
189
+
193
190
A test suite must not contain ``AllTests.php `` scripts, but must rely on the
194
191
existence of a ``phpunit.xml.dist `` file.
195
192
196
193
Documentation
197
194
-------------
198
195
199
- All classes and functions must come with full PHPDoc.
196
+ All classes and functions must be fully documented using ` PHPDoc `_ tags .
200
197
201
198
Extensive documentation should also be provided in the
202
199
:doc: `reStructuredText </contributing/documentation/format >` format, under
@@ -233,8 +230,8 @@ following standardized instructions in your ``README.md`` file.
233
230
Step 2: Enable the Bundle
234
231
-------------------------
235
232
236
- Then, enable the bundle by adding the following line in the `app/AppKernel.php`
237
- file of your project:
233
+ Then, enable the bundle by adding it to the list of registered bundles
234
+ in the `app/AppKernel.php` file of your project:
238
235
239
236
```php
240
237
<?php
@@ -279,8 +276,8 @@ following standardized instructions in your ``README.md`` file.
279
276
Step 2: Enable the Bundle
280
277
-------------------------
281
278
282
- Then, enable the bundle by adding the following line in the ``app/AppKernel.php``
283
- file of your project:
279
+ Then, enable the bundle by adding it to the list of registered bundles
280
+ in the ``app/AppKernel.php`` file of your project:
284
281
285
282
.. code-block:: php
286
283
@@ -306,8 +303,11 @@ following standardized instructions in your ``README.md`` file.
306
303
307
304
.. _`installation chapter`: https://getcomposer.org/doc/00-intro.md
308
305
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 unstable version,
310
+ include the version constraint (e.g. ``composer require friendsofsymfony/user-bundle "~2.0@dev" ``).
311
311
312
312
Optionally, you can add more installation steps (*Step 3 *, *Step 4 *, etc.) to
313
313
explain other required installation tasks, such as registering routes or
@@ -330,7 +330,8 @@ Translation Files
330
330
-----------------
331
331
332
332
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 ``).
334
335
335
336
A bundle must not override existing messages from another bundle.
336
337
@@ -369,18 +370,12 @@ The end user can provide values in any configuration file:
369
370
// app/config/config.php
370
371
$container->setParameter('acme_hello.email.from', '[email protected] ');
371
372
372
- .. code-block :: ini
373
-
374
- ; app/config/config.ini
375
- [parameters]
376
- acme_hello.email.from = [email protected]
377
-
378
373
Retrieve the configuration parameters in your code from the container::
379
374
380
375
$container->getParameter('acme_hello.email.from');
381
376
382
377
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 .
384
379
385
380
.. note ::
386
381
@@ -435,3 +430,4 @@ Learn more from the Cookbook
435
430
* :doc: `/cookbook/bundles/extension `
436
431
437
432
.. _standards : http://www.php-fig.org/psr/psr-4/
433
+ .. _`PHPDoc` : https://en.wikipedia.org/wiki/PHPDoc
0 commit comments