public function js(array $params) { $jsfiles = Registry::getInstance()->assets['js']; $collection = new AssetCollection(); foreach ($jsfiles as $file) { $collection->add(new FileAsset($file, array(new JsCompressorFilter(APP_PATH . "/vendor/bin/yuicompressor.jar")))); } $cache = new AssetCache($collection, new FilesystemCache(APP_PATH . "/cache/assetic/js")); header('Content-Type: text/javascript'); echo $cache->dump(); }
public function testTwoCollectionsHasDifferentCacheKey() { $aggregateResolver = $this->getMock('AssetManager\\Resolver\\ResolverInterface'); //assets with same 'last modifled time'. $now = time(); $bacon = new Asset\StringAsset('bacon'); $bacon->setLastModified($now); $bacon->mimetype = 'text/plain'; $eggs = new Asset\StringAsset('eggs'); $eggs->setLastModified($now); $eggs->mimetype = 'text/plain'; $assets = array(array('bacon', $bacon), array('eggs', $eggs)); $aggregateResolver->expects($this->any())->method('resolve')->will($this->returnValueMap($assets)); $resolver = new CollectionResolver(array('collection1' => array('bacon'), 'collection2' => array('eggs'))); $mimeResolver = new MimeResolver(); $assetFilterManager = new \AssetManager\Service\AssetFilterManager(); $assetFilterManager->setMimeResolver($mimeResolver); $resolver->setAggregateResolver($aggregateResolver); $resolver->setAssetFilterManager($assetFilterManager); $collection1 = $resolver->resolve('collection1'); $collection2 = $resolver->resolve('collection2'); $cacheInterface = $this->getMock('Assetic\\Cache\\CacheInterface'); $cacheKeys = new \ArrayObject(); $callback = function ($key) use($cacheKeys) { $cacheKeys[] = $key; return true; }; $cacheInterface->expects($this->exactly(2))->method('has')->will($this->returnCallback($callback)); $cacheInterface->expects($this->exactly(2))->method('get')->will($this->returnValue('cached content')); $cache1 = new AssetCache($collection1, $cacheInterface); $cache1->load(); $cache2 = new AssetCache($collection2, $cacheInterface); $cache2->load(); $this->assertCount(2, $cacheKeys); $this->assertNotEquals($cacheKeys[0], $cacheKeys[1]); }
/** * Call * * This method will check the HTTP request to see if it is a CSS file. If * so, it will attempt to find a corresponding LESS file. If one is found, * it will compile the file to CSS and serve it, optionally saving the CSS * to a filesystem cache. The request will end at this point. * * If the request is not for a CSS file, or if the corresponding LESS file * is not found, this middleware will pass the request on. */ public function call() { $app = $this->app; // PHP 5.3 closures do not have access to $this, so we must proxy it in. // However, the proxy only has access to public fields. $self = $this; $app->hook('slim.before', function () use($app, $self) { $path = $app->request()->getPathInfo(); // Run filter only for requests for CSS files if (preg_match('/\\.css$/', $path)) { $path = preg_replace('/\\.css$/', '.less', $path); // Get absolute physical path on filesystem for LESS file $abs = str_replace('\\', '/', getcwd()) . $self->options['src'] . '/' . $path; $abs = str_replace('//', '/', $abs); $self->debug("Looking for LESS file: {$abs}"); $abs = realpath($abs); if ($abs === false) { // If LESS file is not found, just return $self->debug("LESS file not found: {$abs}"); return; } $self->debug("LESS file found: {$abs}"); // Prepare Assetic $lessFilter = new LessphpFilter(); $importFilter = new CssImportFilter(); $css = new FileAsset($abs, array($lessFilter, $importFilter)); if ($self->options['minify'] === true) { // Minify, if desired $self->debug("Minifying LESS file: {$abs}"); $css->ensureFilter(new CssMinFilter()); } if ($self->options['cache'] === true) { // Cache results, if desired $self->debug("Caching LESS file: {$self->options['cache.dir']}"); $cache = new FilesystemCache($self->options['cache.dir']); $css = new AssetCache($css, $cache); } // Render results and exit $res = $app->response(); $res['Content-Type'] = 'text/css'; $app->halt(200, $css->dump()); } }); $this->next->call(); }
/** * @param AssetCollectionInterface $assetCollection * @param CacheInterface $cache */ public function __construct(AssetCollectionInterface $assetCollection, CacheInterface $cache) { parent::__construct($assetCollection, $cache); $this->assetCollection = $assetCollection; }
use Assetic\Asset\AssetCollection; use Assetic\Asset\FileAsset; use Assetic\Asset\GlobAsset; use Assetic\AssetManager; use Assetic\Asset\AssetCache; use Assetic\Cache\FilesystemCache; use Shrubbery\QueryProcesor; use Shrubbery\RestaurantProcessor; use Shrubbery\Config; $app = new Silex\Application(); $app['debug'] = true; $app['asset_path'] = 'views'; //todo move this to a separate function $bundles = array(new Symfony\Bundle\AsseticBundle\AsseticBundle()); $styles = new FileAsset('./vendor/twitter/bootstrap/dist/css/bootstrap.css'); $cache = new AssetCache($styles, new FilesystemCache('./views/cache')); $cache->setTargetPath('bootstrap.css'); $writer = new AssetWriter('./views/assets'); $writer->writeAsset($cache); //end todo $getRestaurantList = new RestaurantProcessor(); $jsonObj = $getRestaurantList->readFromDatabase(); //Section for adding configuration $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../templates')); $twig = $app['twig']; $twig->addExtension(new \Entea\Twig\Extension\AssetExtension($app)); $app->get('/', function (Request $request) use($app) { $output = ''; $finalList = ''; $apiKey = ''; $ini_Obj = parse_ini_file("data.ini", true);
protected function processAssets() { // - Alias. $p = $this->paths; $app_name = $this->app_name; $af = $this->asset_factory; $fm = $this->asset_factory->getFilterManager(); $am = $this->asset_manager; $ac = $this->asset_cache; // - During debugging, assets aren't minified and so are named differently. $min = $this->debug ? '' : '.min'; // - Initialize `app_assets` and `theme_assets`. We can do this because PHP // arrays are assign-by-copy by default. $app_assets = $theme_assets = array('coffee' => array(), 'css' => array(), 'js' => array()); // - Populate optional assets via `addOptionalAssets`. For Silex // `app_assets`, if the configuration option group `view` exists in the // application scope, assume its asset-properties are set and specifying // optional assets. For application `theme_assets`, the same applies if // the configuration option group `view` exists in the theme scope. if (isset($this->options['view'])) { $this->addOptionalAssets($app_assets, $this->options['view']); } if (isset($this->theme_options) && isset($this->theme_options['view'])) { $this->addOptionalAssets($theme_assets, $this->theme_options['view']); } // - `css_assets` is the final array of Assetic asset and asset collection // objects that represents the list of stylesheets to be compiled in the // final distribution stylesheet. It should tentatively be empty for // assets to be handled via S3 on prod. It starts off with the merged // result of the base library and application stylesheets, as well as the // optional library stylesheets for the application and theme, even if // there aren't any. $css_assets = $this->is_prod ? array() : array_merge(array(new GlobAsset(array("{$p['lib_web']}/css/*.css")), new FileAsset("{$p['app_web']}/scss/style.scss", array($fm->get('scss')))), $app_assets['css'], $theme_assets['css']); // - Run the `willRegisterCSSAssets` hook to transform `css_assets` and get // an updated `css_modifier` for the filename, though it may still be // empty. list($css_assets, $css_modifier) = $this->willRegisterCSSAssets($css_assets); // - Add the stylesheet for the theme if needed. if ($this->is_themable) { $css_assets[] = new FileAsset("{$p['theme_web']}/scss/style.scss", array($fm->get('scss'))); } // - Our `css_filters` only has the optional minifier when not debugging. $css_filters = $this->debug ? array() : array($fm->get('cssmin')); // - Our `css_cache` wraps around our assets and filters for distribution. // Tie it to `asset_cache`. Build our distribution filename. And export it // to `asset_manager`. $css_cache = new AssetCache(new AssetCollection($css_assets, $css_filters), $ac); $css_cache->setTargetPath("{$app_name}{$css_modifier}.compiled{$min}.css"); $am->set('all_css', $css_cache); // - Finally, add an `asset_urls` entry for `css`. $this->asset_urls['css'][] = "/dist/{$css_cache->getTargetPath()}"; // - `js_assets` is the final collection of arrays of Assetic asset and // asset collection objects. The arrays are labeled by section and // represent the list of scripts to be compiled in the final distribution // script for each section. The arrays should tentatively be empty for // assets to be handled via S3 on prod. $js_assets = array(); // - Add a `head` section with just the scripts in the `js/head` // subdirectory of the `lib_web` path. $js_assets['head'] = $this->is_prod ? array() : array(new GlobAsset(array("{$p['lib_web']}/js/head/*.js"))); // - Add a `lib` section with the root-level scripts in the `js` // subdirectory of the `lib_web` path. Also include any scripts in the // `js/development` subdirectory. Lastly, include any root-level scripts // in the `coffee` subdirectory. Note the passing in of the `coffee` // filter. $js_assets['lib'] = $this->is_prod ? array() : array(new GlobAsset(array("{$p['lib_web']}/js/*.js", "{$p['lib_web']}/js/development/*.js")), new GlobAsset(array("{$p['lib_web']}/coffee/*.coffee"), array($fm->get('coffee')))); // - Add a `main` section. Include the optional library scripts (js and // coffee) for the application and theme. Note that this isn't in the // `lib` section to ensure the lib distribution file stays consistent // across requests. $js_assets['main'] = $this->is_prod ? array() : array_merge($app_assets['coffee'], $app_assets['js'], array(new GlobAsset(array("{$p['app_web']}/coffee/*.coffee", "{$p['app_web']}/coffee/main.coffee", "{$p['app_web']}/coffee/**/*.coffee"), array($fm->get('coffee')))), $theme_assets['coffee'], $theme_assets['js']); // - Run the `willRegisterJSAssets` hook to transform `js_assets` and get an // updated `js_modifiers` for the filenames that's labeled by the section // names, though they may still be empty. list($js_assets, $js_modifiers) = $this->willRegisterJSAssets($js_assets); // - Add the script for the theme if needed to `main`. if ($this->is_themable) { $js_assets['main'][] = new GlobAsset("{$p['theme_web']}/coffee/*.coffee", array($fm->get('coffee'))); } // - Our `js_filters` only has the optional minifier when not debugging. $js_filters = $this->debug ? array() : array($fm->get('jsmin')); // - `js_map` transforms the section names into filename modifiers. $js_map = array('head' => 'head', 'lib' => 'lib', 'main' => ''); // - For each section, set `js_cache` to wrap around our assets and filters // for distribution. Tie it to `asset_cache`. Build our distribution // `file_modifier` and filename. And export it to `asset_manager`. // Finally, add an `asset_urls` entry by a hard-coded map depending on // section name. foreach ($js_map as $name => $file_modifier) { $js_cache = new AssetCache(new AssetCollection($js_assets[$name], $js_filters), $ac); $file_modifier = !empty($file_modifier) ? "-{$file_modifier}" : ''; $file_modifier .= isset($js_modifiers[$name]) ? $js_modifiers[$name] : ''; $js_cache->setTargetPath("{$app_name}{$file_modifier}.compiled{$min}.js"); $am->set("{$name}_js", $js_cache); if ($name === 'head') { $this->asset_urls['head_js'][] = "/dist/{$js_cache->getTargetPath()}"; } else { $this->asset_urls['js'][] = "/dist/{$js_cache->getTargetPath()}"; } } // - Finally, have `asset_writer` write to disk the assets from // `asset_manager`. However, on prod, this is tentatively unneeded due to // S3 handling. if (!$this->is_prod) { $this->asset_writer->writeManagerAssets($am); } }
private function compressAndSave($targetFile, $assets, $filters) { $assetsFactory = $this->assetic->build(); $asset = $assetsFactory->createAsset($assets, $filters); if (null === $this->cacheDir) { $this->saveFile($targetFile, $asset->dump(), false); return; } $assetCache = new AssetCache($asset, new FilesystemCache($this->cacheDir)); $this->saveFile($targetFile, $assetCache->dump(), true); }