Symfony 1.4:以编程方式设置 CLI 环境

发布于 2024-08-26 22:06:43 字数 196 浏览 2 评论 0原文

我有一个基于 Symfony 1.4 构建的项目,它有多个环境 - 每个开发人员都在其本地计算机上安装了自己的副本,因此有自己的环境。

我可以在 index.php 中动态设置环境,但是如何为 CLI 任务执行此操作?

我当然可以在所有任务上使用 --env=myenvironment,但我希望它能够使用与我在 index.php 中相同的逻辑

I have a project built on Symfony 1.4 and it has multiple environments - each developer has their own copy installed on their local machine and therefore has their own environment.

I am able to dynamically set the environment in index.php, but how can I do this for CLI tasks?

I could of course use --env=myenvironment on all tasks, but I would prefer it to be able to use the same logic as I have in index.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独闯女儿国 2024-09-02 22:06:44

sfContext::getInstance()->getConfiguration()->getEnvironment()

sfContext::getInstance()->getConfiguration()->getEnvironment()

熊抱啵儿 2024-09-02 22:06:44

你可以在你的任务中做类似的事情:

class MyTask extends sfBaseTask
{
  protected function configure()
  {
    // Env config file
    $file = sfConfig::get("sf_config_dir") . DIRECTORY_SEPARATOR . "environment.cfg";
    $env = (file_exists($file) ? file_get_contents($file) : "prod");

    $this->addOptions(array(
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', $env),
  }
}

上面应该有效;如果environment.cfg 文件不存在,则默认为“prod”环境。这还假设您在文件中只有环境(例如“prod”、“dev”、“slappythefish”等)。

You could do something similar to this in your task perhaps:

class MyTask extends sfBaseTask
{
  protected function configure()
  {
    // Env config file
    $file = sfConfig::get("sf_config_dir") . DIRECTORY_SEPARATOR . "environment.cfg";
    $env = (file_exists($file) ? file_get_contents($file) : "prod");

    $this->addOptions(array(
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', $env),
  }
}

The above should work; it defaults to the 'prod' environment if the environment.cfg file doesn't exist. This also assumes that you have just the environment in the file (eg 'prod', 'dev', 'slappythefish' etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文