1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel-Dotenv-Diff-Checkerの使い方

Posted at

はじめに

この記事は私が趣味で公開しているLaravel-Dotenv-Diff-Chekerの使い方に関する記事です。
変なパッケージを入れることに抵抗のある方は、このページをそっと閉じてください。

概要

Laravelではdotenvを利用した環境変数の読み込みをサポートしています。
大抵のLaravel環境に存在している.envファイルが該当するファイルです。
開発過程では.envに必要な環境変数を設定し、データベースやメールの設定を行うわけです。
ある程度Laravelに慣れてきたり、開発が進むにつれてLaravelプロジェクトで利用する環境変数が増えていくことになります。
私自身、サクッと環境変数を定義できるので、とても重宝しています。

でも環境変数が増えた時、ついうっかりチームメンバへの共有が漏れてしまったり、本番環境へデプロイする際に足りなかったりすることはないでしょうか?
普通にdiffで.envと.env.exampleを比較して必要なものを調べてもいいのですが、お使いのフレームワークはLaravelです。
せっかくなので、artisanコマンドとして.envと.env.exampleの差分を調べることができるパッケージを作りました。

GitHubページ: Laravel-Dotenv-Diff-Checkerのソースコードはこちら

お使いのLaravelプロジェクトに追加する方法

composerコマンドでライブラリを追加してください。

$ cd /your/laravel/project/root # <- Laravelのプロジェクトのディレクトリで、
$ composer require junichimura/laravel-dotenv-diff-checker # <- 左記のコマンドを実行する。
Using version ^0.0.4 for junichimura/laravel-dotenv-diff-checker
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing junichimura/laravel-dotenv-diff-checker (v0.0.4): Loading from cache
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: junichimura/laravel-dotenv-diff-checker # <- プロジェクトにされたらこのような表示が見えるはず。
Discovered Package: laravel/tinker
Package manifest generated successfully.
$ 

artisanコマンドに追加されたか確認してみましょう。

$ php artisan | grep dotenv
 dotenv
  dotenv:check           Compare file [.env] and [.env.example] # <- 追加されている。
$ 

インストールされたコマンドを実行してみる

▼.env.exampleに定義済みの環境変数で、.envで未定義のものがある場合

$ php artisan dotenv:check
=== Compare file [.env] and [.env.example] ===
An undefined environment variable was found in file [.env].
CACHE_DRIVER=file # <- 未定義の環境変数
SESSION_DRIVER=redis # <- 未定義の環境変数

 Do you want to add the above environment variables to file [.env]?(Append to the end) (yes/no) [no]:
 > y # <- 上記の未定義環境変数を.envに追記する場合は「yes」、しない場合は「no」

Environment variables have been added to file [.env]. Also, a backup file [.env.back] has been created.
=== EXIT ===
$ 

▼.env.exampleに定義済みの環境変数がすべて.envで定義されている場合

$ php artisan dotenv:check
=== Compare file [.env] and [.env.example] ===
There were no undefined environment variables.
=== EXIT ===
$ 

▼【逆に】.envに定義済みの環境変数がすべて.env.exampleで定義されているか確認する場合
コマンドのヘルプを見てみます。

$ php artisan dotenv:check --help
Usage:
  dotenv:check [options]

Options:
  -m, --main[=MAIN]         [default: ".env"] # <- 差分を確認したいファイルの指定
  -e, --example[=EXAMPLE]   [default: ".env.example"] # <- 環境変数のオリジナルファイルの指定
      --separator_crlf
      --separator_cr
  -h, --help               Display this help message
  -q, --quiet              Do not output any message
  -V, --version            Display this application version
      --ansi               Force ANSI output
      --no-ansi            Disable ANSI output
  -n, --no-interaction     Do not ask any interactive question
      --env[=ENV]          The environment the command should run under
  -v|vv|vvv, --verbose     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Compare file [.env] and [.env.example]
$ 

つまり、以下のコマンドを実行すると.env.exampleに未定義の環境変数がないか調べられます。

$ php artisan dotenv:check -m .env.example -e .env

終わり

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?