New in Symfony 6.4: Build Dir Improvements


In Symfony applications, the kernel.build_dir option defines the directory where
read-only contents are generated during the compilation of the application. In
Symfony 6.4 we've made some improvements related to it.

APP_BUILD_DIR Support

Contributed by Roland Franssen
in #50951.

By default, the getBuildDir() method in the Symfony Kernel class returns
$this->getCacheDir() for backward-compatibility reasons. In Symfony 6.4 we've
improved this method to check first the value of the APP_BUILD_DIR environment
variable:

public function getBuildDir(): string
{
if (isset($_SERVER['APP_BUILD_DIR'])) {
return $_SERVER['APP_BUILD_DIR'].'/'.$this->environment;
}

return parent::getBuildDir();
}

These changes were added to the Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait,
which is used in the App\Kernel class created by default in new Symfony applications.
This will simplify the deployment of Symfony applications to read-only filesystems
like Docker containers or AWS Lambda.

Using Build Dir to Generate More Contents

Contributed by Quentin D.
in #50391.

The build_dir feature is probably underused in Symfony. By default, only the
compiled container is dumped in the build_dir. All the other read-only compiled
artifacts are dumped into the cache_dir: the router cache, Doctrine proxy classes,
Symfony Config classes, translations, etc.
In order to improve this, we're making a change in the cache warmer classes.
The warmUp() method of the WarmableInterface now accepts a second argument
with the $buildDir value:

namespace Symfony\Component\HttpKernel\CacheWarmer;

interface WarmableInterface
{
- public function warmUp(string $cacheDir): array;
+ public function warmUp(string $cacheDir/* , string $buildDir = null */): array;
}

Thanks to this change, you can now store read-only artifacts more easily in the
build_dir, which is the best location for them. In order to make the change
backward-compatible, we added the argument as a comment in 6.4 (and it will be a
true argument in Symfony 7.0).
This change will trigger deprecation messages when updating your applications to
Symfony 6.4. Make sure to add this change to your warmers (and to contribute this
change to third-party bundles used in your applications) so you can later upgrade
to Symfony 7.

Sponsor the Symfony project.