Skip to content

Added the explanation about addClassesToCompile() method #6405

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

Closed
wants to merge 6 commits into from
Closed
Changes from 3 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
39 changes: 39 additions & 0 deletions cookbook/bundles/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,42 @@ 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
-------------------------

In order to make applications run as fast as possible on production environment,
Symfony creates a big ``classes.php`` file in the cache directory. This file
aggregates the contents of the PHP classes that are used in every request,
reducing the I/O operations related to those classes.

Your own bundles can add 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\\Service\\Slugger',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using the class constant here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's 2.3, let's not do that. We should however use a single backslash instead of double ones.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I would not advice to use a Service namespace, causes confusion for users.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say let's not do that because we're still targeting PHP 5.3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wouterj I agree about the namespace. Changed!

// ...
));
}

.. note::

If some class extends from other classes, all its parents are included
automatically in the list of classes to compile.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[...] are automatically included [...]"


After adding to compile all the classes commonly used by your bundle, you can
expect a minor performance improvement.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that this paragraph doesn't add any value and can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then we explain how to do something ... but not why it's useful. But I'm going to reword it.

Copy link
Member

@wouterj wouterj May 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the first sentence of the section already explains that we do this to improve performance.


The main drawback of this technique is that it doesn't work when:

* Classes contain annotations, such as controllers with ``@Route`` annotations
and entities with ``@ORM`` or ``@Assert`` annotations;
* Classes use the ``__DIR__`` and ``__FILE__`` constants, because their values
will change when loading these classes from the ``classes.php`` file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally would find it interesting to know why it doesn't work for annotations. One of the reasons is the file location retrieved from reflection. Is this something you could add to the documentation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just added your proposal. Thanks!