ReverseResolver.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Resolves file/directory paths to modules they belong to
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Module\Dir;
  9. use Magento\Framework\Module\Dir;
  10. use Magento\Framework\Module\ModuleListInterface;
  11. class ReverseResolver
  12. {
  13. /**
  14. * @var ModuleListInterface
  15. */
  16. protected $_moduleList;
  17. /**
  18. * @var Dir
  19. */
  20. protected $_moduleDirs;
  21. /**
  22. * @param ModuleListInterface $moduleList
  23. * @param Dir $moduleDirs
  24. */
  25. public function __construct(ModuleListInterface $moduleList, Dir $moduleDirs)
  26. {
  27. $this->_moduleList = $moduleList;
  28. $this->_moduleDirs = $moduleDirs;
  29. }
  30. /**
  31. * Retrieve fully-qualified module name, path belongs to
  32. *
  33. * @param string $path Full path to file or directory
  34. * @return string|null
  35. */
  36. public function getModuleName($path)
  37. {
  38. $path = str_replace('\\', '/', $path);
  39. foreach ($this->_moduleList->getNames() as $moduleName) {
  40. $moduleDir = $this->_moduleDirs->getDir($moduleName);
  41. $moduleDir = str_replace('\\', '/', $moduleDir);
  42. if ($path == $moduleDir || strpos($path, $moduleDir . '/') === 0) {
  43. return $moduleName;
  44. }
  45. }
  46. return null;
  47. }
  48. }