加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 有没有办法在symfony2中自动更新AppKernel?

发布时间:2020-12-13 17:13:59 所属栏目:PHP教程 来源:网络整理
导读:也许类似于generate:bundle命令(在生成包提示更新AppKernel之后)或者Composer(使用您安装的依赖项更新自动加载). 我想获得与generate:bundle类似的功能,但是我想添加一个我刚下载的包,而不是手动编辑AppKernel,而不是创建一个新包. 解决方法 我找不到扩展
也许类似于generate:bundle命令(在生成包提示更新AppKernel之后)或者Composer(使用您安装的依赖项更新自动加载).

我想获得与generate:bundle类似的功能,但是我想添加一个我刚下载的包,而不是手动编辑AppKernel,而不是创建一个新包.

解决方法

我找不到扩展现有命令的方法,所以我的想法是在现有的bundle中创建一个新的控制台命令.

namespace YourOriginalBundleCommand;

use SymfonyBundleFrameworkBundleCommandContainerAwareCommand;
use SymfonyComponentConsoleInputArrayInput;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;

class AppendNewBundleCommand extends ContainerAwareCommand
{
    protected $appKernel;

    protected function configure()
    {
        $this
            ->setName('yourbundle:appendnewbundle')
            ->setDescription('Append a new bundle to the AppKernel.php')
            ->addArgument('namespace',InputArgument::REQUIRED,'Define your new bundle/namespace')
        ;
        $this->appKernel = __DIR__.'/../../../../app/AppKernel.php';
    }

    protected function execute(InputInterface $input,OutputInterface $output)
    {
        if (!file_exists($this->appKernel)) {
            throw new ErrorException(sprintf("Could not locate file %s",$this->appKernel));
        }
        if (!is_writable($this->appKernel)) {
            throw new ErrorException(sprintf('Cannot write into AppKernel (%s)',$this->appKernel));
        }

        $namespace = $input->getArgument('namespace');
        $appContent = file_get_contents($this->appKernel);

        $bundle = str_replace("/","",$namespace)."".str_replace("/","",$namespace);
        $newBundle = "new {$bundle}(),";

        $pattern = '/$bundless?=s?array((.*?));/is';
        preg_match($pattern,$appContent,$matches);

        $bList = rtrim($matches[1],"n ");
        $e = explode(",",$bList);
        $firstBundle = array_shift($e);
        $tabs = substr_count($firstBundle,'    ');

        $newBList = "$bundles = array("
                            .$bList."n"
                            .str_repeat('    ',$tabs).$newBundle."n"
                        .str_repeat('    ',$tabs-1).");";

        file_put_contents($this->appKernel,preg_replace($pattern,$newBList,$appContent));
    }
}

您现在可以通过执行生成捆绑包后立即执行它

php app/console yourbundle:appendnewbundle Your/SecondBundle

这会将此附加到现有捆绑包列表中

new YourSecondBundleYourSecondBundle(),

如果你有一个标准(symfony2)AppKernel,这是有效的.例如:

<?php

use SymfonyComponentHttpKernelKernel;
use SymfonyComponentConfigLoaderLoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new SymfonyBundleFrameworkBundleFrameworkBundle(),new SymfonyBundleSecurityBundleSecurityBundle(),new SymfonyBundleTwigBundleTwigBundle(),new SymfonyBundleMonologBundleMonologBundle(),new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(),new SymfonyBundleDoctrineBundleDoctrineBundle(),new SymfonyBundleAsseticBundleAsseticBundle(),new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),new JMSSecurityExtraBundleJMSSecurityExtraBundle(),new OrnicarApcBundleOrnicarApcBundle(),new YourOriginalBundleYourOriginalBundle(),);

        if (in_array($this->getEnvironment(),array('dev','test'))) {
            $bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle();
            $bundles[] = new SensioBundleDistributionBundleSensioDistributionBundle();
            $bundles[] = new SensioBundleGeneratorBundleSensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读