Interface for editing .gitignore files in PHP.
Interface for editing .gitignore files in PHP
In general, you will open a .gitignore file, use the
after methods to append, or insert some
values, and then call the
save method to write the result.
Open a file and add a value:
use GitIgnoreWriter\GitIgnoreWriter; $writer = new GitIgnoreWriter('../.gitignore'); $writer->add('.env'); $writer->save();
Also supports fluent interface:
(new GitIgnoreWriter('../.gitignore')) ->add('setup.php') ->before('setup.php', 'install.php') ->save();
There are multiple ways to read from a source file, make some changes, and write the result to a different output file. The end result is the same, so choose whichever method best fits your use case:
$writer = (new GitIgnoreWriter('.gitignore.example')) ->setOutputPath('.gitignore'); // ... $writer->save();
$writer = (new GitIgnoreWriter('.gitignore.example')); // ... $writer->save('.gitignore');
Or using the
$writer = (new GitIgnoreWriter('.gitignore')) ->load('.gitignore.example'); // ... $writer->save();
add method appends lines starting at the current position in the file, which defaults to the end of the file.
after methods first seek the current position to a given line, and then start inserting lines relative to that position
$writer->before($find, $prependValue); $writer->after($find, $appendValue);
after methods each will accept the values to be inserted in multiple formats:
- a single-line string
- a multi-line string
- an array of strings
$writer->add(' # You can write comments First/Line/Path # blank lines are retained as well Second/Line/Path ')->save(); $writer->add([ 'line1', 'line2', 'line3', ])->save();
eof methods allow you to manually seek the internal pointer
to a specific line in the file. Any subsequent
add will insert content relative
to that position.
rewind: set the pointer to the beginning of the file
seek: set the pointer to a specific line number starting at 0
eof: set the pointer to append to the end of the file
deleteOffset methods provide ways to remove lines from the file. Note that the remaining lines are renumbered after deletion.
delete($value): remove a matching line from the file
deleteOffset($offset, $count = 1): delete one or more lines, starting at a specific 0-based line number.
$writer ->delete('working/') ->save(); $writer->deleteOffset(5, 2)->save();
exists method returns a boolean to test if a line exists in the file.
$result = $writer->exists('.env');