BUG switch to trigger_error() when a resource is not found (#7468)

* BUG switch to `trigger_error()` when a resource is not found rather than throw an exception

* Add unit test for module url failing
This commit is contained in:
Chris Joe 2017-10-16 16:11:42 +13:00 committed by Aaron Carlino
parent 8d36d55205
commit cafa3fc29a
3 changed files with 11 additions and 5 deletions

View File

@ -77,7 +77,7 @@ class SimpleResourceURLGenerator implements ResourceURLGenerator
$exists = file_exists($absolutePath);
}
if (!$exists) {
throw new InvalidArgumentException("File {$relativePath} does not exist");
trigger_error("File {$relativePath} does not exist", E_USER_NOTICE);
}
// Apply url rewrites
@ -89,7 +89,7 @@ class SimpleResourceURLGenerator implements ResourceURLGenerator
// Apply nonce
$nonce = '';
// Don't add nonce to directories
if ($this->nonceStyle && is_file($absolutePath)) {
if ($this->nonceStyle && $exists && is_file($absolutePath)) {
$nonce = (strpos($relativePath, '?') === false) ? '?' : '&';
switch ($this->nonceStyle) {

View File

@ -106,9 +106,7 @@ class ModuleResourceLoader implements TemplateGlobalProvider
throw new InvalidArgumentException("Can't find module '$module'");
}
$resourceObj = $moduleObj->getResource($resource);
if (!$resourceObj->exists()) {
throw new InvalidArgumentException("Module '$module' does not have specified resource '$resource'");
}
return $resourceObj;
}
}

View File

@ -64,4 +64,12 @@ class SimpleResourceURLGeneratorTest extends SapphireTest
$generator->urlForResource($module->getResource('client/style.css'))
);
}
public function testURLForResourceFailsGracefully()
{
/** @var SimpleResourceURLGenerator $generator */
$generator = Injector::inst()->get(ResourceURLGenerator::class);
$url = @$generator->urlForResource('doesnotexist.jpg');
$this->assertEquals('/doesnotexist.jpg', $url);
}
}