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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Symfony 2:从命令内部访问更新的配置脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我们正在创建一个依赖于其他命令来生成数据库并构建其模式的命令.到目前为止,我们已经成功地读取了config.yML文件,添加了我们的新连接信息,并将文件写回来.在同一个命令中,我们然后尝试运行symfony命令来创建数据库和模式:update.这是我们遇到问题的地方.我们收到以下错误

如果我们第二次运行该命令则没有错误,因为更新的配置文件被新加载到应用程序中.如果我们在写入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);
   }
}
这不起作用的原因是因为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
{
}

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

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

然后针对将与新DIC一起运行的新应用程序运行子命令:

$application->run($db_create_input,$output);

免责声明:这感觉非常hacky.我愿意听取其他解决方案/解决方法.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Symfony 2:从命令内部访问更新的配置全部内容,希望文章能够帮你解决php – Symfony 2:从命令内部访问更新的配置所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。