/** * serve method * * Forward missing css/js requests to MiCompressor; if it isn't for a css or js file * bail out early and save some cycles. * * Don't render a view to prevent any helper/afterFilter logic from further manipulating * what MiCompressor has already done * * Clear the url cache, to regenerate the timestamp on the next request * * @return void * @access public */ function serve() { $url = ltrim($this->params->url, '/'); $message = 'Request for: ' . Router::url('/' . $this->params->url, true) . ' from ' . $this->referer(); $this->autoRender = false; if (!preg_match('@/?(css|js)@', $url)) { $message = 'Invalid request. ' . $message; if (Configure::read()) { $this->Session->setFlash($message); } return trigger_error($message); } if (preg_match('@^/?js/theme@', $url) && $this->_linkTheme() && file_exists(WWW_ROOT . $url)) { $this->redirect('/' . ltrim($this->params['url']['url'], '/')); } if (preg_match('@.*\\.(?:png|bmp|jpg|jpeg|gif)$@', $this->params->url)) { return $this->_missingImage($message); } if (App::import('Vendor', 'Mi.UrlCache')) { $inst = UrlCache::getInstance('/', $this->params); $inst->delete($_SERVER['REQUEST_URI']); } App::import('Vendor', 'MiAsset.MiCompressor'); $bits = explode('.', $url); $type = array_pop($bits); if (preg_match('@^/?' . $type . '/(.*)\\.' . $type . '@', $url, $matches)) { $request = $matches[1]; } else { $request = preg_replace('@\\.' . $type . '$@', '', $url); } $result = MiCompressor::serve($request, $type); if (!trim(preg_replace('@/\\*.*\\*\\/@s', '', $result))) { if ($result) { $message = $result; } return $this->_missingAsset($message); } if (Configure::read() && strpos($result, 'PROBLEM:')) { preg_match('@^(/\\*.*\\*/)@s', $result, $matches); $this->Session->setFlash('<pre>' . $matches[1] . '</pre>'); } if ($type === 'css') { header('Content-type: text/css'); } else { header('Content-type: application/javascript'); } echo $result; Configure::write('debug', 0); }
/** * compress method * * @param string $type 'css' * @return void * @access protected */ function _compress($type = 'css') { Configure::write('MiCompressor.store', true); $file = MiCompressor::cRead('configFile'); if (!file_exists($file)) { $this->out($file . ' file not found'); $this->out(' ... skipping'); return; } $this->out($file . ' found'); $config = array(); include APP . $file; if (empty($config['requestMap'][$type])) { $this->out(' ... nothing to do'); return; } include_once APP . $file; $allRequests = array(); $multiRequests = array(); $prefix = Configure::read('MiCompressor.prefix'); foreach ($config['requestMap'][$type] as $request => $_) { $allRequests[] = $request; } $allRequests = am(array_unique($allRequests), $multiRequests); sort($allRequests); MiCompressor::log(null, $this); $this->hr(); $this->out(' *** Normal Assetts ***'); foreach ($allRequests as $request) { $this->hr(); MiCompressor::serve($request, $type); MiCompressor::log(); } $this->hr(); $this->out(' *** Minified Assetts ***'); foreach ($allRequests as $request) { $this->hr(); MiCompressor::serve($request . '.min', $type); MiCompressor::log(); } }