Taskにも時々application名を指定してあげる

うっかり忘れるのでメモ。


Taskを作る際、symfony generate:task して処理を書くわけですが、sfConfig::get()で取得できる設定が妙に少なくて焦りました。

以下 var_dump(sfConfig::getAll()); の結果です。

array(17) {
  ["sf_symfony_lib_dir"]=>
  string(33) "/path/to/symfony1.2/lib"
  ["sf_root_dir"]=>
  string(43) "/home/yoshihi6/my-project"
  ["sf_apps_dir"]=>
  string(48) "/home/yoshihi6/my-project/apps"
  ["sf_lib_dir"]=>
  string(47) "/home/yoshihi6/my-project/lib"
  ["sf_log_dir"]=>
  string(47) "/home/yoshihi6/my-project/log"
  ["sf_data_dir"]=>
  string(48) "/home/yoshihi6/my-project/data"
  ["sf_config_dir"]=>
  string(50) "/home/yoshihi6/my-project/config"
  ["sf_test_dir"]=>
  string(48) "/home/yoshihi6/my-project/test"
  ["sf_doc_dir"]=>
  string(47) "/home/yoshihi6/my-project/doc"
  ["sf_plugins_dir"]=>
  string(51) "/home/yoshihi6/my-project/plugins"
  ["sf_web_dir"]=>
  string(47) "/home/yoshihi6/my-project/web"
  ["sf_upload_dir"]=>
  string(55) "/home/yoshihi6/my-project/web/uploads"
  ["sf_cache_dir"]=>
  string(49) "/home/yoshihi6/my-project/cache"
  ["sf_orm"]=>
  string(6) "propel"
  ["sf_admin_module_web_dir"]=>
  string(15) "/sfPropelPlugin"
  ["sf_environment"]=>
  string(3) "dev"
  ["sf_logging_enabled"]=>
  bool(false)
}

application名を特に指定していないため、setting.yml, routing.yml なんかが読み込まれないということではあるのですが、

  • (sf_root_dir)/config/app.yml すら読み込まれない
  • log/ 以下にログが出力されない

あたりが一見不可解におもいました。ymlに書いた設定を参照したい、あるいはログを出したいという時点でapplication名の指定が必要なんですね。

ちなみに、application名を指定するのはconfigureメソッド内にあるsfCommandOption('application',〜〜)の第5引数で指定すればよいです。

  protected function configure()
  {
    // 〜〜〜

    $this->addOptions(array(
-      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
+      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'backend'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
      // add your own options here
    ));

    // 〜〜〜
  }