Skip to content

Commit

Permalink
Component manager should throw an exception if dispatch is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Apr 10, 2019
1 parent db19628 commit 57de7ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -153,7 +159,7 @@ public function getResourceUri($relativeFullPath): ?string
public function getRelativeHash($filePath)
{
return Dispatch::instance()->generateHash(Dispatch::instance()->calculateRelativePath($filePath), 4);
}
}

/**
* @param $relativePath
Expand Down
4 changes: 2 additions & 2 deletions src/ResourceStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions tests/ResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 57de7ae

Please sign in to comment.