/** * Get a built file. Use query parameters for dynamic builds. * for dynamic builds to work, you must be in debug mode, and not have the same * build file already defined. */ public function get($build) { $Config = $this->_getConfig(); if (isset($this->params['url']['ext']) && in_array($this->params['url']['ext'], $Config->extensions())) { $build .= '.' . $this->params['url']['ext']; } if (isset($this->params['url']['theme'])) { $Config->theme($this->params['url']['theme']); } // dynamic build file if (Configure::read('debug') > 0 && $Config->files($build) === array()) { $files = array(); if (isset($this->params['url']['file'])) { $files = $this->params['url']['file']; } $Config->files($build, $files); } try { $Compiler = new AssetCompiler($Config); $contents = $Compiler->generate($build); if ($Config->cachingOn($build)) { $Cache = new AssetCache($Config); $Cache->write($build, $contents); } } catch (Exception $e) { $this->log($e->getMessage()); $this->header('HTTP/1.1 404 Not Found'); $this->autoRender = false; return; } $this->header('Content-Type: ' . $this->_getContentType($Config->getExt($build))); $this->set('contents', $contents); $this->render('contents'); }
/** * Get a built file. Use query parameters for dynamic builds. * for dynamic builds to work, you must be in debug mode, and not have the same * build file already defined. */ public function get($build) { $Config = $this->_getConfig(); $production = Configure::read('debug') == 0; if ($production && !$Config->general('alwaysEnableController')) { throw new ForbiddenException(); } if (isset($this->request->params['ext']) && in_array($this->request->params['ext'], $Config->extensions())) { $build .= '.' . $this->request->params['ext']; } if (isset($this->request->query['theme'])) { $Config->theme($this->request->query['theme']); } // Dynamically defined build file. Disabled in production for // hopefully obvious reasons. if ($Config->files($build) === array()) { $files = array(); if (isset($this->request->query['file'])) { $files = $this->request->query['file']; } $Config->files($build, $files); } try { $Compiler = new AssetCompiler($Config); $contents = $Compiler->generate($build); } catch (Exception $e) { $message = Configure::read('debug') > 0 ? $e->getMessage() : ''; throw new NotFoundException($message); } $this->response->type($Config->getExt($build)); $this->set('contents', $contents); $this->render('contents'); }
protected function postCompile($content) { $content = parent::postCompile($content); if (($baseUrl = $this->getBaseUrl()) !== null) { $content = str_replace('url(', 'url(' . $baseUrl, $content); } return $content; }
/** * Checks if request is for a compiled asset, otherwise skip any operation * * @param CakeEvent $event containing the request and response object * @throws NotFoundException * @return CakeResponse if the client is requesting a recognized asset, null otherwise */ public function beforeDispatch(CakeEvent $event) { $url = $event->data['request']->url; $Config = $this->_getConfig(); $production = !Configure::read('debug'); if ($production && !$Config->general('alwaysEnableController')) { return; } $build = $this->_getBuild($url); if ($build === false) { return; } if (isset($event->data['request']->query['theme'])) { $Config->theme($event->data['request']->query['theme']); } // Dynamically defined build file. Disabled in production for // hopefully obvious reasons. if ($Config->files($build) === array()) { $files = array(); if (isset($event->data['request']->query['file'])) { $files = $event->data['request']->query['file']; } $Config->files($build, $files); } try { $Compiler = new AssetCompiler($Config); $mtime = $Compiler->getLastModified($build); $event->data['response']->modified($mtime); if ($event->data['response']->checkNotModified($event->data['request'])) { $event->stopPropagation(); return $event->data['response']; } $contents = $Compiler->generate($build); } catch (Exception $e) { throw new NotFoundException($e->getMessage()); } $event->data['response']->type($Config->getExt($build)); $event->data['response']->body($contents); $event->stopPropagation(); return $event->data['response']; }
function testCombineThemeFileWithNonTheme() { App::build(array('views' => array($this->_testFiles . 'views' . DS))); $Config = AssetConfig::buildFromIniFile($this->_themeConfig); $Config->paths('css', array($this->_pluginPath . 'tests' . DS . 'test_files' . DS . 'css' . DS . '**')); $Config->theme('red'); $Compiler = new AssetCompiler($Config); $result = $Compiler->generate('combined.css'); $expected = <<<TEXT @import url("reset/reset.css"); #nav { \twidth:100%; }body { \tcolor: red !important; } TEXT; $this->assertEqual($result, $expected); }
public function testCompileRemoteFiles() { $Config = AssetConfig::buildFromIniFile($this->_testFiles . 'Config' . DS . 'remote_file.ini'); $Compiler = new AssetCompiler($Config); $result = $Compiler->generate('remote_file.js'); $this->assertContains('jQuery', $result); }
/** * Generate and save the cached file for a build target. * * @param string $build The build to generate. * @return void */ protected function _buildTarget($build) { $this->out('Saving file for ' . $build); $Compiler = new AssetCompiler($this->_Config); $Cacher = new AssetCache($this->_Config); try { $contents = $Compiler->generate($build); $Cacher->write($build, $contents); } catch (Exception $e) { $this->err('Error: ' . $e->getMessage()); } }
function testCompilePluginFiles() { App::build(array('Plugin' => array($this->_testFiles . 'Plugin' . DS))); CakePlugin::load('TestAsset'); $Config = AssetConfig::buildFromIniFile($this->_pluginConfig); $Config->paths('css', array($this->_pluginPath . 'Test' . DS . 'test_files' . DS . 'css' . DS . '**')); $Compiler = new AssetCompiler($Config); $result = $Compiler->generate('plugins.css'); $expected = <<<TEXT @import url("reset/reset.css"); #nav { \twidth:100%; }.plugin-box { \tcolor: orange; } TEXT; $this->assertEqual($result, $expected); }