| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Finder; |
| 13 |
|
| 14 |
/** |
| 15 |
* Extends \SplFileInfo to support relative paths. |
| 16 |
* |
| 17 |
* @author Fabien Potencier <[email protected]> |
| 18 |
*/ |
| 19 |
class SplFileInfo extends \SplFileInfo |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @param string $file The file name |
| 23 |
* @param string $relativePath The relative path |
| 24 |
* @param string $relativePathname The relative path name |
| 25 |
*/ |
| 26 |
public function __construct( |
| 27 |
string $file, |
| 28 |
private string $relativePath, |
| 29 |
private string $relativePathname, |
| 30 |
) { |
| 31 |
parent::__construct($file); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the relative path. |
| 36 |
* |
| 37 |
* This path does not contain the file name. |
| 38 |
*/ |
| 39 |
public function getRelativePath(): string |
| 40 |
{ |
| 41 |
return $this->relativePath; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the relative path name. |
| 46 |
* |
| 47 |
* This path contains the file name. |
| 48 |
*/ |
| 49 |
public function getRelativePathname(): string |
| 50 |
{ |
| 51 |
return $this->relativePathname; |
| 52 |
} |
| 53 |
|
| 54 |
public function getFilenameWithoutExtension(): string |
| 55 |
{ |
| 56 |
$filename = $this->getFilename(); |
| 57 |
|
| 58 |
return pathinfo($filename, \PATHINFO_FILENAME); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the contents of the file. |
| 63 |
* |
| 64 |
* @throws \RuntimeException |
| 65 |
*/ |
| 66 |
public function getContents(): string |
| 67 |
{ |
| 68 |
set_error_handler(static function ($type, $msg) use (&$error) { $error = $msg; }); |
| 69 |
try { |
| 70 |
$content = file_get_contents($this->getPathname()); |
| 71 |
} finally { |
| 72 |
restore_error_handler(); |
| 73 |
} |
| 74 |
if (false === $content) { |
| 75 |
throw new \RuntimeException($error); |
| 76 |
} |
| 77 |
|
| 78 |
return $content; |
| 79 |
} |
| 80 |
} |