Naify\App\AppConfig クラス

アプリ設定 — Web アプリの起動設定

Web アプリを起動するために必要な、変わりにくい基底情報をまとめて保持する。 業務設定や外部サービス設定は持たず、アプリ識別子・ディレクトリ・URL basePath・ session 開始前の設定だけを扱う。

値は no-arg 生成時に可能な範囲で自動導出され、 必要なものだけ setter で明示上書きできる。

使用例

$config = new ApplicationConfig();
$config
    ->setAppId('naify.dev')
    ->setAppVersion('current')
    ->setSystemDir(__DIR__)
    ->setConfigDir(__DIR__ . '/config')
    ->setPublicDir(dirname(__DIR__) . '/public_html')
    ->setBasePath((string) ($_SERVER['SCRIPT_NAME'] ?? '/'));

// Naify facade から生成することもできる
\Naify\Naify::createConfig();

プロパティ

名前型説明
private $appId string アプリケーション識別子。cookie 名やログ識別子の基点に使える
private $appVersion string アプリケーション表示用バージョン。診断表示やヘッダー表示向け
private $systemDir string Bootstrap や routes, views を抱える system ディレクトリ
private $publicDir string front controller を公開する public ディレクトリ
private $configDir string database.php などアプリ設定ファイルを置く config ディレクトリ
private $cacheDir string 再利用前提の中間生成物を置く cache ディレクトリ
private $logsDir string アプリケーションログの出力先ディレクトリ
private $tempDir string 短命で破棄してよい作業ファイル用ディレクトリ
private $basePath string URL 側の basePath。'/' または '/app' のような形式
private $sessionConfig array session_name や ini 相当の開始前設定

メソッド

メソッド説明
public __construct(): void 可能な範囲で初期値を自動導出して生成する
public snapshot(): static 現在の設定を snapshot として複製する
Naify::createWebApp() などが App へ設定を渡す時に、以後の変更が実行中 App へ
混ざらないよう clone で切り離す。
public reset(): static 設定を自動導出ベースの初期状態へ戻す
config object を再利用する時に、前回の積み残しを消して
sample/bootstrap を素直に書けるようにする。
current の既定値は「systemDir を起点に最低限動く」ことを優先し、
`config/`, `_cache/`, `_logs/`, `_tmp/` を規約的に補う。
public getAppId(): string アプリ識別子を取得する
public setAppId(string $appId): static アプリ識別子を設定する
public getAppVersion(): string アプリバージョンを取得する
public setAppVersion(string $appVersion): static アプリバージョンを設定する
public getSystemDir(): string system ディレクトリを取得する
Bootstrap や routes, views などを抱えるアプリ本体のディレクトリを返す。
public setSystemDir(string $systemDir): static system ディレクトリを設定する
既定の `configDir` がまだ自動導出値のままなら、
`systemDir/config` へ追従して一緒に更新する。
明示的に `setConfigDir()` された後は、その値を尊重して変更しない。
public getPublicDir(): string public ディレクトリを取得する
public setPublicDir(string $publicDir): static public ディレクトリを設定する
public getConfigDir(): string config ディレクトリを取得する
`database.php` など、アプリ起動時に読む設定ファイル群の置き場を返す。
public setConfigDir(string $configDir): static config ディレクトリを設定する
public getCacheDir(): string cache ディレクトリを取得する
public setCacheDir(string $cacheDir): static cache ディレクトリを設定する
public getLogsDir(): string logs ディレクトリを取得する
public setLogsDir(string $logsDir): static logs ディレクトリを設定する
public getTempDir(): string temp ディレクトリを取得する
短命で消えてよい一時ファイルの置き場を返す。
public setTempDir(string $tempDir): static temp ディレクトリを設定する
public getBasePath(): string URL basePath を取得する
public setBasePath(string $basePath): static URL basePath を設定する
`/myapp` のような basePath 文字列だけでなく、
`$_SERVER['SCRIPT_NAME']` のような front controller のファイル付きパスも受け取れる。
末尾が .php の場合だけファイル名を除去し、/api/v1.0 等のディレクトリ名は保持する。
public setSessionConfig(string $key, mixed $value): static session 開始前の設定値を 1 件セットする
public hasSessionConfig(string $key): bool 指定キーの session 設定が存在するか返す
public getSessionConfig(?string $key, mixed $default): mixed session 設定を取得する
キーを省略すると全設定配列を返す。
public replaceSessionConfig(array $sessionConfig): static session 設定を配列ごと置き換える
public toArray(): array 現在の設定を配列として返す
debug dump や API reference で「いま何が積まれているか」を
ひと目で観測したい時の出口。
private detectSystemDir(): string 現在の実行ディレクトリから systemDir の初期値を求める
private detectPublicDir(string $systemDir): string 実行中のスクリプトから publicDir の初期値を求める
Web 実行時は `SCRIPT_FILENAME` を優先し、
取れない場合だけ `systemDir` の親にある `public_html` を既定とする。
private detectBasePath(): string 実行された front controller の公開パスから basePath を求める
private normalizeBasePathInput(string $path): string basePath 入力を正規化する
`/app` のようなディレクトリパスと、
`/app/index.php` のようなファイル付きパスの両方を受け付ける。
private normalizeDir(string $path): string ディレクトリパスを正規化する
Windows / Unix の差を吸収しつつ、末尾スラッシュを落とす。
ただし root そのものを厳密に表す用途ではないため、
current の app 設定では「通常の directory path」を前提にする。
private normalizeBasePath(string $path): string basePath 文字列を正規化する