diff --git a/src/Core/Manifest/Module.php b/src/Core/Manifest/Module.php index e270f4fc8..f5de81e32 100644 --- a/src/Core/Manifest/Module.php +++ b/src/Core/Manifest/Module.php @@ -4,6 +4,7 @@ namespace SilverStripe\Core\Manifest; use Exception; use Serializable; +use SilverStripe\Assets\File; class Module implements Serializable { @@ -148,4 +149,33 @@ class Module implements Serializable $this->composerData = $result; } } + + /** + * Gets path to physical file resource relative to base directory. + * Directories included + * + * This method makes no distinction between public / local resources, + * which may change in the near future. + * + * @internal Experimental API and may change + * @param string $path File or directory path relative to module directory + * @return string Path relative to base directory + */ + public function getResource($path) + { + return File::join_paths($this->getRelativePath(), $path); + } + + /** + * Check if this module has a given resource + * + * @internal Experimental API and may change + * @param string $path + * @return bool + */ + public function hasResource($path) + { + $resource = $this->getResource($path); + return file_exists($this->basePath . '/' . $resource); + } } diff --git a/src/Core/Manifest/ModuleLoader.php b/src/Core/Manifest/ModuleLoader.php index 64d3fc067..fbfebea9f 100644 --- a/src/Core/Manifest/ModuleLoader.php +++ b/src/Core/Manifest/ModuleLoader.php @@ -25,6 +25,18 @@ class ModuleLoader return self::$instance ? self::$instance : self::$instance = new self(); } + /** + * Get module by name from the current manifest. + * Alias for ::instance()->getManifest()->getModule() + * + * @param string $module + * @return Module + */ + public static function getModule($module) + { + return static::instance()->getManifest()->getModule($module); + } + /** * Returns the currently active class manifest instance that is used for * loading classes. diff --git a/tests/php/Core/Manifest/ModuleManifestTest.php b/tests/php/Core/Manifest/ModuleManifestTest.php index 001159509..cfef11688 100644 --- a/tests/php/Core/Manifest/ModuleManifestTest.php +++ b/tests/php/Core/Manifest/ModuleManifestTest.php @@ -69,4 +69,19 @@ class ModuleManifestTest extends SapphireTest $this->assertEquals('moduleb', $module->getShortName()); $this->assertEquals('moduleb', $module->getRelativePath()); } + + /* + * Note: Tests experimental API + * @internal + */ + public function testGetResource() + { + $module = $this->manifest->getModule('moduleb'); + $this->assertTrue($module->hasResource('composer.json')); + $this->assertFalse($module->hasResource('package.json')); + $this->assertEquals( + 'moduleb/composer.json', + $module->getResource('composer.json') + ); + } }