Skip to content

Symfony support

  1. Project files
  2. Project directory
  3. Cache
  4. Runtime component

Examples of Symfony applications can be found in the project under fixtures/build/dir012 (Symfony5) and fixtures/build/dir018 (Symfony6 with the Runtime component).

They may slightly vary to what you want to do since they are here for testing purposes, but they should be good enough to show-case a working scenario if this doc does not prove to be enough.

Project files

When working with a Symfony project, you usually have a few files that are necessary for the application to work but cannot be inferred from Composer. A non-exhaustive list: .env, public, config, var.

As a result, you will likely need to add them via directories, files or their *-bin variant which may force you to use the force-autodiscovery setting. For more information you can find check the Including files doc.

Project directory

Symfony 5.1+ defines the "project dir" as the directory where the composer.json file is. Because box deletes it during PHAR compilation, you need to redefine it in your Kernel. It is usually located in src/Kernel.php and can be defined as follow:

<?php

class Kernel extends BaseKernel
{
...
    public function getProjectDir()
    {
        return __DIR__.'/../';
    }
}

Cache

What makes Symfony a bit special for shipping it into a PHAR is its compilation step. Indeed, the Symfony container can be dumped depending on multiple parameters such the application environment, whether it is in debug mode or not and if the cache is fresh.

A PHAR however is a readonly only environment, which means the container cannot be dumped once inside the PHAR. To prevent the issue, you need to make sure of the following:

  • The cache is warmed up before being shipped within the PHAR
  • The application within the PHAR is running in production mode

To achieve this with the least amount of changes is to:

  • Create the .env.local.php file by running the following command:
composer dump-env prod

This will ensure when loading the variables that your application is in production mode.

  • Change the following part of the composer.json file:
"scripts": {
    "auto-scripts": {
        "cache:clear": "symfony-cmd",
        "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
        "@auto-scripts"
    ],
    "post-update-cmd": [
        "@auto-scripts"
    ]
},

For:

"scripts": {
    "auto-scripts": {
        "cache:clear": "symfony-cmd"
    },
    "post-autoload-dump": [
        "@auto-scripts"
    ]
},

I.e.:

  • You skip the installation of assets (which you shouldn't need in the context of a CLI application)
  • Trigger the auto-scripts, which includes the cache warming phase, on the Composer dump-autoload event

This last part takes advantage of Box dumping the autoloader by default.

Runtime component

If you are using symfony/runtime, you may end up into some errors (see https://github.com/symfony/symfony/issues/49443).

To fix the issue, you need to create your own autoload_runtime.template file, for example:

<?php

// autoload_runtime.php @generated by Symfony Runtime

if (true === (require_once __DIR__.'/autoload.php') || empty($_SERVER['SCRIPT_FILENAME'])) {
    return;
}

$pharPath = Phar::running();
if (strlen($pharPath) > 0 && !str_starts_with($scriptFileName, 'phar://')) {
    $scriptFileName = $pharPath.DIRECTORY_SEPARATOR.$scriptFileName;
} else {
    $scriptFileName = $_SERVER['SCRIPT_FILENAME'];
}

$app = require $scriptFileName;

if (!is_object($app)) {
    throw new TypeError(sprintf('Invalid return value: callable object expected, "%s" returned from "%s".', get_debug_type($app), $_SERVER['SCRIPT_FILENAME']));
}

$runtime = $_SERVER['APP_RUNTIME'] ?? $_ENV['APP_RUNTIME'] ?? %runtime_class%;
$runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%);

[$app, $args] = $runtime
    ->getResolver($app)
    ->resolve();

$app = $app(...$args);

exit(
    $runtime
        ->getRunner($app)
        ->run()
);

Notice the Phar::running() section: this is the key of the fix.

If you ended up changing the location of the console file (which is bin/console), you will need to further customize this in order to fix the problem.



« Docker supportReproducible build »