Merge pull request #6733 from open-sausages/pulls/4.0/module-manifest-resource

API Add experimental getResource() to Module
This commit is contained in:
Ingo Schommer 2017-03-28 17:48:38 +13:00 committed by GitHub
commit 6f6a53c17c
3 changed files with 58 additions and 0 deletions

View File

@ -148,4 +148,35 @@ class Module implements Serializable
$this->composerData = $result; $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);
}
} }

View File

@ -25,6 +25,18 @@ class ModuleLoader
return self::$instance ? self::$instance : self::$instance = new self(); 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 * Returns the currently active class manifest instance that is used for
* loading classes. * loading classes.

View File

@ -69,4 +69,19 @@ class ModuleManifestTest extends SapphireTest
$this->assertEquals('moduleb', $module->getShortName()); $this->assertEquals('moduleb', $module->getShortName());
$this->assertEquals('moduleb', $module->getRelativePath()); $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')
);
}
} }