Is there a way to update automatically the AppKernel in symfony2?

你离开我真会死。 提交于 2020-01-01 03:45:18

问题


Maybe something similar to the generate:bundle command (which after generating the bundle prompts to update the AppKernel) or to Composer (which updates your autoload with the dependencies you install).

I want to get a similar functionality to the generate:bundle, but instead of creating a new bundle I want to add a bundle I just downloaded without having to edit the AppKernel manually.


回答1:


I couldn't find a way to EXTEND an existing command, so my thought goes to create a new console command into an existing bundle.

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

You can now execute it, right after generating your bundle by doing

php app/console yourbundle:appendnewbundle Your/SecondBundle

This will append this to the list of existing bundles

new Your\SecondBundle\YourSecondBundle(),

This works if you have a standard (symfony2) AppKernel. Ex:

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



回答2:


i haven't test it

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



回答3:


Technically it is of course possible, someone just has to do it.



来源:https://stackoverflow.com/questions/13082254/is-there-a-way-to-update-automatically-the-appkernel-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!