Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the explanation about addClassesToCompile() method #6405

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions cookbook/bundles/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.