Available Methods
className string
Section titled “className string”Get the assumed PSR-4 class name for the file. Same as name(): only the last extension is removed, so Rupert.test.php gives Rupert.test.
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->className();// RandomClassForTestingWithappendContent Utility
Section titled “appendContent Utility”If the path is a file and if it exists, appends content to the end of the file
$file = new Utility('./logfile.txt');
echo $file->appendContent('hello world');// ... hello worldcontent string
Section titled “content string”If the path is a file and if it exists, gets the content of the file
$file = new Utility('./logfile.txt');
echo $file->content();// ...delete Utility
Section titled “delete Utility”Delete the file or directory with the given path
$file = new Utility('./src/RandomClassForTestingWith.php');
$file->delete();directory Utility
Section titled “directory Utility”Get the directory of the path. A path that exists as a directory is always returned as is, even if its name has a dot (v1.2, .git); for paths that do not exist, an extension means a file.
$file = new Utility('./src/RandomClassForTestingWith.php');
$file->directory(); // ./src
$file = new Utility('./src/App/Commands');
$file->directory(); // ./src/App/Commandsexists bool
Section titled “exists bool”Does the file or directory actually exists
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->exists(); // true$file->delete();echo $file->exists(); // falseextension string
Section titled “extension string”Guess the path’s file extension
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->extension(); // php
// get with dot prefixecho $file->extension(true); // .phpfiles array
Section titled “files array”Get a collection of spl file objects, from within the path if it’s a directory
$file = new Utility('./src/');
echo $file->files(); // [...$srcFiles]fullyQualifiedClassname string
Section titled “fullyQualifiedClassname string”Get the fully qualified class name from the file (if it’s a PHP file)
$file = new Utility('./src/');
echo $file->fullyQualifiedClassname(); // App/RandomClassForTestingWithhasAbsolutePath bool
Section titled “hasAbsolutePath bool”Does the current path have an absolute file path
$file = new Utility('/src');
echo $file->hasAbsolutePath(); // true
$file = new Utility('~/src');
echo $file->hasAbsolutePath(); // falseisDirectory bool
Section titled “isDirectory bool”Checks if the path is a directory
$file = new Utility('./src/');
echo $file->isDirectory(); // true
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->isDirectory(); // falseisFile bool
Section titled “isFile bool”Checks if the path is a file
$file = new Utility('./src/');
echo $file->isFile(); // false
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->isFile(); // truemakeDirectory Utility
Section titled “makeDirectory Utility”Create directory if it doesn’t already exist
$file = new Utility('~/Code/app');
echo $file->makeDirectory();namespace string
Section titled “namespace string”Get the namespace of the file (if it’s a PHP file). The file is tokenised, so braced namespaces, tabs, CRLF line endings and commented-out declarations are handled. Throws FileFormatException when no namespace is declared. The old misspelt FileFormatExpection is deprecated but remains its parent class, so existing catch blocks keep working.
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->namespace(); // Applines Generator
Section titled “lines Generator”Stream the file one line at a time without loading it all into memory. Line endings are stripped. Throws FileNotFoundException straight away if the file does not exist.
$file = new Utility('./logfile.txt');
foreach ($file->lines() as $line) { echo $line;}name string
Section titled “name string”Get the file or directory name, optionally with its extension
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->name(); // RandomClassForTestingWith
// get with file extensionecho $file->name(true); // RandomClassForTestingWith.phppath string
Section titled “path string”Get the path of the file
$file = new Utility('./src/RandomClassForTestingWith.php');
echo $file->path(); // ./src/RandomClassForTestingWith.phpsetContent Utility
Section titled “setContent Utility”If the path is a file and if it exists, sets the content of the file.
The write is atomic: content goes to a temporary file in the same directory, which is then renamed over the original, so readers never see a partial file. Permissions are preserved and symlinks are written through. appendContent writes in place under an exclusive lock.
$file = new Utility('./logfile.txt');
echo $file->setContent('hello world');// hello worldtouchFile Utility
Section titled “touchFile Utility”Touch a file to, ensure it exists
$file = new Utility('~/Code/app.php');
echo $file->touchFile();touch Utility
Section titled “touch Utility”Touch a file or directory, to ensure it exists.
If the path doesn’t exist and has no file extension, a directory is assumed to be wanted. A path that already exists is left untouched.
$file = new Utility('~/Code/app.php');
echo $file->touch(); // file created
$file = new Utility('~/Code/app');
echo $file->touch(); // directory createdSecurity
Section titled “Security”- Pass a
$rootas the second constructor argument (new Utility($path, $root)) to confine reads (content,namespace,lines,files) and mutating operations (delete,setContent,appendContent,touch,touchFile,makeDirectory). Paths that resolve outside$rootare refused. Without a$root, no confinement applies, so never build paths from untrusted input without one. - Stream wrapper paths such as
phar://,php://,data:andhttp://are rejected with anInvalidArgumentException. Only plain paths andfile:are accepted.