有没有一种方法可以自动更新Symfony2中的AppKernel



也许类似于生成:捆绑命令(生成捆绑包提示更新Appkernel之后)或作曲家(通过安装的依赖关系更新您的自动加载)。

我想获得与生成的类似功能:捆绑包,但是我不想添加一个捆绑包,而不必手动编辑appkernel。

我找不到扩展现有命令的方法,所以我的想法是将新的控制台命令创建到现有捆绑包中。

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

我没有测试

use SymfonyComponentHttpKernelKernelInterface;
use SensioBundleGeneratorBundleManipulatorKernelManipulator;
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);
    }
}

从技术上讲,当然是可能的,有人只需要这样做。

相关内容

  • 没有找到相关文章

最新更新