Skip to main content

Building applications with Symfony Console, Disco and box2

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

In the past few weeks I built a few cli applications with Symfony Console and Disco, the DI container we open sourced last year. I wanted to figure ouf if we run into issues with Disco that we need to work on. The main script looks like this:

try {
$cache = new \Doctrine\Common\Cache\VoidCache();
$beanFactory = new \bitExpert\Disco\AnnotationBeanFactory(
\Myapp\CliConfiguration::class,
[],
$cache
);
\bitExpert\Disco\BeanFactoryRegistry::register($beanFactory);

/** @var \Symfony\Component\Console\Application $app */
$app = $beanFactory->get('cliApplication');
$app->run();
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit 1;
}

Calling the script from the console worked without any problems. Next up was trying to build a .phar file out of the sourcs. To help me build the .phar file I tried box. Building the .phar file was easy, but when i tried to run the generated .phar file I got the following error from Disco:

"\MyApp\CliConfiguration" seems not to be a valid configuration class. 
@Configuration annotation missing!

It was clear to me that somehow the docblock comments got stripped out when the .phar file was built. As it turns out I was using the default box.json.dist file which contains a so-called compactor configuration. Compactors like the \Herrera\Box\Compactor\Php are trying to reduce the size of the source files by stripping whitespaces or comments. The "fix" was simple: I removed the compactors definition from the box.json.dist file, rebuilt the .phar file and was able to run it without any further problems.