Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Dir.php
Go to the documentation of this file.
1 <?php
9 
12 
13 class Dir
14 {
18  const MODULE_ETC_DIR = 'etc';
19  const MODULE_I18N_DIR = 'i18n';
20  const MODULE_VIEW_DIR = 'view';
21  const MODULE_CONTROLLER_DIR = 'Controller';
22  const MODULE_SETUP_DIR = 'Setup';
26  private $componentRegistrar;
27 
31  public function __construct(ComponentRegistrarInterface $componentRegistrar)
32  {
33  $this->componentRegistrar = $componentRegistrar;
34  }
35 
44  public function getDir($moduleName, $type = '')
45  {
46  $path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
47 
48  // An empty $type means it's getting the directory of the module itself.
49  if (empty($type) && !isset($path)) {
50  // Note: do not throw \LogicException, as it would break backwards-compatibility.
51  throw new \InvalidArgumentException("Module '$moduleName' is not correctly registered.");
52  }
53 
54  if ($type) {
55  if (!in_array($type, [
56  self::MODULE_ETC_DIR,
57  self::MODULE_I18N_DIR,
58  self::MODULE_VIEW_DIR,
59  self::MODULE_CONTROLLER_DIR,
60  self::MODULE_SETUP_DIR
61  ])) {
62  throw new \InvalidArgumentException("Directory type '{$type}' is not recognized.");
63  }
64  $path .= '/' . $type;
65  }
66 
67  return $path;
68  }
69 }
$componentRegistrar
Definition: bootstrap.php:23
getDir($moduleName, $type='')
Definition: Dir.php:44
$type
Definition: item.phtml:13
__construct(ComponentRegistrarInterface $componentRegistrar)
Definition: Dir.php:31