@@ -133,9 +133,14 @@ Symfony creates a big ``classes.php`` file in the cache directory to aggregate
133133the contents of the PHP classes that are used in every request. This reduces the
134134I/O operations and increases the application performance.
135135
136+ .. versionadded :: 3.2
137+ The ``addAnnotatedClassesToCompile() `` method was added in Symfony 3.2.
138+
136139Your bundles can also add their own classes into this file thanks to the
137- ``addClassesToCompile() `` method. Define the classes to compile as an array of
138- their fully qualified class names::
140+ ``addClassesToCompile() `` and ``addAnnotatedClassesToCompile() `` methods (both
141+ work in the same way, but the second one is for classes that contain PHP
142+ annotations). Define the classes to compile as an array of their fully qualified
143+ class names::
139144
140145 use AppBundle\Manager\UserManager;
141146 use AppBundle\Utils\Slugger;
@@ -145,22 +150,52 @@ their fully qualified class names::
145150 {
146151 // ...
147152
153+ // this method can't compile classes that contain PHP annotations
148154 $this->addClassesToCompile(array(
149155 UserManager::class,
150156 Slugger::class,
151157 // ...
152158 ));
159+
160+ // add here only classes that contain PHP annotations
161+ $this->addAnnotatedClassesToCompile(array(
162+ 'AppBundle\\Controller\\DefaultController',
163+ // ...
164+ ));
153165 }
154166
155167.. note ::
156168
157169 If some class extends from other classes, all its parents are automatically
158170 included in the list of classes to compile.
159171
160- Beware that this technique **can't be used in some cases **:
172+ .. versionadded :: 3.2
173+ The option to add classes to compile using patterns was introduced in Symfony 3.2.
174+
175+ The classes to compile can also be added using file path patterns::
176+
177+ // ...
178+ public function load(array $configs, ContainerBuilder $container)
179+ {
180+ // ...
181+
182+ $this->addClassesToCompile(array(
183+ '**Bundle\\Manager\\',
184+ // ...
185+ ));
186+
187+ $this->addAnnotatedClassesToCompile(array(
188+ '**Bundle\\Controller\\',
189+ // ...
190+ ));
191+ }
192+
193+ Patterns are transformed into the actual class namespaces using the classmap
194+ generated by Composer. Therefore, before using these patterns, you must generate
195+ the full classmap executing the ``dump-autoload `` command of Composer.
196+
197+ .. caution ::
161198
162- * When classes contain annotations, such as controllers with ``@Route ``
163- annotations and entities with ``@ORM `` or ``@Assert `` annotations, because
164- the file location retrieved from PHP reflection changes;
165- * When classes use the ``__DIR__ `` and ``__FILE__ `` constants, because their
166- values will change when loading these classes from the ``classes.php `` file.
199+ This technique can't be used when the classes to compile use the ``__DIR__ ``
200+ or ``__FILE__ `` constants, because their values will change when loading
201+ these classes from the ``classes.php `` file.
0 commit comments