Keine Beschreibung

SplFileInfo.php 2.0KB

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