php - 有没有办法自动更新 symfony2 中的 AppKernel?

标签 php symfony console bundle composer-php

可能类似于 generate:bundle 命令(生成 bundle 后提示更新 AppKernel)或 Composer(使用安装的依赖项更新自动加载)。

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

最佳答案

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

namespace Your\OriginalBundle\Command;

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

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 = '/\$bundles\s?=\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

这会将其附加到现有 bundle 列表中

new Your\SecondBundle\YourSecondBundle(),

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

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
            new Ornicar\ApcBundle\OrnicarApcBundle(),
            new Your\OriginalBundle\YourOriginalBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

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

关于php - 有没有办法自动更新 symfony2 中的 AppKernel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13082254/

相关文章:

php - 如何获取 PHP 的操作码?

php - Doctrine 2,HINT_FORCE_PARTIAL_LOAD 不会获取连接的实体

php - Symfony Controller 中 Action 的含义

linux - 如何使用 brewperl 在 Ubuntu 上安装应用程序

powershell - 如何以编程方式将Powershell背景色设置为RGB值

php - PHP 值没有结果 Mysqli stmt

php - 使用下拉菜单、PHP 和 MySQL 将数据输入数据库

php - 商店的表格结构 - 商品 - 价格

symfony - 将Symfony 4项目部署到 digital ocean

javascript - Firebug 将 'delete' 运算符应用于不合格的名称已弃用