0

我有以下 PHP 文件:

<?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\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\AopBundle\JMSAopBundle(),
            new JMS\DiExtraBundle\JMSDiExtraBundle($this),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
            $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');
    }
}

考虑到最后一行可能有也可能没有逗号,我想在最后的 $bundles 变量中添加一行。

我需要添加的行如下:

new FOS\\UserBundle\\FOSUserBundle()

我编写了我的版本,但如果 $bundles 数组的最后一个元素中有逗号,它就不起作用。

需要稍作修改,但我不知道该怎么做。

sed '/bundles.=.array/,/);/ { /[^(,;]$/ s//&,\n            new FOS\\UserBundle\\FOSUserBundle()/ }' app/AppKernel.php;

最终文件应如下所示:

<?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\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new JMS\AopBundle\JMSAopBundle(),
            new JMS\DiExtraBundle\JMSDiExtraBundle($this),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
            new FOS\UserBundle\FOSUserBundle(), //with comma or without

        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
            $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');
    }
}
4

1 回答 1

1

不用说,如果您的文件格式偏差太大,这将失败,但对于您的情况,以下适用于 GNU sed:

*add_line.sed*

/bundles.=.array/ {     # When the right array is found
  : a
  N                     # Keep two lines in pattern space
  /);/bb                # Jump to b when end of array reached
  P                     # Print first line of pattern space
  s/[^\n]*\n//          # and delete it
  ba                    # Jump to a and repeat
  : b
  /,\n/! s#\n#,\n#      # Ensure comma ends the line before end of array
  s#\([ \t]*\)\([^\n]*\)\n#\1\2\n\1new FOS\\UserBundle\\FOSUserBundle()\n#
}

像这样运行它:

sed -f add_line.sed app/AppKernel.php

或作为单行:

sed '/bundles.=.array/ { :a; N; /);/bb; P; s/[^\n]*\n//; ba; :b; /,\n/! s#\n#,\n#; s#\([ \t]*\)\([^\n]*\)\n#\1\2\n\1new FOS\\UserBundle\\FOSUserBundle()\n#; }' app/AppKernel.php

输出摘录:

...
    $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\AsseticBundle\AsseticBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new JMS\AopBundle\JMSAopBundle(),
        new JMS\DiExtraBundle\JMSDiExtraBundle($this),
        new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
        new FOS\UserBundle\FOSUserBundle()
    );

...
于 2012-11-16T08:19:46.320 回答