You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.1 KiB
60 lines
1.1 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Engine;
|
|
|
|
/**
|
|
* File helper for handling resolving filenames in a variety of include paths.
|
|
*
|
|
* @author Adam Pippin <hello@adampippin.ca>
|
|
*/
|
|
class File
|
|
{
|
|
/**
|
|
* List of include paths to search, in order.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $paths;
|
|
|
|
/**
|
|
* Create a new file helper.
|
|
*
|
|
* @param string[] $paths list of paths to search in order
|
|
*/
|
|
public function __construct(array $paths)
|
|
{
|
|
$this->paths = $paths;
|
|
}
|
|
|
|
/**
|
|
* Resolve a filename by searching all include paths and checking if the
|
|
* file exists there. Returns the first one it finds.
|
|
*
|
|
* @param string $file
|
|
* @return string
|
|
*/
|
|
public function resolve(string $file): string
|
|
{
|
|
foreach ($this->paths as $path)
|
|
{
|
|
if (file_exists($path.DIRECTORY_SEPARATOR.$file))
|
|
{
|
|
return realpath($path.DIRECTORY_SEPARATOR.$file);
|
|
}
|
|
}
|
|
throw new \Exception('File not found: '.$file);
|
|
}
|
|
|
|
/**
|
|
* Add a new search path to the end of the paths.
|
|
*
|
|
* @param string $path
|
|
* @return void
|
|
*/
|
|
public function appendPath(string $path): void
|
|
{
|
|
$this->paths[] = $path;
|
|
}
|
|
}
|
|
|