Skip to content

gnugat/redaktilo

Repository files navigation

Redaktilo

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:

<?php
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:

<?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;
    }
}

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:

About

[maintained] An easy "line manipulation" PHP lib: jump, insert and do anything!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages