Symfony 1.4 为命令行任务设置默认应用程序


Symfony 1.4 set default application for command line tasks

我正在寻找以下问题的解决方案:

在symfony 1.4项目中,我有2个应用程序:frontendbackend。每当我运行像php symfony doctrine:build --all-classes这样的symfony cli任务时,symfony都会使用默认backend应用程序。

如何让symfony cli使用frontend作为默认应用程序?

我知道我可以运行与php symfony --application=frontend doctrine:build --all-classes相同的命令,但必须有一种方法可以使它默认使用 frontend 应用程序。

编辑:

lib/vendor/symfony/lib/task/sfBaseTask.class.php中找到了这种方法

/**
 * Returns the first application in apps.
 *
 * @return string The Application name
 */
protected function getFirstApplication()
{
    if (count($dirs = sfFinder::type('dir')->maxdepth(0)->follow_link()->relative()->in(sfConfig::get('sf_apps_dir'))))
    {
        return $dirs[0];
    }
   return null;
}

似乎顺序是按字母顺序排列的...

谢谢。

签入任务configure方法,您应该看到类似以下内容:

new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'backend'),

backend替换为 frontend

编辑:

根据你的发现(关于getFirstApplication),最好的解决方案是创建你自己的任务(/lib/task/myDoctrineBuildTask.class.php年),扩展当前的教义任务。然后在 configure 方法中定义frontend应用程序:

class myDoctrineBuildTask extends sfDoctrineBuildTask
{
  /**
   * @see sfTask
   */
  protected function configure()
  {
    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', 'frontend'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Whether to force dropping of the database'),
      new sfCommandOption('all', null, sfCommandOption::PARAMETER_NONE, 'Build everything and reset the database'),
      new sfCommandOption('all-classes', null, sfCommandOption::PARAMETER_NONE, 'Build all classes'),
      new sfCommandOption('model', null, sfCommandOption::PARAMETER_NONE, 'Build model classes'),
      new sfCommandOption('forms', null, sfCommandOption::PARAMETER_NONE, 'Build form classes'),
      new sfCommandOption('filters', null, sfCommandOption::PARAMETER_NONE, 'Build filter classes'),
      new sfCommandOption('sql', null, sfCommandOption::PARAMETER_NONE, 'Build SQL'),
      new sfCommandOption('db', null, sfCommandOption::PARAMETER_NONE, 'Drop, create, and either insert SQL or migrate the database'),
      new sfCommandOption('and-migrate', null, sfCommandOption::PARAMETER_NONE, 'Migrate the database'),
      new sfCommandOption('and-load', null, sfCommandOption::PARAMETER_OPTIONAL | sfCommandOption::IS_ARRAY, 'Load fixture data'),
      new sfCommandOption('and-append', null, sfCommandOption::PARAMETER_OPTIONAL | sfCommandOption::IS_ARRAY, 'Append fixture data'),
    ));
    $this->namespace = 'mydoctrine';
    $this->name = 'build';
    $this->briefDescription = 'Generate code based on your schema';
    $this->detailedDescription = ''; // feel free to re-add all the doc
  }
}

然后,使用以下方法启动构建: php symfony mydoctrine:build --all-classes

当你在没有特定应用程序的情况下运行cache:clear任务时,Symfony会清除所有应用程序的缓存。这可以在此任务的来源中观察到。对于提供默认应用程序的任务(您自己创建的任务),只需将其更改为所需的任何内容即可。

这已经晚了四年,希望Symfony 1.x现在没有被太多使用,但文件系统决定了什么是默认应用程序。应用程序查询应用程序目录,第一个文件夹用作默认应用程序。因此,您可以通过修改文件系统来破解它,但强烈建议不要这样做。