From ebef6cde873201c25e1d2be0dee3668533454727 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Thu, 8 May 2014 12:37:39 +0100 Subject: [PATCH] Allow the asset manager to generate html includes --- composer.json | 14 +++--- src/AssetManager.php | 89 +++++++++++++++++++++++++++++++++----- tests/AssetManagerTest.php | 47 ++++++++++++++++++++ 3 files changed, 131 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index e9805bd..2f99c9d 100644 --- a/composer.json +++ b/composer.json @@ -1,26 +1,26 @@ { - "name": "packaged/dispatch", - "license": "MIT", - "authors": [ + "name": "packaged/dispatch", + "license": "MIT", + "authors": [ { "name": "Brooke Bryan", "email": "brooke@bajb.net" } ], - "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" } diff --git a/src/AssetManager.php b/src/AssetManager.php index a5a2e02..80e1a83 100644 --- a/src/AssetManager.php +++ b/src/AssetManager.php @@ -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 = ''; + + if($for == 'css') + { + $template = ''; + } + else if($for == 'js') + { + $template = ''; + } + + $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; } /** @@ -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 * @@ -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 + ); + } } /** @@ -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 + ); + } } } diff --git a/tests/AssetManagerTest.php b/tests/AssetManagerTest.php index 623caa5..5b57119 100644 --- a/tests/AssetManagerTest.php +++ b/tests/AssetManagerTest.php @@ -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( + '', + \Packaged\Dispatch\AssetManager::generateHtmlIncludes('css') + ); + + $this->assertEquals( + '', + \Packaged\Dispatch\AssetManager::generateHtmlIncludes('js') + ); + $this->assertEquals( + '', + \Packaged\Dispatch\AssetManager::generateHtmlIncludes('fnt') + ); } public function testConstructException()