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(); }
/** * 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(); }
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); }