private function getMap(PreprocessTool $tool) { $ret = array(); $mapKey = comma_str_to_array($this->options['requirejs.map']); foreach ($mapKey as $key) { $ret = array_merge($ret, $tool->getMap($key)); } // 去掉自身 unset($ret[str_replace(C('SRC.SRC_PATH'), '', $this->options['requirejs.path'])]); // map中地址为实际地址,将其变为引用地址 // 为value加入cdn $ret = array_combine(array_map(create_function('$key', 'return Tool::getVirtualPath($key);'), array_keys($ret)), array_map(create_function('$value', 'return Tool::addCdn($value);'), array_values($ret))); return $ret; }
/** * 处理media资源,地址替换 */ private function handleMediaResource() { static $reg = null; if (is_null($reg)) { $processor = parent::getInstance('media'); $mediaTypes = $processor->getOptions(); $mediaTypes = comma_str_to_array($mediaTypes['type']); if (!empty($mediaTypes)) { $reg = '/(?<=[\'\\"])(?:' . str_replace('/', '\\/', C('STATIC_VIRTUAL_PREFIX')) . '[^\'\\"\\\\]{1,300})\\.(?:' . implode('|', $mediaTypes) . ')(?=[\'\\"\\\\])/'; } else { $reg = false; } } if ($reg) { $this->contents = preg_replace_callback($reg, array($this, 'replaceMediaPath'), $this->contents); } }
/** * 写编译后文件 * @param Preprocess $processor * @param $item * @param $path */ public function writeBuildFile(Preprocess $processor, $item, $path) { trigger('write_build_file_start', $processor); // 如果设置了to参数,则改变编译后文件路径 if (!$this->isWhitefile($path)) { if (!empty($item['to'])) { // 是否md5化 if (C('IS_MD5') && in_array($item['processor'], array('media', 'css', 'js'))) { $path = $item['to'] . '/' . $processor->getFileUid() . '.' . $processor->getType(); } else { if ($item['subfile']) { $froms = comma_str_to_array($item['from']); $patterns = array(); foreach ($froms as $from) { array_push($patterns, "/^" . str_replace('/', '\\/', $from) . "/"); } $path = $item['to'] . preg_replace($patterns, array(""), $path); } else { $path = $item['to'] . $path; // for webapp node // $path = $item['to'].'/'.$processor->getFilename(); } } } } else { if (C('WHITE_LIST_TO')) { $path = C('WHITE_LIST_TO') . $path; } else { $path = C('STATIC_VIRTUAL_PREFIX') . $path; } } $buildPath = C('SRC.BUILD_PATH') . $path; $cachePath = C('SRC.BUILD_CACHE_PATH') . $path; $contents = $processor->getContents(); // 写入文件到编译后路径中 contents_to_file($cachePath, $contents); contents_to_file($buildPath, $this->onlineStrReplace($contents, $processor)); return $path; }
/** * 递归扫描,得到所需的文件 * @param $paths * @param $types * @param string $root * @return array */ function get_files_by_type($paths, $types, $root = '') { $ret = array(); $paths = comma_str_to_array($paths); $types = comma_str_to_array($types); foreach ($paths as $path) { $glob = $root . $path . '/*'; while ($entries = glob($glob)) { foreach ($entries as $entry) { if (is_file($entry)) { $extension = pathinfo($entry, PATHINFO_EXTENSION); if (in_array($extension, $types)) { $ret[] = realpath($entry); } } } $glob .= '/*'; } } return $ret; }
/** * 得到待编译的文件 * @param $params */ public static function getFileList($params) { $paths = $params[1]; $types = $params[2]; $ret = $params[3]; $ret->return = array(); // 若为sprite合图文件 $spritePath = C('IMERGE_PATH') . '/' . C('IMERGE_SPRITE_DIR'); if ($paths === $spritePath) { $files = self::getChangeMergeImage(); $files = array_merge($files[self::MODIFY], $files[self::ADD]); foreach ($files as $file) { $file = $spritePath . '/' . $file; array_push($ret->return, $file); } } else { $files = array_merge(self::$files[self::MODIFY], self::$files[self::ADD]); if (!empty($files)) { $paths = comma_str_to_array($paths); $types = comma_str_to_array($types); foreach ($paths as $path) { $len = strlen($path); foreach ($files as $file) { if (substr($file, 0, $len) === $path && in_array(pathinfo($file, PATHINFO_EXTENSION), $types)) { $file = C('SRC.SRC_PATH') . $file; array_push($ret->return, $file); } } } } } }