/** * 处理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); } }
public function run($params) { if (!C('JCSS.IS_JCSS')) { off('processor_init', 'JCssParser'); return; } $tool = $params[1]; // 第1个参数是PreprocessTool对象 $options = $params[2]; // 第2个参数是配置信息 $processor = $params[3]; // 第3个参数是返回,将最后的预处理器对象赋给$processor->return // 判断当前是否在初始化CssPreprocess if ($options['processor'] === 'css') { $processor->return = new JCssPreprocess($tool->getMap(), $options); Preprocess::setInstance('CssPreprocess', $processor->return); } }
public function __construct($map, $options = null, $isMergeImage = null, $isReplaceUri = null) { parent::__construct($map); $this->isMergeImage = is_null($isMergeImage) ? C('IS_MERGE_IMAGE') : $isMergeImage; $this->isReplaceUri = is_null($isReplaceUri) ? C('IS_REPLACE_URI') : $isReplaceUri; if (!self::$cacheDir) { self::$cacheDir = C('CSS_CACHE_PATH') . '/' . str_random(); } // 如果需要合图,则导入合图配置 if ($this->isMergeImage) { if (!file_exists(C('IMERGE_PATH'))) { mark('您已打开合图功能,但没有生成合图配置,请先进行合图,或者关闭合图功能', 'error'); $this->isMergeImage = false; } else { $loader = new MergeConfigLoader(C('IMERGE_PATH')); $this->spriteConfig = $loader->getSpriteConfig(); } } }
public function run($params) { $item = $params[2]; if ($this->options['requirejs.path'] && $item['processor'] === 'js') { mark('requireJs插件开始处理' . $this->options['requirejs.path'], 'emphasize'); $this->options['requirejs.path'] = C('SRC.SRC_PATH') . $this->options['requirejs.path']; if (!file_exists($this->options['requirejs.path'])) { mark('"' . $this->options['requirejs.path'] . '"不存在,请检查m3d.php中requireJs配置', 'error'); return; } $tool = $params[1]; $map = $this->getMap($tool); $script = $this->genScript($map); $processor = Preprocess::getInstance('js'); $mapFiles = $this->options['requirejs.path']; if (is_string($mapFiles)) { $mapFiles = array($mapFiles); } foreach ($mapFiles as $file) { $processor->setFile($file); $processor->setContents($script); $processor->process(); $processor->compress(); $path = $processor->getRelativePath(); $buildPath = $tool->writeBuildFile($processor, $item, $path); $oldBuildPath = $tool->getMap('js', $path); if ($buildPath !== $oldBuildPath) { $tool->updateMap('js', $path, $buildPath); // 清除文件 $file = C('SRC.BUILD_PATH') . $oldBuildPath; if (file_exists($file)) { unlink($file); } $file = C('SRC.BUILD_CACHE_PATH') . $oldBuildPath; if (file_exists($file)) { unlink($file); } trigger('change_file', $path); } } } }
/** * 处理js地址 * @param simple_html_dom $html */ private function handleJs(simple_html_dom $html) { $lDelimiter = C('SRC.SMARTY_LEFT_DELIMITER'); $rDelimiter = C('SRC.SMARTY_RIGHT_DELIMITER'); foreach ($html->find('script') as $value) { if (!empty($value->src)) { $this->replacePath('js', $value, 'src'); } else { // 处理内联js if (isset($value->_xcompress) && $value->_xcompress === 'true') { $processor = Preprocess::getInstance('js'); $processor->setFile($this->path); $processor->setContents($value->innertext); $processor->process(); $processor->compress(); $text = $lDelimiter . 'literal' . $rDelimiter; $text .= $processor->getContents(); $text .= $lDelimiter . '/literal' . $rDelimiter; $value->innertext = $text; $value->_xcompress = null; } } } }
/** * 合图文件处理 * @param $processor * @param $item */ private function processSprite(Preprocess $processor, $item) { // 确保大图最新 $imergeTool = new InstantmergeTool($this->options, false); $imergeTool->updateSprite(); // 如果处理图片类,则需要扫描合图目录 $spriteList = $this->getFileList(C('IMERGE_PATH') . '/' . C('IMERGE_SPRITE_DIR'), $item['type']); foreach ($spriteList as $file) { mark('处理文件:' . $file); $processor->setFile($file); $processor->process(); $processor->compress(true); $path = str_replace(C('SRC.ROOT'), '', $file); $buildPath = $this->writeBuildFile($processor, $item, $path); // 更新map, 合图文件仅使用文件名作为key $filename = $processor->getFilename(); $this->map[$item['name']][$filename] = $buildPath; } }