実行環境
- symfony 2.7
symfony2.7を使ったプロジェクトをテストサーバーにデプロイしようとcomposer --no-dev installを実行した際
下記のエラーが発生、少しひっかかったのでメモ
PHP Fatal error: Class 'Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle' not found in /tmp/build_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/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.
解決法
- export SYMFONY_ENV=prod を実行する
理由
composer install --no-devで実行すると
require-devのmoduleが読み込まれません。
なのでrequire-devで読み込まれているmoduleを使用する処理が失敗します。
symfonyのインストール時の実行環境判断はSYMFONY_ENVという環境変数で行われているようで、これがない場合は開発環境dev
の扱いになります。
post-install-cmd時に行われているキャッシュクリアの処理の際に、AppKernel.phpが実行され、その処理の中でdevかtestの場合に読まれるもの中にrequire-devに含まれているsensio/generator-bundleが入っており
落ちる模様。
composer.phar
require-dev
"require-dev": {
"sensio/generator-bundle": "~2.3",
"symfony/phpunit-bridge": "~2.7",
"phpunit/phpunit": "3.7.*"
},
scripts
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
]
},
AppKernel.php
if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
最後に
- 一応公式サイト(http://docs.symfony.gr.jp/symfony2/cookbook/deployment/tools.html) class not found出た場合はということが書いてあるけど、なんで落ちるのかが殆ど説明がなかったので調べてみた。composerとsymfonyで二重に実行環境指定しておくのはちょっと怖い。
ローカルではオプション未指定(--dev)でインストール、SYMFONY_ENVは設定しなくてもdev
なので設定を見落としても動いてしまう・・・
日本語公式からコピペ
Caution
もし “class not found” というエラーをこのステップで発生したら、 本番環境で post-install-cmd スクリプトが実行される前に export SYMFONY_ENV=prod を実行する必要があります。