From 874c6ccdd4a4eb887c989e858a8bb3b34d02f384 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 24 Mar 2017 09:58:54 +1300 Subject: [PATCH 1/3] API Add experimental getResource() to Module --- src/Core/Manifest/Module.php | 30 +++++++++++++++++++ src/Core/Manifest/ModuleLoader.php | 12 ++++++++ .../php/Core/Manifest/ModuleManifestTest.php | 15 ++++++++++ 3 files changed, 57 insertions(+) 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') + ); + } } From 556c9a537eb483781b320fcba4672dcc35d0e60b Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 28 Mar 2017 17:22:30 +1300 Subject: [PATCH 2/3] Module->getResourcePath() Based on Sam's feedback in https://github.com/silverstripe/silverstripe-framework/pull/6733 --- src/Core/Manifest/Module.php | 4 ++-- tests/php/Core/Manifest/ModuleManifestTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Core/Manifest/Module.php b/src/Core/Manifest/Module.php index f5de81e32..02b22d4fb 100644 --- a/src/Core/Manifest/Module.php +++ b/src/Core/Manifest/Module.php @@ -161,7 +161,7 @@ class Module implements Serializable * @param string $path File or directory path relative to module directory * @return string Path relative to base directory */ - public function getResource($path) + public function getResourcePath($path) { return File::join_paths($this->getRelativePath(), $path); } @@ -175,7 +175,7 @@ class Module implements Serializable */ public function hasResource($path) { - $resource = $this->getResource($path); + $resource = $this->getResourcePath($path); return file_exists($this->basePath . '/' . $resource); } } diff --git a/tests/php/Core/Manifest/ModuleManifestTest.php b/tests/php/Core/Manifest/ModuleManifestTest.php index cfef11688..5e8d98d37 100644 --- a/tests/php/Core/Manifest/ModuleManifestTest.php +++ b/tests/php/Core/Manifest/ModuleManifestTest.php @@ -81,7 +81,7 @@ class ModuleManifestTest extends SapphireTest $this->assertFalse($module->hasResource('package.json')); $this->assertEquals( 'moduleb/composer.json', - $module->getResource('composer.json') + $module->getResourcePath('composer.json') ); } } From e08948480cd5b894528fbcd435052342968413f7 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 28 Mar 2017 17:26:23 +1300 Subject: [PATCH 3/3] Remove File dependency from Module class --- src/Core/Manifest/Module.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Core/Manifest/Module.php b/src/Core/Manifest/Module.php index 02b22d4fb..0d0726830 100644 --- a/src/Core/Manifest/Module.php +++ b/src/Core/Manifest/Module.php @@ -4,7 +4,6 @@ namespace SilverStripe\Core\Manifest; use Exception; use Serializable; -use SilverStripe\Assets\File; class Module implements Serializable { @@ -163,7 +162,9 @@ class Module implements Serializable */ public function getResourcePath($path) { - return File::join_paths($this->getRelativePath(), $path); + $base = rtrim($this->getRelativePath(), '/\\'); + $path = rtrim($path, '/\\'); + return "{$base}/{$path}"; } /**