API Add experimental getResource() to Module

This commit is contained in:
Damian Mooyman 2017-03-24 09:58:54 +13:00
parent 416b3dd724
commit 874c6ccdd4
3 changed files with 57 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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.

View File

@ -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')
);
}
}