Find, insert, replace and remove lines with an Editor-like object

Installs: 1 393

Dependents: 4

Stars: 36

Watchers: 8

Forks: 4

Open Issues: 5

Language: PHP


Redaktilo allows you to find, insert, replace and remove lines using an editor-like object.

Because your code too needs an editor to manipulate files.

SensioLabsInsight Travis CI

Getting started

Use Composer to install Redaktilo in your projects:

composer require "gnugat/redaktilo:~1.0"

Redaktilo provides an Editor class which can be instanciated using EditorFactory:

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

use Gnugat\Redaktilo\EditorFactory;

$editor = EditorFactory::createEditor();

Real life example

For our example, we will create a KernelManipulator similar to the one we can find in SensioGeneratorBundle.

It takes a bundle's fully qualified classname and inserts it in the AppKernel file:


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

        return true;

As you can see it's easier to read and to understand than the original PHP token parsing.

Further documentation

You can see the current and past versions using one of the following:

You can find more documentation at the following links:

Next readings: