Dir.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Encapsulates directories structure of a Magento module
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Module;
  9. use Magento\Framework\Component\ComponentRegistrar;
  10. use Magento\Framework\Component\ComponentRegistrarInterface;
  11. class Dir
  12. {
  13. /**#@+
  14. * Directories within modules
  15. */
  16. const MODULE_ETC_DIR = 'etc';
  17. const MODULE_I18N_DIR = 'i18n';
  18. const MODULE_VIEW_DIR = 'view';
  19. const MODULE_CONTROLLER_DIR = 'Controller';
  20. const MODULE_SETUP_DIR = 'Setup';
  21. /**#@-*/
  22. /**#@-*/
  23. private $componentRegistrar;
  24. /**
  25. * @param ComponentRegistrarInterface $componentRegistrar
  26. */
  27. public function __construct(ComponentRegistrarInterface $componentRegistrar)
  28. {
  29. $this->componentRegistrar = $componentRegistrar;
  30. }
  31. /**
  32. * Retrieve full path to a directory of certain type within a module
  33. *
  34. * @param string $moduleName Fully-qualified module name
  35. * @param string $type Type of module's directory to retrieve
  36. * @return string
  37. * @throws \InvalidArgumentException
  38. */
  39. public function getDir($moduleName, $type = '')
  40. {
  41. $path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
  42. // An empty $type means it's getting the directory of the module itself.
  43. if (empty($type) && !isset($path)) {
  44. // Note: do not throw \LogicException, as it would break backwards-compatibility.
  45. throw new \InvalidArgumentException("Module '$moduleName' is not correctly registered.");
  46. }
  47. if ($type) {
  48. if (!in_array($type, [
  49. self::MODULE_ETC_DIR,
  50. self::MODULE_I18N_DIR,
  51. self::MODULE_VIEW_DIR,
  52. self::MODULE_CONTROLLER_DIR,
  53. self::MODULE_SETUP_DIR
  54. ])) {
  55. throw new \InvalidArgumentException("Directory type '{$type}' is not recognized.");
  56. }
  57. $path .= '/' . $type;
  58. }
  59. return $path;
  60. }
  61. }