Skip to content

Commit 2ab7ad0

Browse files
committed
Support for include css/js
1 parent 0b99dd8 commit 2ab7ad0

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/ResourceManager.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Packaged\Dispatch;
33

4+
use Exception;
45
use Packaged\Dispatch\Component\DispatchableComponent;
56
use Packaged\Dispatch\Component\FixedClassComponent;
67
use Packaged\Helpers\Path;
@@ -179,7 +180,7 @@ public function getFileHash($fullPath)
179180
{
180181
if(!file_exists($fullPath))
181182
{
182-
if($this->getOption(self::OPT_THROW_ON_FILE_NOT_FOUND, false))
183+
if($this->getOption(self::OPT_THROW_ON_FILE_NOT_FOUND, true))
183184
{
184185
throw new RuntimeException("Unable to find dispatch file '$fullPath'", 404);
185186
}
@@ -223,6 +224,26 @@ public function isExternalUrl($path)
223224
);
224225
}
225226

227+
/**
228+
* Add js to the store, ignoring exceptions
229+
*
230+
* @param string $toRequire filename, or JS if inline manager
231+
* @param $options
232+
*
233+
* @return ResourceManager
234+
*/
235+
public function includeJs($toRequire, $options = null)
236+
{
237+
try
238+
{
239+
return $this->requireJs($toRequire, $options);
240+
}
241+
catch(Exception $e)
242+
{
243+
return $this;
244+
}
245+
}
246+
226247
/**
227248
* Add js to the store
228249
*
@@ -274,6 +295,26 @@ public function requireCss($toRequire, $options = null)
274295
return $this;
275296
}
276297

298+
/**
299+
* Add css to the store, ignoring exceptions
300+
*
301+
* @param string $toRequire filename, or CSS if inline manager
302+
* @param $options
303+
*
304+
* @return ResourceManager
305+
*/
306+
public function includeCss($toRequire, $options = null)
307+
{
308+
try
309+
{
310+
return $this->requireCss($toRequire, $options = null);
311+
}
312+
catch(Exception $e)
313+
{
314+
return $this;
315+
}
316+
}
317+
277318
/**
278319
* Add css to the store
279320
*

src/Resources/AbstractDispatchableResource.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ protected function _getDispatchUrl($path): string
119119

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

122-
$url = $this->_manager->getResourceUri($newPath);
122+
try
123+
{
124+
$url = $this->_manager->getResourceUri($newPath);
125+
}
126+
catch(\RuntimeException $e)
127+
{
128+
$url = null;
129+
}
123130
if(empty($url))
124131
{
125132
return $path;

tests/ResourceManagerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function testUniqueRequire()
7878
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
7979
ResourceManager::resources()->requireJs('js/alert.js');
8080
$this->assertCount(1, Dispatch::instance()->store()->getResources(ResourceStore::TYPE_JS));
81+
ResourceManager::resources()->includeJs('js/alert.js');
8182
ResourceManager::resources()->requireJs('js/alert.js');
8283
$this->assertCount(1, Dispatch::instance()->store()->getResources(ResourceStore::TYPE_JS));
8384
ResourceManager::resources()->requireJs('js/misc.js');
@@ -87,6 +88,7 @@ public function testUniqueRequire()
8788
public function testRequireCss()
8889
{
8990
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
91+
ResourceManager::resources()->includeCss('css/test.css');
9092
ResourceManager::resources()->requireCss('css/test.css');
9193
$this->assertContains(
9294
'href="r/f643eb32/css/test.css"',
@@ -130,8 +132,11 @@ public function testMissingFile()
130132
Dispatch::bind(new Dispatch(Path::system(__DIR__, '_root')));
131133
$component = new DemoComponent();
132134
$manager = ResourceManager::component($component);
135+
$manager->setOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, false);
133136
$this->assertNull($manager->getResourceUri('style.missing.css'));
134137
$manager->setOption(ResourceManager::OPT_THROW_ON_FILE_NOT_FOUND, true);
138+
$manager->includeCss('style.missing.css');
139+
$manager->includeJs('script.missing.js');
135140
$this->expectExceptionCode(404);
136141
$this->expectException(RuntimeException::class);
137142
$manager->getResourceUri('style.missing.css');

0 commit comments

Comments
 (0)