コンテンツにスキップ
Nibiru docsv0.9.2

設定 & 設定

Nibiru は環境固有の INI ファイルから設定を読み込みます。

Stable Reading time ~ 1 min Edit on GitHub

Nibiruの設定層は意図的にシンプルです:起動時に単一のsettings.<env>.iniファイルを解析し、シングルトンを通じて公開し、モジュールが独自のINIsをレジストリを通じて追加できるようにします。

APPLICATION_ENV 環境変数は、どのファイルを読み込むかを選択します:

APPLICATION_ENVファイル読み込み
development(デフォルト)application/settings/config/settings.development.ini
stagingapplication/settings/config/settings.staging.ini
productionapplication/settings/config/settings.production.ini
Terminal window
export APPLICATION_ENV=production
./nibiru -mi production

APPLICATION_ENV が設定されていない場合、Nibiru はデフォルトで development を使用します。

一般的な settings.<env>.ini:

[ENGINE]
templates = "/../../application/view/templates/"
templates_c = "/../../application/view/templates_c/"
cache = "/../../application/view/cache/"
config_dir = "/../../application/view/configs/"
caching = false
debug = true
error.controller = "error"
[AUTOLOADER]
iface.pos[] = "users"
iface.pos[] = "cms"
class.pos[] = "users"
class.pos[] = "cms"
[SETTINGS]
page.url = "https://my-app.local"
navigation = "/../../application/settings/config/navigation/main.json"
modules.path = "/../../application/module/"
entries.per.page = 25
smarty.css[] = "/public/css/app.css"
smarty.js[] = "/public/js/app.js"
timezone = "Europe/Vienna"
[DATABASE]
driver = "pdo"
hostname = "localhost"
port = 3306
username = "nibiru"
password = "secret"
basename = "nibiru_dev"
encoding = "utf8mb4"
is.active = true
[SECURITY]
password_hash = "change-me-at-once"
[GENERATOR]
database = true
database.overwrite = true
[EMAIL]
host = "smtp.example.com"
port = 587
encryption = "tls"
username = "noreply@example.com"
password = "smtp-secret"
from = "Nibiru <noreply@example.com>"
[NIBIRU_ROUTING]
; optional regex routes — see /core/routing/
[NIBIRU_SECURITY]
password_hash = "another-salt-for-AES_DECRYPT"
$cfg = \Nibiru\Config::getInstance()->getConfig();
$cfg['DATABASE']['driver']; / 'pdo'
$cfg['SETTINGS']['page.url']; / 'https://my-app.local'

深くネストされた設定には、View インターフェースから型付けされた定数を使用してください。

$cfg[\Nibiru\View::NIBIRU_SETTINGS]['smarty.css']; / ['/public/css/app.css']

各モジュールの application/module/<name>/settings/ 以下には独自の INI ファイルを配置できます。Registry はそれらを自動的に取り込み、以下の通りに公開します:

$users = \Nibiru\Registry::getInstance()->loadModuleConfigByName('users');
$users->session_lifetime; / from [USERS] section in users.ini

レジストリは <module>.<env>.ini<module>.ini よりも優先します。これにより、環境ごとのオーバーライドが無料で利用できます。

INI ファイルはプレーンテキストです。2つの生産安全なオプションがあります:

settings.production.ini をバージョン管理に含め、プレースホルダーを使用し、実際の値をランタイム時に環境変数から注入します。

[DATABASE]
password = "${DB_PASSWORD}"

その後、コンテナ/CIで展開します。

Terminal window
envsubst < settings.production.ini.tpl > settings.production.ini

settings.production.ini をリポジトリの外に保ち、デプロイ時にシンボリックリンクを作成します。

Terminal window
ln -s /etc/nibiru/settings.production.ini \
application/settings/config/settings.production.ini

フレームワークは、ファイルがどこに存在するかを気にしない限り、parse_ini_file が読み取れるかさえあればよいです。

設定は起動時に1回だけ、Settings静的クラスに読み込まれます。INIファイルの変更には、リクエストサイクルが必要です(または長時間実行されるスクリプトでは、明示的なSettings::setConfig(\Nibiru\Config::getEnv())呼び出しが必要です)。

  • パス /../../ の指定。 フレームワークの INI パスは フレームワークディレクトリを基準に 相対パスです。プロジェクトルートに移動するためには /../../ で始める必要があります。はい、見た目が奇妙ですが、機能します。
  • ブール値の解析。 parse_ini_file は許容的です — true, on, 1 すべてが 1 になります;false, off, 0, "" すべてが 0 になります。リテラルな "true" を必要とする場合は、トリプルクォートで囲んでください。
  • 配列値。 [] 構文を使用してください (smarty.css[] = …) — Nibiru はこれらが配列であることを依存しています。