public function buildCss() { App::import('Vendor', 'AssetMinify.JSMinPlus'); // Ouverture des fichiers de config $dir = new Folder(Configure::read('App.www_root') . 'css' . DS . 'minified'); if ($dir->path !== null) { foreach ($dir->find('config_.*.ini') as $file) { preg_match('`^config_(.*)\\.ini$`', $file, $grep); $file = new File($dir->pwd() . DS . $file); $ini = parse_ini_file($file->path, true); $fileFull = new File($dir->path . DS . 'full_' . $grep[1] . '.css', true, 0644); $fileGz = new File($dir->path . DS . 'gz_' . $grep[1] . '.css', true, 0644); $contentFull = ''; foreach ($ini as $data) { // On a pas de version minifié if (!($fileMin = $dir->find('file_' . md5($data['url'] . $data['md5']) . '.css'))) { $fileMin = new File($dir->path . DS . 'file_' . md5($data['url'] . $data['md5']) . '.css', true, 0644); $this->out("Compression de " . $data['file'] . ' ... ', 0); $fileMin->write(MinifyUtils::compressCss(MinifyUtils::cssAbsoluteUrl($data['url'], file_get_contents($data['file'])))); $this->out('OK'); } else { $fileMin = new File($dir->path . DS . 'file_' . md5($data['url'] . $data['md5']) . '.css'); } $contentFull .= $fileMin->read() . PHP_EOL; } // version full $fileFull->write($contentFull); $fileFull->close(); // compression $fileGz->write(gzencode($contentFull, 6)); } } }
private function _buildCss() { // if ($this->cssMinifiedDir->path !== null) { foreach ($this->cssMinifiedDir->find('config_.*.ini') as $file) { preg_match('`^config_(.*)\\.ini$`', $file, $grep); $file = new File($this->cssMinifiedDir->pwd() . DS . $file); $ini = parse_ini_file($file->path, true); $fileFull = new File($this->cssMinifiedDir->path . DS . 'full_' . $grep[1] . '.css', true, 0644); $fileGz = new File($this->cssMinifiedDir->path . DS . 'gz_' . $grep[1] . '.css', true, 0644); $contentFull = ''; foreach ($ini as $data) { // On a pas de version minifié if (!($fileMin = $this->cssMinifiedDir->find('file_' . md5($data['url'] . $data['md5']) . '.css'))) { $fileMin = new File($this->cssMinifiedDir->path . DS . 'file_' . md5($data['url'] . $data['md5']) . '.css', true, 0644); $fileMin->write(MinifyUtils::compressCss(MinifyUtils::cssAbsoluteUrl($data['url'], file_get_contents(APP . $data['file'])))); } else { $fileMin = new File($this->cssMinifiedDir->path . DS . 'file_' . md5($data['url'] . $data['md5']) . '.css'); } $contentFull .= $fileMin->read() . PHP_EOL; } // version full $fileFull->write($contentFull); $fileFull->close(); // compression $fileGz->write(gzencode($contentFull, 6)); } } }
private function _buildJsConfig($block = 0) { // collecte des fichiers $paths = array(); foreach ($this->_scripts[$block] as $md5 => $data) { $url = $this->assetUrl($data['file'], array('pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js')); $url = preg_replace('`^' . $this->request->webroot . '`', '/', $url); $file = MinifyUtils::getPath($url); if (!file_exists(APP . $file)) { continue; } $paths[] = array('url' => $url, 'md5' => md5_file(APP . $file), 'file' => preg_replace('`(\\\\|\\/)+`', '/', $file)); } $md5 = md5(implode(';', Hash::combine($paths, '{n}.url', array('%s => %s', '{n}.url', '{n}.md5')))); $jsIdentity = Configure::read('debug') > 0 ? md5(implode(';', Hash::extract($this->_scripts[$block], '{s}.file'))) : $md5; // Ecriture du fichier de config $iniFile = new File($this->jsMinifiedDir->path . DS . 'config_' . $jsIdentity . '.ini', true, 0600); if ($iniFile->read() === '') { $iniContent = array('; <?php exit; ?>'); foreach ($paths as $data) { $iniContent[] = '[' . md5($data['url']) . ']'; $iniContent[] = 'url=' . $data['url']; $iniContent[] = 'file=' . $data['file']; $iniContent[] = 'md5=' . $data['md5']; $iniContent[] = ''; } $iniFile->write(implode(PHP_EOL, $iniContent)); } $this->_urlJs[$block] = Router::url(array('plugin' => 'AssetMinify', 'controller' => 'minify', 'action' => 'js', 'id' => $jsIdentity, '?' => Configure::read('debug') > 0 ? $md5 : null), true); }
static function cssAbsoluteUrl($abs, $content) { $images = array(); $parse = parse_url($abs); $abs = $parse['path']; if (preg_match('`(\\.\\w{1,3}|/)$`', $abs)) { $abs = dirname($abs); } if (self::$webroot === null) { self::$webroot = Router::url('/'); } $baseUrl = self::$webroot . 'css_minified/'; $assetPath = Configure::read('App.www_root') . 'css_minified'; preg_match_all('`url\\((\'|\\")?(?!data:image)((\\w|\\.|\\/)+[^\\)]+[^\'\\"])(\'|\\")?\\)`', $content, $grep); $pattern = array(); $replace = array(); foreach ($grep[2] as $k => $relative) { $filePath = realpath(APP . MinifyUtils::getPath($abs . DS . $relative)); /* * Si il n'a pas déjà été traité, on copie ce fichier vers /webroot/css_minified * avec un nom MD5 qui va bien puis on remplace l'url dans la CSS par ce fichier racine */ if ($filePath !== false) { // Extrait uniquement le fichier et laisse les fragments/paramètres preg_match('/((#|\\?).*[^\\)"\'])/', $grep[0][$k], $fragments); // Ajoute le pattern de recherche pour remplacement $pattern[] = '`' . preg_quote($grep[0][$k]) . '`'; $md5name = md5($filePath); if (!isset($images[$md5name])) { $fileInfo = MinifyUtils::getFileInfo($filePath, $assetPath); // fichier destination n'existe pas if (!file_exists($fileInfo['destfile'])) { // on le copie copy($fileInfo['srcfile'], $fileInfo['destfile']); } $images[$fileInfo['md5name']] = $fileInfo; } $replace[] = 'url("' . $baseUrl . $images[$md5name]['destname'] . (!empty($fragments) ? $fragments[0] : '') . '")'; } } return preg_replace($pattern, $replace, $content); }