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

2024-01-15

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

我想要获得与generate:bundle类似的功能,但我不想创建新的bundle,而是想添加刚刚下载的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

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

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');
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法自动更新symfony2中的AppKernel? 的相关文章

  • Web 服务器和 servlet 容器之间的区别

    Web 服务器 和 Servlet 容器 有什么区别 一般来说 所有 Web 服务器也是 Servlet Web 容器 反之亦然吗 我一直在使用 Apache Tomcat for JSP 和 Apache for PHP 但无法弄清楚这两
  • 如何在 PHP 中实现前向索引?

    我希望在 PHP 中实现一个简单的前向索引器 是的 我确实知道 PHP 并不是完成这项任务的最佳工具 但无论如何我还是想这样做 其背后的理由很简单 我想要一个 并且是 PHP 版本 让我们做一些基本假设 整个互联网包括 大约五千个 HTML
  • PHP如何计算时差? [复制]

    这个问题在这里已经有答案了 我必须计算日期时间差 如何在 PHP 中做到这一点 我需要准确的小时 分钟和秒 有人有这方面的脚本吗 Use the diff 方法 http www php net manual en datetime dif
  • 将结果从 pdo 发送到 ajax 时遇到问题

    我想做的是 如果用户成功注册 pdo 将提供信息并将其发送到 ajax 如果用户注册与否 ajax 将发送消息 在我将这个条件放入我的 pdo 中后 它工作正常 现在它不会再插入 并且 ajax 告诉 注册用户时出错 每时每刻 script
  • PHP 数组转换为 Javascript 数组

    下午都 下面的代码工作完美 但是 我需要将 php sql 数组的每一行拉出并放入脚本 var 中 关于如何编写可以做到这一点的 while 循环有什么想法吗 谢谢你的帮助 var enableDays enableDays push 附加
  • Laravel 强制 SSL 给出“此网页有重定向循环”

    我正在使用 Laravel 4 1 并且想要在整个站点范围内强制使用 SSL 我的应用程序部署在 Heroku 上 将其添加到任一App before或作为过滤器 if Request secure return Redirect secu
  • 我怎样才能完成笛卡尔积函数的 Objective-C 实现?

    作为我的问题的后续here https stackoverflow com questions 8176719 algorithm generating all combinations from items that must be ch
  • 使用 Poedit 创建 POT 文件

    我正在拼命地尝试为我的 php 新应用程序创建一个目录 in 1 我创建了一个文件 trans php 其中放置了所有要翻译的值 例如 这是我的文件 2 我打开Poedit 在 路径 中这是我输入的内容 见图 然后我保存文件 php 的相同
  • 如何在 phpstorm 中自动生成类的属性?

    如果我实现一个类 它注入一些服务 我必须编写大量代码
  • 导出具有高质量图像的画布的最佳实践是什么?

    我需要你的帮助 我解释一下我的情况 我正在使用 Fabric js 库在我的应用程序中放置形状 文本等 我的画布尺寸为 1000x1000 像素 约 26 45x26 45 厘米 我有一个图像上传脚本 仅用于上传高质量图像 例如 300 d
  • 比较两个关联数组的键顺序

    假设我们有 2 个关联数组
  • 会话劫持和 PHP

    让我们只考虑服务器对用户的信任 会话固定 为了避免我使用的固定session regenerate id 仅在身份验证中 login php 会话侧劫持 整个站点的 SSL 加密 我安全吗 阅读 OWASPA3 破坏的身份验证和会话管理 h
  • 如何在 PHP 的 foreach 循环中获取两个项目? [复制]

    这个问题在这里已经有答案了 我有一个推荐轮播 轮播每次循环浏览两个项目 现在我想每次得到两个项目foreach环形 我怎么才能得到它 Code div div class row div class col md 6 div class s
  • PHP 中的 __DIR__ 和 dirname(__FILE__) 有什么区别吗?

    对我来说看起来是一样的 但我不确定 因为有很多项目使用dirname FILE 他们的结果是完全一样的 所以 这没有什么区别 例如 以下两行 var dump dirname FILE var dump DIR 两者都会给出相同的输出 st
  • SQL 未插入到 Yii 中具有关系的表中

    我正在尝试创建一个用户 但所有值都没有插入到数据库中 Systems user 表与partys 表有关系 因为party id 是sytems user 的主键 没有插入任何内容 甚至没有错误 它只是返回到 创建 页面 这是我的架构 Ta
  • 在 Bluemix 中激活 PHP 扩展

    这纯粹是 Bluemix 问题 我的代码在本地主机上顺利运行 但是当我将其迁移到 Bluemix 时 我的数据库连接失败了 检查日志 我发现问题 调用未定义的函数 mysqli init HTTP 响应 500 我发现扩展已被禁用以使其更小
  • Laravel Eloquent with()-> 返回 null

    我正在尝试使用 Eloquent 来获取具有以下功能的特定产品 brand id映射到a的列brands表 该brand数组返回空 这里有什么明显需要改变的地方吗 product Product with images gt with br
  • 性能方面插值(直接插入字符串)VS串联[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 使用 htaccess 文件重定向[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 Edit 我想重定向一个网址 to www example com location sydney from www example com rss
  • File_get_contents($url): 无法打开流

    我有一个脚本 我使用以下方法读取文件 file get contents urlencode url 我收到此错误 failed to open stream HTTP request failed HTTP 1 0 400 Bad req

随机推荐