A forgiving command loader for Symfony Console

The problem

By default, Symfony will instantiate all your application’s commands when initialising. If just one of the commands fails to initialize, the entire application will fail — even when executing a command that works fine.

Command loading

The solution is what Symfony calls ‘lazy loading’. This is simply retrieving the commands from the IoC container using a ‘command loader’. Symfony Console conveniently provides us with a CommandLoaderInterface and a ContainerCommandLoader implementation.

$consoleApp = new ConsoleApplication;
new ContainerCommandLoader($iocContainer, [
'command:one' => CommandOne::class,
'command:two' => ComamndTwo::class,
php console.php command:one

Remaining problem of listing

So far, we’ve solved the problem of non-instantiable commands blocking all other commands. However, listing (php console.php list) all of the console application’s commands is still not possible if just one of the commands is not instantiable.

php console.php listAvailable commands:
command:one Our first command
command:two ** Could not load
command:three Our third command
command:four Our fourth command



Software engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store