Skip to content

Commit

Permalink
Allow the asset manager to generate html includes
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed May 8, 2014
1 parent f6fecf8 commit ebef6cd
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 19 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"name": "packaged/dispatch",
"license": "MIT",
"authors": [
"name": "packaged/dispatch",
"license": "MIT",
"authors": [
{
"name": "Brooke Bryan",
"email": "[email protected]"
}
],
"bin": [
"bin": [
"bin/dispatch"
],
"require": {
"require": {
"php": ">=5.4.0",
"packaged/helpers": "0.0.*",
"packaged/config": "0.0.*",
"symfony/http-kernel": "2.4.*",
"symfony/console": "2.4.*"
},
"require-dev": {
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"autoload": {
"psr-4": {
"Packaged\\Dispatch\\": "src"
}
Expand Down
89 changes: 77 additions & 12 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,50 @@ class AssetManager
*
* @param string $type
*
* @return mixed
* @return array|null
*/
public static function getUrisByType($type = 'js')
{
return static::$_resourceStore[$type];
return isset(static::$_resourceStore[$type]) ?
static::$_resourceStore[$type] : null;
}

public static function generateHtmlIncludes($for = 'css')
{
if(!isset(static::$_resourceStore[$for])
|| empty(static::$_resourceStore[$for])
)
{
return '';
}

$template = '<link href="%s"%s>';

if($for == 'css')
{
$template = '<link href="%s" rel="stylesheet" type="text/css"%s>';
}
else if($for == 'js')
{
$template = '<script src="%s"%s></script>';
}

$return = '';
foreach(static::$_resourceStore[$for] as $uri => $options)
{
$opts = $options;
if(is_array($options))
{
$opts = '';
foreach($options as $key => $value)
{
$value = ValueAs::string($value);
$opts .= " $key=\"$value\"";
}
}
$return .= sprintf($template, $uri, $opts);
}
return $return;
}

/**
Expand Down Expand Up @@ -231,6 +270,24 @@ protected function _addToStore($type, $uri, $options = null)
static::$_resourceStore[$type][$uri] = $options;
}

/**
* Clear the entire resource store with a type of null, or all items stored
* by a type if supplied
*
* @param null $type
*/
public function clearStore($type = null)
{
if($type === null)
{
static::$_resourceStore = [];
}
else
{
unset(static::$_resourceStore[$type]);
}
}

/**
* Add a js file to the store
*
Expand All @@ -239,11 +296,15 @@ protected function _addToStore($type, $uri, $options = null)
*/
public function requireJs($filename, $options = null)
{
static::_addToStore(
'js',
$this->getResourceUri($filename . '.js'),
$options
);
$filenames = ValueAs::arr($filename);
foreach($filenames as $filename)
{
static::_addToStore(
'js',
$this->getResourceUri($filename . '.js'),
$options
);
}
}

/**
Expand All @@ -254,10 +315,14 @@ public function requireJs($filename, $options = null)
*/
public function requireCss($filename, $options = null)
{
static::_addToStore(
'css',
$this->getResourceUri($filename . '.css'),
$options
);
$filenames = ValueAs::arr($filename);
foreach($filenames as $filename)
{
static::_addToStore(
'css',
$this->getResourceUri($filename . '.css'),
$options
);
}
}
}
47 changes: 47 additions & 0 deletions tests/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,59 @@ public function testStore()
$manager = \Packaged\Dispatch\AssetManager::assetType();
$manager->requireCss('test', ['delay' => true]);
$manager->requireJs('test');

$this->assertEquals(
[
'//www.packaged.in/res/p/8cac7/b/76d6c18/test.css' => ['delay' => true]
],
\Packaged\Dispatch\AssetManager::getUrisByType('css')
);

$this->assertEquals(
[
'//www.packaged.in/res/p/8cac7/b/e2218e4/test.js' => null
],
\Packaged\Dispatch\AssetManager::getUrisByType('js')
);

$this->assertNotNull(\Packaged\Dispatch\AssetManager::getUrisByType('css'));
$manager->clearStore('css');
$this->assertEmpty(\Packaged\Dispatch\AssetManager::getUrisByType('css'));

$this->assertNotNull(\Packaged\Dispatch\AssetManager::getUrisByType('js'));
$manager->clearStore();
$this->assertEmpty(\Packaged\Dispatch\AssetManager::getUrisByType('js'));
}

public function testGenerateHtmlIncludes()
{
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$request->headers->set('HOST', 'www.packaged.in');
$request->server->set('REQUEST_URI', '/');
$opts = ['assets_dir' => 'asset'];
$opt = new \Packaged\Config\Provider\ConfigSection('', $opts);
$dispatcher = new \Packaged\Dispatch\Dispatch(new DummyKernel(), $opt);
$dispatcher->setBaseDirectory(__DIR__);
$dispatcher->handle($request);
$manager = \Packaged\Dispatch\AssetManager::assetType();
$manager->requireCss('test');
$manager->requireJs('test', ['delay' => true]);

$this->assertEquals(
'<link href="//www.packaged.in/res/p/8cac7/b/76d6c18/test.css"' .
' rel="stylesheet" type="text/css">',
\Packaged\Dispatch\AssetManager::generateHtmlIncludes('css')
);

$this->assertEquals(
'<script src="//www.packaged.in/res/p/8cac7/b/e2218e4/test.js"' .
' delay="true"></script>',
\Packaged\Dispatch\AssetManager::generateHtmlIncludes('js')
);
$this->assertEquals(
'',
\Packaged\Dispatch\AssetManager::generateHtmlIncludes('fnt')
);
}

public function testConstructException()
Expand Down

0 comments on commit ebef6cd

Please sign in to comment.