array(), 'individualStylesheetPaths' => array(), 'stylesheet' => array(), 'script' => array(), ); protected static $_configLoaded = false; protected static $_configChanged = false; protected static $_createMergeFiles = false; /** * @param boolean $createMergeFiles */ public static function setCreateMergeFiles($createMergeFiles) { Qs_Packer::$_createMergeFiles = (bool) $createMergeFiles; } /** * @return boolean */ public static function getCreateMergeFiles() { return Qs_Packer::$_createMergeFiles; } protected static function _getConfigFileName() { return WWW_PATH . '/_lib/packer.config.php'; } public static function loadConfig() { if (Qs_Packer::$_configLoaded) { return false; } $file = Qs_Packer::_getConfigFileName(); if (!file_exists($file)) { $content = " $href, 'attribs' => $options); continue; } if (!Qs_Packer::isRegisteredStylesheet($options['href'])) { Qs_Packer::registerStylesheet($options['href']); } $path = $_path = dirname($options['href']); $media = Qs_Array::get($options, 'media', 'screen'); $isIndividualPath = false; do { if (in_array($_path, Qs_Packer::$_config['individualStylesheetPaths'])) { $isIndividualPath = true; break; } } while ('.' != ($_path = dirname($_path))); if (!$isIndividualPath) { $path = Qs_Packer::$_defaultStylesheetPath; } $itemIndex = $path . ':' . $media; if (!array_key_exists($itemIndex, $indexes)) { $indexes[$itemIndex] = array( 'path' => $path, 'media' => $media, 'indexes' => array(), ); } $indexes[$itemIndex]['indexes'][] = Qs_Packer::getStylesheetIndex($options['href']); } foreach ($indexes as $options) { $href = $options['path'] . '/' . implode('-', $options['indexes']) . Qs_Packer::_getSuffix() . '.merge.css'; $result[] = array( // 1-st argument in doc->addStylesheet function 'href' => $href, // 2-nd argument in doc->addStylesheet function 'attribs' => array( 'media' => $options['media'] ) ); } if (self::$_createMergeFiles) { self::_createMergeFiles($result);; } Qs_Packer::updateConfig(); return $result; } protected static function _createMergeFiles($links) { foreach ($links as $link) { if (!Qs_Packer::_isLocalFile($link['href'])) { continue; } if (!preg_match('/\.merge\.(css|js)$/', $link['href'])) { continue; } $file = constant('WWW_PATH') . '/' . parse_url($link['href'], PHP_URL_PATH); if (file_exists($file)) { continue; } Qs_Packer::createMergeFile($link['href']); } } public static function createMergeFile($file) { $basename = basename($file); $parts = explode('.', $basename); $indexes = explode('-', array_shift($parts)); $extension = array_pop($parts); array_pop($parts); $suffix = array_pop($parts); if (false === Qs_Packer::merge($indexes, $file, $suffix, $extension)) { return false; } return true; } public static function getMergedScripts($list) { if (empty($list)) { return false; } Qs_Packer::loadConfig(); $result = array(); $indexes = array(); foreach ($list as $options) { if (!Qs_Packer::_isLocalFile($options['src'])) { $result[] = $options['src']; continue; } if (!Qs_Packer::isRegisteredScript($options['src'])) { Qs_Packer::registerScript($options['src']); } $path = $_path = dirname($options['src']); $isIndividualPath = false; do { if (in_array($_path, Qs_Packer::$_config['individualScriptPaths'])) { $isIndividualPath = true; break; } } while ('.' != ($_path = dirname($_path))); if (!$isIndividualPath) { $path = Qs_Packer::$_defaultScriptPath; } $indexes[$path][] = Qs_Packer::getScriptIndex($options['src']); } foreach ($indexes as $path => $pathIndexes) { $result[] = $path . '/' . implode('-', $pathIndexes) . Qs_Packer::_getSuffix(). '.merge.js'; } if (self::$_createMergeFiles) { self::_createMergeFiles($result);; } Qs_Packer::updateConfig(); return $result; } protected static function _isLocalFile($file) { $fileHost = parse_url($file, PHP_URL_HOST); if ($fileHost && $fileHost != parse_url(constant('BASE_URL'), PHP_URL_HOST)) { return false; } return true; } public static function merge($indexes, $file, $suffix, $extension) { if (empty($indexes)) { return false; } Qs_Packer::loadConfig(); switch ($extension) { case 'css': $files = Qs_Packer::$_config['stylesheet']; $delimiter = "\n"; break; case 'js': $files = Qs_Packer::$_config['script']; $delimiter = ";\n"; break; default: return false; } $list = array(); foreach ($indexes as $index) { if (array_key_exists($index, $files)) { $list[$index] = $files[$index]; if ($suffix == 'pack') { $list[$index] = str_replace('.' . $extension, '.' . $suffix . '.' . $extension, $list[$index]); } } } if (!$handle = fopen(WWW_PATH . '/' . $file, 'w+')) { return false; } foreach ($list as $name) { $content = file_get_contents(WWW_PATH . '/' . $name); fwrite($handle, $content . $delimiter); } fclose($handle); return true; } }