/** * 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); }
/** * populateVendorMap method * * @param bool $reset false * @static * @return void * @access protected */ protected static function _populateVendorMap($reset = false) { if ($reset || !self::$vendorMap) { App::import('Vendor', 'Mi.MiCache'); $plugins = MiCache::mi('plugins'); foreach ($plugins as $path => $plugin) { if (is_dir($path . DS . 'webroot')) { if (!class_exists('Mi')) { APP::import('Vendor', 'Mi.Mi'); } $files = Mi::files($path . DS . 'webroot', null, '.*\\.(css|js)$'); foreach ($files as $fPath) { $fPath = realpath($fPath); self::$vendorMap[str_replace(realpath($path . DS . 'webroot') . DS, $plugin . DS, $fPath)] = $fPath; } } } self::log("Populating Vendor Map"); if (!class_exists('MiCache')) { self::log("\tMiCache doesn't exist. Skipping."); return; } self::$vendorMap = am(self::$vendorMap, MiCache::mi('vendors', null, array('shells'), array('excludeFolders' => array('shells', 'simpletest'), 'extension' => array('css', 'js'), 'excludePattern' => false))); self::log("\tDone."); } }
/** * printJs method * * @param mixed $sizeLimit null * @return void * @access protected */ function _printJs($sizeLimit = null) { foreach ($this->__jsViewStack as $package => $files) { if (isset($this->__jsStack[$package])) { $this->__jsStack[$package] = Set::merge($this->__jsStack[$package], $this->__jsViewStack[$package]); } else { $this->__jsStack[$package] = $this->__jsViewStack[$package]; } } $this->__jsViewStack = array(); if (!$this->__jsStack) { $return = $this->__scripts; $this->__scripts = ''; return $return; } if ($sizeLimit === true) { if (!isset($this->__RequestHandler)) { App::import('Component', 'RequestHandler'); $this->__RequestHandler = new RequestHandlerComponent(); } if ($this->__RequestHandler->isMobile()) { $sizeLimit = 25 * 1024; } } if (isset($this->__jsStack['jquery'])) { $this->__jsStack = array_merge(array('jquery' => $this->__jsStack['jquery']), $this->__jsStack); } $urls = MiCompressor::url($this->__jsStack, array('type' => 'js', 'sizeLimit' => $sizeLimit)); $this->__jsStack = array('default' => array()); $return = array(); foreach ((array) $urls as $url) { $url = str_replace($this->webroot, '/', $this->url($url)); $return[] = $this->Html->script($url); } $return[] = $this->__scripts . $this->__scriptsView; $this->__scripts = $this->__scriptsView = ''; return "\r" . implode("\r", $return) . "\r"; }
/** * 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(); } }