Skip to content

Commit

Permalink
Support for include css/js
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 25, 2019
1 parent 0b99dd8 commit 2ab7ad0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/ResourceManager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Packaged\Dispatch;

use Exception;
use Packaged\Dispatch\Component\DispatchableComponent;
use Packaged\Dispatch\Component\FixedClassComponent;
use Packaged\Helpers\Path;
Expand Down Expand Up @@ -179,7 +180,7 @@ public function getFileHash($fullPath)
{
if(!file_exists($fullPath))
{
if($this->getOption(self::OPT_THROW_ON_FILE_NOT_FOUND, false))
if($this->getOption(self::OPT_THROW_ON_FILE_NOT_FOUND, true))
{
throw new RuntimeException("Unable to find dispatch file '$fullPath'", 404);
}
Expand Down Expand Up @@ -223,6 +224,26 @@ public function isExternalUrl($path)
);
}

/**
* Add js to the store, ignoring exceptions
*
* @param string $toRequire filename, or JS if inline manager
* @param $options
*
* @return ResourceManager
*/
public function includeJs($toRequire, $options = null)
{
try
{
return $this->requireJs($toRequire, $options);
}
catch(Exception $e)
{
return $this;
}
}

/**
* Add js to the store
*
Expand Down Expand Up @@ -274,6 +295,26 @@ public function requireCss($toRequire, $options = null)
return $this;
}

/**
* Add css to the store, ignoring exceptions
*
* @param string $toRequire filename, or CSS if inline manager
* @param $options
*
* @return ResourceManager
*/
public function includeCss($toRequire, $options = null)
{
try
{
return $this->requireCss($toRequire, $options = null);
}
catch(Exception $e)
{
return $this;
}
}

/**
* Add css to the store
*
Expand Down
9 changes: 8 additions & 1 deletion src/Resources/AbstractDispatchableResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ protected function _getDispatchUrl($path): string

$newPath = $this->_makeFullPath($newPath, dirname($this->_path));

$url = $this->_manager->getResourceUri($newPath);
try
{
$url = $this->_manager->getResourceUri($newPath);
}
catch(\RuntimeException $e)
{
$url = null;
}
if(empty($url))
{
return $path;
Expand Down
5 changes: 5 additions & 0 deletions tests/ResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function testUniqueRequire()
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
ResourceManager::resources()->requireJs('js/alert.js');
$this->assertCount(1, Dispatch::instance()->store()->getResources(ResourceStore::TYPE_JS));
ResourceManager::resources()->includeJs('js/alert.js');
ResourceManager::resources()->requireJs('js/alert.js');
$this->assertCount(1, Dispatch::instance()->store()->getResources(ResourceStore::TYPE_JS));
ResourceManager::resources()->requireJs('js/misc.js');
Expand All @@ -87,6 +88,7 @@ public function testUniqueRequire()
public function testRequireCss()
{
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
ResourceManager::resources()->includeCss('css/test.css');
ResourceManager::resources()->requireCss('css/test.css');
$this->assertContains(
'href="r/f643eb32/css/test.css"',
Expand Down Expand Up @@ -130,8 +132,11 @@ public function testMissingFile()
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
$component = new DemoComponent();
$manager = ResourceManager::component($component);
$manager->setOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, false);
$this->assertNull($manager->getResourceUri('style.missing.css'));
$manager->setOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true);
$manager->includeCss('style.missing.css');
$manager->includeJs('script.missing.js');
$this->expectExceptionCode(404);
$this->expectException(RuntimeException::class);
$manager->getResourceUri('style.missing.css');
Expand Down

0 comments on commit 2ab7ad0

Please sign in to comment.