FileResolver.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem;
  7. /**
  8. * Contains logic for finding class filepaths based on include_path configuration.
  9. */
  10. class FileResolver
  11. {
  12. /**
  13. * Find a file in include path. Include path is set in composer.json or with set_include_path()
  14. *
  15. * @param string $class
  16. * @return string|bool
  17. */
  18. public function getFile($class)
  19. {
  20. $relativePath = $this->getFilePath($class);
  21. return stream_resolve_include_path($relativePath);
  22. }
  23. /**
  24. * Get relative file path for specified class
  25. *
  26. * @param string $class
  27. * @return string
  28. */
  29. public function getFilePath($class)
  30. {
  31. return ltrim(str_replace(['_', '\\'], '/', $class), '/') . '.php';
  32. }
  33. /**
  34. * Add specified path(s) to the current include_path
  35. *
  36. * @param string|array $path
  37. * @param bool $prepend Whether to prepend paths or to append them
  38. * @return void
  39. */
  40. public static function addIncludePath($path, $prepend = true)
  41. {
  42. $includePathExtra = implode(PATH_SEPARATOR, (array)$path);
  43. $includePath = get_include_path();
  44. $pathSeparator = $includePath && $includePathExtra ? PATH_SEPARATOR : '';
  45. if ($prepend) {
  46. $includePath = $includePathExtra . $pathSeparator . $includePath;
  47. } else {
  48. $includePath = $includePath . $pathSeparator . $includePathExtra;
  49. }
  50. set_include_path($includePath);
  51. }
  52. }