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.
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 instert 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:
- the
git tag
command - the releases page on Github
- the file listing the changes between versions
You can find more documentation at the following links:
- copyright and MIT license
- versioning and branching models
- contribution instructions
- migration to 2.0 instructions
Next readings: