symfonyの設定ファイルのカスケードルール
symfonyの設定ファイルは「カスケード」機能があって、下位のレベルに配置した設定ファイルで上位の設定を上書きしたりできます。
基本的な説明は、公式サイトの以下のページを参照してください。
基本ルールは「symfonyの初期設定」→「プロジェクトグローバル」→「アプリケーション」→「モジュール」と上書きされていくのですが、ここに「プラグイン」が入ると多少ややこしくなります。
プラグインも含めた設定ファイルのカスケードルール
symfonyでプラグインを作成・インストールした場合、有効になっているプラグインのconfigディレクトリにある設定ファイルも自動読み込みの対象になります(config_handlers.ymlで設定しているもの)。
この場合のカスケードルールについては、sfApplicationConfigration::getConfigPaths()メソッドの以下の部分を見ると分かります。
<?php public function getConfigPaths($configPath) { $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath); $files = array( $this->getSymfonyLibDir().'/config/'.$globalConfigPath, // symfony ); foreach ($this->getPluginPaths() as $path) { if (is_file($file = $path.'/'.$globalConfigPath)) { $files[] = $file; // plugins } } $files = array_merge($files, array( $this->getRootDir().'/'.$globalConfigPath, // project $this->getRootDir().'/'.$configPath, // project sfConfig::get('sf_app_dir').'/'.$globalConfigPath, // application sfConfig::get('sf_app_cache_dir').'/'.$configPath, // generated modules )); foreach ($this->getPluginPaths() as $path) { if (is_file($file = $path.'/'.$configPath)) { $files[] = $file; // plugins } } $files[] = sfConfig::get('sf_app_dir').'/'.$configPath; // module $configs = array(); foreach (array_unique($files) as $file) { if (is_readable($file)) { $configs[] = $file; } } return $configs; }
このコードを読むと、特定の名前の設定ファイルに対して、以下の順番で各設定ファイルを読み込んでいることが分かります。
- symfonyライブラリの初期値(symfony/lib/config/config/hoge.yml)
- すべてのプラグインのグローバルconfig(プラグイン/config/hoge.yml)
- プロジェクトのグローバルconfig(プロジェクトルート/config/hoge.yml)
- プロジェクト直下の対象パスのconfig(プロジェクトルート/(パス)/hoge.yml)
- アプリケーションのグローバルconfig(アプリケーション/config/hoge.yml)
- アプリケーションキャッシュ直下の対象パスのconfig(アプリケーションキャッシュ/(パス)/hoge.yml)
- すべてのプラグインの対象パスのconfig(プラグイン/(パス)/hoge.yml)
- アプリケーション直下の対象パスのconfig(アプリケーション/(パス)/hoge.yml)
ここで、「グローバルconfig」とは、「config/hoge.yml」というパスのもので、「対象パスのconfig」というのは、主に「modules/(モジュール名)/config/hoge.yml」を指します。