Skip to content

Commit 57de7ae

Browse files
committed
Component manager should throw an exception if dispatch is not available
1 parent db19628 commit 57de7ae

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/ResourceManager.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,29 @@ public static function componentClass(string $componentClassName, $options = [])
9999
protected static function _componentManager($fullClass, Dispatch $dispatch = null, $options = []): ResourceManager
100100
{
101101
$class = ltrim($fullClass, '\\');
102-
if($dispatch)
102+
if(!$dispatch)
103103
{
104-
$maxPrefix = $maxAlias = '';
105-
$prefixLen = 0;
106-
foreach($dispatch->getComponentAliases() as $alias => $namespace)
104+
$dispatch = Dispatch::instance();
105+
if($dispatch === null)
107106
{
108-
$trimNs = ltrim($namespace, '\\');
109-
$len = strlen($trimNs);
110-
if(Strings::startsWith($class, $trimNs) && $len > $prefixLen)
111-
{
112-
$maxPrefix = $trimNs;
113-
$prefixLen = $len;
114-
$maxAlias = $alias;
115-
}
107+
throw new RuntimeException("Dispatch must be available to use the component manager");
116108
}
117-
$class = str_replace($maxPrefix, $maxAlias, $class);
118109
}
110+
111+
$maxPrefix = $maxAlias = '';
112+
$prefixLen = 0;
113+
foreach($dispatch->getComponentAliases() as $alias => $namespace)
114+
{
115+
$trimNs = ltrim($namespace, '\\');
116+
$len = strlen($trimNs);
117+
if(Strings::startsWith($class, $trimNs) && $len > $prefixLen)
118+
{
119+
$maxPrefix = $trimNs;
120+
$prefixLen = $len;
121+
$maxAlias = $alias;
122+
}
123+
}
124+
$class = str_replace($maxPrefix, $maxAlias, $class);
119125
$parts = explode('\\', $class);
120126
array_unshift($parts, count($parts));
121127

@@ -153,7 +159,7 @@ public function getResourceUri($relativeFullPath): ?string
153159
public function getRelativeHash($filePath)
154160
{
155161
return Dispatch::instance()->generateHash(Dispatch::instance()->calculateRelativePath($filePath), 4);
156-
}
162+
}
157163

158164
/**
159165
* @param $relativePath

src/ResourceStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ protected function _addToStore($type, $uri, $options = null, int $priority = sel
128128
* Clear the entire resource store with a type of null, or all items stored
129129
* by a type if supplied
130130
*
131-
* @param null $type
131+
* @param string $type Store Type e.g. ResourceStore::TYPE_CSS
132132
*/
133-
public function clearStore($type = null)
133+
public function clearStore(string $type = null)
134134
{
135135
if($type === null)
136136
{

tests/ResourceManagerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function testComponent()
6161
'c/3/_/DemoComponent/DemoComponent/1a9ffb748d31/style.css',
6262
$manager->getResourceUri('style.css')
6363
);
64+
Dispatch::destroy();
65+
$this->expectException(RuntimeException::class);
66+
ResourceManager::component($component);
6467
}
6568

6669
public function testRequireJs()

0 commit comments

Comments
 (0)