composer.jsonやターミナル上で以下のようにして環境変数指定できますがバッチ内で環境変数指定する方法はないでしょうか?
php artisan ide-helper:generate --env=develop
class PostScriptsCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'post:cmd {argument : install or update}'; /** * The console command description. * * @var string */ protected $description = 'composer.json実行時にpost-update-cmdまたはpost-install-cmdで実行したい処理を実行する'; private $environment; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); $this->environment = \App::environment(); } /** * Execute the console command. * * @return mixed */ public function handle() { if (!isRequireDev()) { $this->error($this->environment . '環境では実行できません。'); return; } switch ($this->argument('argument')) { case 'install': $this->install(); break; case 'update': $this->update(); break; default: $this->error('php artisan post:cmd -h でプログラム実行条件を確認してください。'); } } /** * composer.jsonのpost-install-cmdディレクティブ内で実行したい処理を実行。 */ private function install() { $env = $this->argument('argument'); $this->info('post-' . $env . '-cmdを実行します。'); } /** * composer.jsonのpost-update-cmdディレクティブ内で実行したい処理を実行。 */ private function update() { $argument = $this->argument('argument'); $this->info('post-' . $argument . '-cmdを実行します。'); $this->call('ide-helper:generate'); // $this->call('ide-helper:meta --env=local'); // 認識されない、、、 // $this->call('ide-helper:generate --env=' . $env); // $this->call('ide-helper:meta--env=' . $env); } }
ide-helperはrequire-devに設定していて、本番環境では読み込まないようにしているので
composer installやupdate実行時に、--env=***で実行しないと以下のエラーがでてしまいます。
> php artisan ide-helper:generate Exception: No application encryption key has been specified. Skipping \Illuminate\Support\Facades\Crypt. Exception: Database [user_server] not configured. Skipping \Illuminate\Support\Facades\Schema. Exception: No application encryption key has been specified. Skipping \Illuminate\Support\Facades\Crypt. Exception: Database [user_server] not configured. Skipping \Illuminate\Support\Facades\Schema.
AppServiceProviderでenvがlocalとdevelopのみ、require-devを読み込むようにしています。
/* |-------------------------------------------------------------------------- | 開発環境と検証環境のみ必要パッケージ等を読み込む。composer.jsonのrequire-devに依存する。 | require-devで導入するパッケージ等はconfig/xxx/app.phpへ追記して読み込む。 |-------------------------------------------------------------------------- */ if (isRequireDev()) { $providers = config(\App::environment() . '.app.providers', []); foreach ($providers as $index => $provider) { if (class_exists($provider)) { $this->app->register($provider); } } $aliases = config('develop.app.aliases', []); foreach ($aliases as $alias => $facade) { if (class_exists($facade)) { $this->app->alias($alias, $facade ); } } }
function isRequireDev() { return in_array(\App::environment(), ['local', 'develop']); }
つまり、--env=localか--env=developを付与してphp artisan ide-helper:generateを実行しないとそんなクラスないってことでエラーがでます。
これを回避する良い方法は何かないでしょうか?
composer.jsonのpost-update-cmdで冒頭のConsoleを読み込んでいます。
"post-update-cmd": [ "php artisan post:cmd update", "php artisan optimize" ]
jsonの中でenvを取得できればいいのですが、、、
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。