SourceMonkey
SourceMonkey is a package for working with source code, or text files on your local filesystem. SourceMonkey is useful for automated code generation and modification within your application., (*1)
Installation
To install with composer use the following command:, (*2)
composer install webspanner/sourcemonkey
Usage
Instantiating the class
Pass the fully qualified path to the file which you are editing into the constructor., (*3)
$sourceMonkey = new SourceMonkey('/tmp/my-file.php');
Methods
insertLineAfter()
To insert a string into a file after a given line number, moving all later text further down the page use the insertLineAfter() method, (*4)
$sourceMonkey->insertLineAfter('some string', 42);
write()
To write a string to a file, overwriting it's existing contents use the write() method., (*5)
$sourceMonkey->write('some string');
getLines()
To read the contents of a file into a 1-indexed array of strings, without line breaks use the getLines() method., (*6)
$sourceMonkey->getLines();
getPath()
Get the path of the SourceMonkey instance, (*7)
$sourceMonkey->getPath();
firstLineWithString()
Returns the line number of the first line which contains the given string, (*8)
$sourceMonkey->firstLineWithString($string);
{
deleteLine()
Delete the line at the given number, (*9)
$sourceMonkey->deleteLine($lineNumber);
replaceLine()
Replace the contents of the given line number with the given string, (*10)
$sourceMonkey->replaceLine($lineNumber, $string);
getProperty()
Get the PHP class property matching the given name from the class in the given source file and return it as a Property model object or null if it does not exist, (*11)
$sourceMonkey->getProperty($propertyName);
getClass()
Get the PHP class in the given file name, (*12)
$sourceMonkey->getClass()