From 57de7ae66df90f5f6903136f44c7fb87b2a54a55 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Wed, 10 Apr 2019 16:57:35 +0100 Subject: [PATCH] Component manager should throw an exception if dispatch is not available --- src/ResourceManager.php | 34 ++++++++++++++++++++-------------- src/ResourceStore.php | 4 ++-- tests/ResourceManagerTest.php | 3 +++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/ResourceManager.php b/src/ResourceManager.php index 8abd654..c7c2e48 100644 --- a/src/ResourceManager.php +++ b/src/ResourceManager.php @@ -99,23 +99,29 @@ public static function componentClass(string $componentClassName, $options = []) protected static function _componentManager($fullClass, Dispatch $dispatch = null, $options = []): ResourceManager { $class = ltrim($fullClass, '\\'); - if($dispatch) + if(!$dispatch) { - $maxPrefix = $maxAlias = ''; - $prefixLen = 0; - foreach($dispatch->getComponentAliases() as $alias => $namespace) + $dispatch = Dispatch::instance(); + if($dispatch === null) { - $trimNs = ltrim($namespace, '\\'); - $len = strlen($trimNs); - if(Strings::startsWith($class, $trimNs) && $len > $prefixLen) - { - $maxPrefix = $trimNs; - $prefixLen = $len; - $maxAlias = $alias; - } + throw new RuntimeException("Dispatch must be available to use the component manager"); } - $class = str_replace($maxPrefix, $maxAlias, $class); } + + $maxPrefix = $maxAlias = ''; + $prefixLen = 0; + foreach($dispatch->getComponentAliases() as $alias => $namespace) + { + $trimNs = ltrim($namespace, '\\'); + $len = strlen($trimNs); + if(Strings::startsWith($class, $trimNs) && $len > $prefixLen) + { + $maxPrefix = $trimNs; + $prefixLen = $len; + $maxAlias = $alias; + } + } + $class = str_replace($maxPrefix, $maxAlias, $class); $parts = explode('\\', $class); array_unshift($parts, count($parts)); @@ -153,7 +159,7 @@ public function getResourceUri($relativeFullPath): ?string public function getRelativeHash($filePath) { return Dispatch::instance()->generateHash(Dispatch::instance()->calculateRelativePath($filePath), 4); -} + } /** * @param $relativePath diff --git a/src/ResourceStore.php b/src/ResourceStore.php index 82a5eff..67f54c9 100644 --- a/src/ResourceStore.php +++ b/src/ResourceStore.php @@ -128,9 +128,9 @@ protected function _addToStore($type, $uri, $options = null, int $priority = sel * Clear the entire resource store with a type of null, or all items stored * by a type if supplied * - * @param null $type + * @param string $type Store Type e.g. ResourceStore::TYPE_CSS */ - public function clearStore($type = null) + public function clearStore(string $type = null) { if($type === null) { diff --git a/tests/ResourceManagerTest.php b/tests/ResourceManagerTest.php index 7d2ed54..5a534cf 100644 --- a/tests/ResourceManagerTest.php +++ b/tests/ResourceManagerTest.php @@ -61,6 +61,9 @@ public function testComponent() 'c/3/_/DemoComponent/DemoComponent/1a9ffb748d31/style.css', $manager->getResourceUri('style.css') ); + Dispatch::destroy(); + $this->expectException(RuntimeException::class); + ResourceManager::component($component); } public function testRequireJs()