【问题标题】:Symfony 1.4 set default application for command line tasksSymfony 1.4 为命令行任务设置默认应用程序
【发布时间】:2012-05-16 13:06:44
【问题描述】:

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

在 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;
}

好像是按字母顺序排列的……

谢谢。

【问题讨论】:

  • 后端是您创建的第一个应用程序吗? (即:前端之前)
  • 不,首先我创建了前端,然后是后端
  • 但是为什么要选择运行哪个应用程序doctrine:build,因为这个任务会为整个项目生成学说类?
  • 一个应用程序(frontend) 正在使用撇号,它添加了一些构建类所需的代码,而backend 不包括撇号。

标签: php symfony1 symfony-1.4 command-line-interface


【解决方案1】:

检查您的任务的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

【讨论】:

  • 这不是自定义任务,我不想更改 symfony/doctrine 源代码。
  • 哦,好吧,我不知道它与核心任务有关。
  • 我想避免这样做,因为有人可能会调用核心任务
【解决方案2】:

当您在没有特定应用程序的情况下运行 cache:clear 任务时,Symfony 会清除所有应用程序的缓存。这可以在source of this task 中观察到。对于提供默认应用程序的任务(您自己创建的),您可以简单地将其更改为您想要的任何内容。

【讨论】:

  • 好吧,也许cache:clear 命令不是一个很好的例子,我遇到的问题是关于教义:build 命令
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2014-04-24
    • 2013-09-12
    • 1970-01-01
    • 2011-10-18
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2017-09-04
    相关资源
    最近更新 更多