DirectoryResolver.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\App\Filesystem;
  8. use Magento\Framework\Filesystem;
  9. /**
  10. * Magento directories resolver.
  11. */
  12. class DirectoryResolver
  13. {
  14. /**
  15. * @var DirectoryList
  16. */
  17. private $directoryList;
  18. /**
  19. * @var \Magento\Framework\Filesystem
  20. */
  21. private $filesystem;
  22. /**
  23. * @param DirectoryList $directoryList
  24. * @param Filesystem $filesystem
  25. */
  26. public function __construct(DirectoryList $directoryList, Filesystem $filesystem)
  27. {
  28. $this->directoryList = $directoryList;
  29. $this->filesystem = $filesystem;
  30. }
  31. /**
  32. * Validate path.
  33. *
  34. * Gets real path for directory provided in parameters and compares it with specified root directory.
  35. * Will return TRUE if real path of provided value contains root directory path and FALSE if not.
  36. * Throws the \Magento\Framework\Exception\FileSystemException in case when directory path is absent
  37. * in Directories configuration.
  38. *
  39. * @param string $path
  40. * @param string $directoryConfig
  41. * @return bool
  42. * @throws \Magento\Framework\Exception\FileSystemException
  43. */
  44. public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
  45. {
  46. $directory = $this->filesystem->getDirectoryWrite($directoryConfig);
  47. $realPath = $directory->getDriver()->getRealPathSafety($path);
  48. $root = $this->directoryList->getPath($directoryConfig);
  49. return strpos($realPath, $root) === 0;
  50. }
  51. }