SplFileInfo.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /**
  19. * @var string
  20. */
  21. private $relativePath;
  22. /**
  23. * @var string
  24. */
  25. private $relativePathname;
  26. /**
  27. * @param string $file The file name
  28. * @param string $relativePath The relative path
  29. * @param string $relativePathname The relative path name
  30. */
  31. public function __construct($file, $relativePath, $relativePathname)
  32. {
  33. parent::__construct($file);
  34. $this->relativePath = $relativePath;
  35. $this->relativePathname = $relativePathname;
  36. }
  37. /**
  38. * Returns the relative path.
  39. *
  40. * This path does not contain the file name.
  41. */
  42. public function getRelativePath()
  43. {
  44. return $this->relativePath;
  45. }
  46. /**
  47. * Returns the relative path name.
  48. *
  49. * This path contains the file name.
  50. */
  51. public function getRelativePathname()
  52. {
  53. return $this->relativePathname;
  54. }
  55. public function getFilenameWithoutExtension()
  56. {
  57. $filename = $this->getFilename();
  58. return pathinfo($filename, \PATHINFO_FILENAME);
  59. }
  60. /**
  61. * Returns the contents of the file.
  62. *
  63. * @throws \RuntimeException
  64. */
  65. public function getContents()
  66. {
  67. set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
  68. try {
  69. $content = file_get_contents($this->getPathname());
  70. } finally {
  71. restore_error_handler();
  72. }
  73. if (false === $content) {
  74. throw new \RuntimeException($error);
  75. }
  76. return $content;
  77. }
  78. }