SplFileInfo.php 1.9 KB

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