tomphp / patch-builder
PHP Library for manipulating files to produce a patch.
Installs: 583
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tomphp/patch-builder
Requires
- php: >=5.3
 - phpspec/php-diff: 1.0.2
 
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: dev-master
 - phpmd/phpmd: 1.4.0
 - phpspec/phpspec: 2.0.*@dev
 - satooshi/php-coveralls: dev-master
 - squizlabs/php_codesniffer: 1.*
 
This package is not auto-updated.
Last update: 2025-10-25 21:07:46 UTC
README
A PHP Library which allows your to load some text, make some modifications to it and then generate a patch.
Installation
Installation is quite and easy with composer:
composer require tomphp/patch-builder:dev-master
Usage
Simply create a PatchBuffer containing the content you want to modify then
apply the modifications with the methods available:
use TomPHP\PatchBuilder\PatchBuffer; use TomPHP\PatchBuilder\Types\LineNumber; use TomPHP\PatchBuilder\Types\LineRange; $buffer = PatchBuffer::createWithContents(file('somedata.txt')); // Insert 2 at line 27 $buffer->insert(new LineNumber(27), array('hello', 'world')); // Delete lines 12 to 16 $buffer->delete(LineRange::createFromNumbers(12, 16)); // Replace line 4 and 5 with a new line $buffer->replace(LineRange::createFromNumbers(4, 5), array('hello moon'));
###Line Numbers
Line numbers and ranges are specified as TomPHP\PatchBuilder\Types\LineNumber
and TomPHP\PatchBuilder\Types\LineRange objects and refer to the line in the
buffer in it's modified state.
If you want to refer to a line but its number in the original content you can use
a TomPHP\PatchBuilder\Types\OriginalLineNumber object instead and it will
be translated to the current line number in the modified file.
use TomPHP\PatchBuilder\PatchBuffer; use TomPHP\PatchBuilder\Types\LineNumber; use TomPHP\PatchBuilder\Types\OrignalLineNumber; $buffer = PatchBuffer::createWithContents(file('somedata.txt')); // Insert 2 at line 5 $buffer->insert(new LineNumber(5), array('hello', 'world')); // Actually inserts at line 8 because 2 lines have been added before here. $buffer->insert(new OriginalLineNumber(6), array('hello', 'moon'));
If you try to access a line by original line number which has since been deleted
a TomPHP\PatchBuilder\LineTracker\Exception\DeletedLineException will be thrown.
###Outputting the patch
The buffer can be converted to a patch using a PatchBuilder class. Currently
only 1 PatchBuilder class is provided:
use TomPHP\PatchBuilder\PatchBuffer; use TomPHP\PatchBuilder\Builder\PhpDiffBuilder; $filename = 'myfile.txt'; $src = 'orignal/' . $filename; $dest = 'new/' . $filename; $buffer = PatchBuffer::createWithContents(file($src)); // ... perform buffer manipulations ... $builder = new PhpDiffBuilder(); echo $builder->buildPatch($src, $dest, $buffer);