Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Symfony missing dev bundles in prod environment?

My Symfony application has a few dependencies that are only required for development, testing and the like. These are defined in my composer.json in the require-dev section.

Here is how I add them in AppKernel.php:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            // ...
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
            $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
        }

        return $bundles;
    }
}

When I update my application, I run php composer.phar install --no-dev --optimize-autoloader. This installs all requirements not required for the dev environment and then clears the cache.

However, clearing the cache fails with the following message:

PHP Fatal error:  Class 'Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle' not found in /my/project/app/AppKernel.php on line 29
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception



  [RuntimeException]
  An error occurred when executing the "'cache:clear --no-warmup'" command.

This is not only a problem with the Doctrine Fixtures Bundle. If I change the order so the Liip Functional Test Bundle comes first then the error will be about that bundle.

Why am I seeing this error? Why does Symfony try to access these bundles even though we are explicitly not in the dev environment (notice the --no-dev composer flag)? And what can I do to make this go away without having to install all dev dependencies on the production machine?

like image 949
Chris Avatar asked May 18 '15 08:05

Chris


2 Answers

It is because symfony default env is dev, composer --no-dev only tells composer not to install dev requirements, symfony does not know about the environment. Use SYMFONY_ENV=prod environmental variable. http://symfony.com/doc/current/cookbook/deployment/tools.html#c-install-update-your-vendors

E.g: $ SYMFONY_ENV=prod php composer.phar install --no-dev --optimize-autoloader

like image 64
Fracsi Avatar answered Nov 06 '22 03:11

Fracsi


You need to tell the the cache:clear command to run in a production environment.

php app/console --env=production cache:clear

(If necessary, changing "production" to whatever you're calling the particular non-dev environment you're dealing with)

like image 31
Jez Avatar answered Nov 06 '22 03:11

Jez