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

时间:2012-10-26 06:59:34

标签: php symfony console bundle composer-php

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

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

3 个答案:

答案 0 :(得分:4)

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

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

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

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');
    }
}

答案 1 :(得分:2)

我没有测试它

use Symfony\Component\HttpKernel\KernelInterface;
use Sensio\Bundle\GeneratorBundle\Manipulator\KernelManipulator;
use RuntimeException;

class SomeClass {
    /**
     * Register bundle in Kernel
     * @param  KernelInterface $kernel
     * @param  sting           $namespace
     * @param  sting           $bundle
     * @return boolean
     * @throws RuntimeException         When bundle already defined in <comment>AppKernel::registerBundles()</comment>
     */
    protected function registerBundle(KernelInterface $kernel, $namespace, $bundle)
    {
        $manip = new KernelManipulator($kernel);

        return $manip->addBundle($namespace.'\\'.$bundle);
    }
}

答案 2 :(得分:-2)

从技术上讲,当然有可能,有人必须这样做。