Symfony 2:从命令内部访问更新的配置

lew*_*sid 5 php symfony doctrine-orm symfony-2.1

我们正在创建一个依赖于其他命令来生成新数据库并构建其模式的命令.到目前为止,我们已经成功地读取了config.yml文件,添加了我们的新连接信息,并将文件写回来.在同一个命令中,我们然后尝试运行symfony命令来创建数据库和模式:update.这是我们遇到问题的地方.我们收到以下错误:

[InvalidArgumentException]名为"mynewdatabase"的Doctrine ORM Manager不存在.

如果我们第二次运行该命令则没有错误,因为更新的配置文件被新加载到应用程序中.如果我们在写入config.yml文件后手动运行doctrine命令,它也可以正常运行.

我们认为,在我们运行数据库创建和更新命令的命令中,它仍然使用存储在内存中的当前内核版本的config.yml/database.yml.我们尝试了许多不同的方法来重新初始化应用程序/内核配置(调用shutdown(),boot()等)而没有运气.这是代码:

namespace Test\MyBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

class GeneratorCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
           ->setName('generate')
           ->setDescription('Create a new database.')
           ->addArgument('dbname', InputArgument::REQUIRED, 'The db name')
        ;
    }

   /*
      example: php app/console generate mynewdatabase
   */
   protected function execute(InputInterface $input, OutputInterface $output)
   {
      //Without this, the doctrine commands will prematurely end execution
      $this->getApplication()->setAutoExit(false);

      //Open up app/config/config.yml
      $yaml = Yaml::parse(file_get_contents($this->getContainer()->get('kernel')->getRootDir() .'/config/config.yml'));

      //Take input dbname and use it to name the database
      $db_name = $input->getArgument('dbname');

      //Add that connection to app/config/config.yml
      $yaml['doctrine']['dbal']['connections'][$site_name] = Array('driver' => '%database_driver%', 'host' => '%database_host%', 'port' => '%database_port%', 'dbname' => $site_name, 'user' => '%database_user%', 'password' => '%database_password%', 'charset' => 'UTF8');
      $yaml['doctrine']['orm']['entity_managers'][$site_name] = Array('connection' => $site_name, 'mappings' => Array('MyCustomerBundle' => null));

      //Now put it back
      $new_yaml = Yaml::dump($yaml, 5);
      file_put_contents($this->getContainer()->get('kernel')->getRootDir() .'/config/config.yml', $new_yaml);

      /* http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command */

      //Set up our db create script arguments
      $args = array(
         'command'      => 'doctrine:database:create',
         '--connection'   => $site_name,
      );
      $db_create_input = new ArrayInput($args);

      //Run the symfony database create arguments
      $this->getApplication()->run($db_create_input, $output);

      //Set up our schema update script arguments
      $args = array(
         'command'   => 'doctrine:schema:update',
         '--em'      => $site_name,
         '--force'   => true
      );
      $update_schema_input = new ArrayInput($args);

      //Run the symfony database create command
      $this->getApplication()->run($update_schema_input, $output);
   }
}
Run Code Online (Sandbox Code Playgroud)

MDr*_*tte 5

这不起作用的原因是 DIC 经过编译过程,然后写入 PHP 文件,然后将其包含到当前运行的进程中。您可以在这里看到:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Kernel.php#L562

如果您更改服务定义,然后尝试“重新启动”内核来编译这些更改,它将不会再次包含已编译的文件(require_once),并且只会使用旧的编译文件创建已包含的 DIC 类的另一个实例服务定义。

我能想到的解决这个问题的最简单方法是创建一个空的 Kernel 类,它只是扩展您的 AppKernel。就像这样:

<?php

namespace Test\MyBundle\Command;

class FakeKernel extends \AppKernel
{
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的命令中,您可以在保存新的服务定义后启动此内核,并且它将使用“FakeKernel”名称作为文件名的一部分重新编译新的 DIC 类,这意味着它将被包含在内。像这样:

$kernel = new \Test\MyBundle\Command\FakeKernel($input->getOption('env'), true);
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
Run Code Online (Sandbox Code Playgroud)

然后,您针对这个将使用新 DIC 运行的新应用程序运行子命令:

$application->run($db_create_input, $output);
Run Code Online (Sandbox Code Playgroud)

免责声明:这感觉很hacky。我愿意听到其他解决方案/解决方法。