gnugat/redaktilo

使用类似于编辑器的对象查找、插入、替换和删除行

2.0.1 2024-08-12 20:19 UTC

README

Redaktilo 允许您使用类似编辑器的对象查找、插入、替换和删除行。

因为您的代码也需要编辑器来操作文件.

入门

使用Composer在项目中安装 Redaktilo

$ composer require "gnugat/redaktilo:^2.0"

Redaktilo 提供了一个 Editor 类,可以使用 EditorFactory 实例化

<?php
require_once __DIR__.'/vendor/autoload.php';

use Gnugat\Redaktilo\EditorFactory;

$editor = EditorFactory::createEditor();

真实案例

在我们的示例中,我们将创建一个类似于KernelManipulatorSensioGeneratorBundle

它接受一个捆绑的全限定类名并在 AppKernel 文件中插入它

<?php

namespace Sensio\Bundle\GeneratorBundle\Manipulator;

use Gnugat\Redaktilo\Editor;

class KernelManipulator extends Manipulator
{
    protected $editor;
    protected $appKernelFilename;

    public function __construct(Editor $editor, $appKernelFilename)
    {
        $this->editor = $editor;
        $this->appKernelFilename = $appKernelFilename;
    }

    public function addBundle($bundle)
    {
        $appKernel = $this->editor->open($this->appKernelFilename);
        $newBundle = "            new $bundle(),";
        if ($this->editor->hasBelow($appKernel, $newBundle)) {
            $message = sprintf('Bundle "%s" is already defined in "AppKernel::registerBundles()".', $bundle);

            throw new \RuntimeException($message);
        }
        $this->editor->jumpBelow($appKernel, '        );');
        $this->editor->insertAbove($appKernel, $newBundle);
        $this->editor->save($appKernel);

        return true;
    }
}

如您所见,它比原始的 PHP 标记解析更容易阅读和理解。

更多文档

您可以使用以下方式查看当前和过去的版本:

您可以在以下链接中找到更多文档:

下一部分