On his site Tomas Votruba continues his look at the Symfony/Console component of the Symfony framework. In this latest article he walks through the loading of configuration options from a file provided by a --config
option on the command line.
PHP CLI apps usually accept config, to setup their behavior. For PHPUnit it's phpunit.xml, for PHP CS Fixer it's .php_cs, for EasyCodingStandard it's easy-coding-standard.yml, for PHPStan it's phpstan.neon and so on.In the first post about PHP CLI Apps I wrote about poor DI support in PHP CLI projects.
Today we look on the first barrier that leads most people to prefer static over DI - how to load config with services.
He starts off talking about the "chicken and egg" issue when it comes to loading configuration: needing a configuration to create an Application
instance which then needs the config (and so on...). He then walks through three possible solutions:
- Not using a container to manage dependencies for the application
- Setting up a container in a command
- Using the
ArgvInput
input helper to pull directly from the arguments
He gets into more detail on this last method, providing code examples and input/output examples of it in use. Unfortunately this method also introduces some undesired dependencies between commands. He finishes the post with an alternative: setting up option definitions in the getDefaultInputDefinition
method of the main application and having them available to all commands.