Piping
Installation through composer
composer require fedorovmixail/piping
Creates a stream of data from a file or url or command output. Then, it applies the callback function line by line and places the processed data in a file, array, or string.
Syntax examples:
- Add line numbering in
ls
output
$count = 0;
Piper::command('ls')->setHandler(function ($string) use (&$count) {
$count++;
return "$count. $string";
})->toFile('tic');
- Example. Double processing, making a file into one line and delete spacing. Return string.
$string = Piper::file('lorem_ipsum')->setHandler(function($string) {
return preg_replace('/\s/', ' ', $string);
})->setHandler(function($string) {
return preg_replace('/\n/', ' ', $string);
})->toString();
- Example. Replaces 'some' on 'any'. Creates a stream from a url. Returns an array of strings
$array = Piping::url('https://php.net')->setHandler(function($string) {
return str_replace('some', 'any', $string);
})->toArray();
Available method:
Piper::command('ls') //creates a stream from command output
Piper::file('lorem_ipsum') //creates a stream from file
Piper::url('https://php.net') //creates a stream from URL
setHandler(function($string){return $string . 'some'}) //sets handler for every string
toArray() //returns the result to array
toString() //returns the result to string
toFile('file') //saves the result to file