大佬教程收集整理的这篇文章主要介绍了php – Symfony 2:从命令内部访问更新的配置,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我们第二次运行该命令则没有错误,因为更新的配置文件被新加载到应用程序中.如果我们在写入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 scheR_363_11845@a update script arguments $args = array( 'command' => 'doctrine:scheR_363_11845@a:update','--em' => $site_name,'--force' => true ); $update_scheR_363_11845@a_input = new ArrayInput($args); //Run the symfony database create command $this->getApplication()->run($update_scheR_363_11845@a_input,$output); } }
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,请注明来意。