mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #6733 from open-sausages/pulls/4.0/module-manifest-resource
API Add experimental getResource() to Module
This commit is contained in:
commit
6f6a53c17c
@ -148,4 +148,35 @@ 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 getResourcePath($path)
|
||||
{
|
||||
$base = rtrim($this->getRelativePath(), '/\\');
|
||||
$path = rtrim($path, '/\\');
|
||||
return "{$base}/{$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->getResourcePath($path);
|
||||
return file_exists($this->basePath . '/' . $resource);
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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->getResourcePath('composer.json')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user