From c5664a1fea99830fb3b0fdd088c8e9d35393e8ee Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 29 Mar 2016 13:04:29 +0200 Subject: [PATCH] Added the explanation about addClassesToCompile() method --- cookbook/bundles/extension.rst | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/cookbook/bundles/extension.rst b/cookbook/bundles/extension.rst index 908a408e34f..9a48ae39221 100644 --- a/cookbook/bundles/extension.rst +++ b/cookbook/bundles/extension.rst @@ -125,3 +125,39 @@ Using Configuration to Change the Services The Extension is also the class that handles the configuration for that particular bundle (e.g. the configuration in ``app/config/config.yml``). To read more about it, see the ":doc:`/cookbook/bundles/configuration`" article. + +Adding Classes to Compile +------------------------- + +Symfony creates a big ``classes.php`` file in the cache directory to aggregate +the contents of the PHP classes that are used in every request. This reduces the +I/O operations and increases the application performance. + +Your bundles can also add their own classes into this file thanks to the +``addClassesToCompile()`` method. Define the classes to compile as an array of +their fully qualified class names:: + + // ... + public function load(array $configs, ContainerBuilder $container) + { + // ... + + $this->addClassesToCompile(array( + 'AppBundle\\Manager\\UserManager', + 'AppBundle\\Utils\\Slugger', + // ... + )); + } + +.. note:: + + If some class extends from other classes, all its parents are automatically + included in the list of classes to compile. + +Beware that this technique **can't be used in some cases**: + +* When classes contain annotations, such as controllers with ``@Route`` + annotations and entities with ``@ORM`` or ``@Assert`` annotations, because + the file location retrieved from PHP reflection changes; +* When classes use the ``__DIR__`` and ``__FILE__`` constants, because their + values will change when loading these classes from the ``classes.php`` file.