为什么 Symfony 在 prod 环境中缺少开发包?

2022-01-21 00:00:00 php symfony composer-php

我的 Symfony 应用程序有一些依赖项,这些依赖项仅用于开发、测试等.这些在我的 composer.jsonrequire-dev 部分中定义.

这是我在 AppKernel.php 中添加它们的方法:

类 AppKernel 扩展内核{公共函数 registerBundles(){$bundles = 数组(新 SymfonyBundleFrameworkBundleFrameworkBundle(),新的 SymfonyBundleSecurityBundleSecurityBundle(),//...);if (in_array($this->getEnvironment(), array('dev', 'test'))) {$bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle();$bundles[] = 新 SensioBundleDistributionBundleSensioDistributionBundle();$bundles[] = 新 SensioBundleGeneratorBundleSensioGeneratorBundle();$bundles[] = new DoctrineBundleFixturesBundleDoctrineFixturesBundle();$bundles[] = 新的 LiipFunctionalTestBundleLiipFunctionalTestBundle();}返回 $bundles;}}

当我更新我的应用程序时,我会运行 php composer.phar install --no-dev --optimize-autoloader.这会安装开发环境不需要的所有要求,然后清除缓存.

但是,清除缓存失败并显示以下消息:

<上一页>PHP 致命错误:在第 29 行的/my/project/app/AppKernel.php 中找不到类 'DoctrineBundleFixturesBundleDoctrineFixturesBundle'脚本 SensioBundleDistributionBundleComposerScriptHandler::clearCache 处理 post-install-cmd 事件以异常终止[运行时异常]执行'cache:clear --no-warmup'"命令时出错.

这不仅仅是 Doctrine Fixtures Bundle 的问题.如果我更改顺序以便 Liip 功能测试包首先出现,那么错误将与该包有关.

为什么我会看到这个错误?为什么 Symfony 尝试访问这些包,即使我们明确不在开发环境中(注意 --no-dev 作曲家标志)?我可以做些什么来消除这种情况,而不必在生产机器上安装所有开发依赖项?

解决方案

因为symfony默认的env是devcomposer --no-dev只告诉composer不要安装开发需求,symfony 不知道环境.使用 SYMFONY_ENV=prod 环境变量.http://symfony.com/doc/current/cookbook/deployment/tools.html#c-install-update-your-vendors

例如:$ SYMFONY_ENV=prod php composer.phar install --no-dev --optimize-autoloader

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 SymfonyBundleFrameworkBundleFrameworkBundle(),
            new SymfonyBundleSecurityBundleSecurityBundle(),
            // ...
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle();
            $bundles[] = new SensioBundleDistributionBundleSensioDistributionBundle();
            $bundles[] = new SensioBundleGeneratorBundleSensioGeneratorBundle();
            $bundles[] = new DoctrineBundleFixturesBundleDoctrineFixturesBundle();
            $bundles[] = new LiipFunctionalTestBundleLiipFunctionalTestBundle();
        }

        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 'DoctrineBundleFixturesBundleDoctrineFixturesBundle' not found in /my/project/app/AppKernel.php on line 29
Script SensioBundleDistributionBundleComposerScriptHandler::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?

解决方案

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

相关文章