In Magento 1, the static getModuleDir
method of the Mage
class
Mage::getModuleDir('', 'Mage_Core')
provided a stable, "works across versions" way of getting the base folder of a Magento module.
Does Magento 2 have something similar? I found the Magento\Framework\Module\Dir\Reader
class that has a getModuleDir
method.
#File: vendor/magento/framework/Module/Dir/Reader.php
/**
* Get module directory by directory type
*
* @param string $type
* @param string $moduleName
* @return string
*/
public function getModuleDir($type, $moduleName)
{
if (isset($this->customModuleDirs[$moduleName][$type])) {
return $this->customModuleDirs[$moduleName][$type];
}
return $this->moduleDirs->getDir($moduleName, $type);
}
I could inject this class as a dependency in my own class and call this method. However, this method is not marked with any sort of @api
flag, which means it might change unexpectedly.
Is there a safe/stable way for extension developers to determine which folder their module is in (in order to load extra configuration or data files that ship with the module)? Or do we need to roll the dice the the above class, and/or do directory traversing tricks with __FILE__
and __DIR__
?