/** * @return AssetInfo */ public function getAssetInfo() { $assetInfo = new AssetInfo(); $assetInfo->setPath($this->getSrc()); $assetInfo->setPriority($this->getPriority()); $assetInfo->setAllowCompression($this->getCompression()); $assetInfo->setContentType($this->getType()); $assetInfo->setContent($this->getContent()); return $assetInfo; }
public function hasAsset(AssetInfo $assetInfo, $assets = null) { if (!$assets) { $assets = $this->assetsInfo; } $assets = $assets ? call_user_func_array('array_merge', $assets) : []; foreach ($assets as $asset) { if (!$assetInfo->getPath() && $assetInfo->getContent() === $asset->getContent() && $assetInfo->getContentType() === $asset->getContentType()) { return true; } if ($asset->getPath() && $asset->getPath() === $assetInfo->getPath()) { return true; } } return false; }
/** * @param AssetInfo $assetInfo * * @return AssetInfo[] */ public function compileAsset(AssetInfo $assetInfo) { $compiler = $this->getCompileHandlerByContentType($assetInfo->getContentType()); if (!$compiler) { $compiler = $this->getCompileHandlerByFileExtension($assetInfo->getPath()); } if (!$compiler) { return [$assetInfo]; //no compiler found, so ok } if ($compiledAssetInfoResult = $compiler->compileFile($assetInfo)) { if (is_array($compiledAssetInfoResult)) { return $compiledAssetInfoResult; } else { if ($compiledAssetInfoResult instanceof AssetInfo) { return [$compiledAssetInfoResult]; } } } return []; }
/** * @param array $files * * @return AssetInfo */ public function compressFiles(array $files) { $md5String = ''; foreach ($files as $file) { $path = $this->getAssetPath($file); $md5String .= '.' . filemtime($path); } $fileUpToDate = false; $md5Line = '/* ' . md5($md5String) . " */\n"; $oFile = 'cache/compressed-css/' . md5($md5String) . '.css'; $handle = @fopen($this->getJarves()->getRootDir() . '/../web/' . $oFile, 'r'); if ($handle) { $line = fgets($handle); fclose($handle); if ($line == $md5Line) { $fileUpToDate = true; } } if (!$fileUpToDate) { $content = $this->utils->compressCss($files, 'cache/compressed-css/'); $content = $md5Line . $content; $this->webFilesystem->write($oFile, $content); } $assetInfo = new AssetInfo(); $assetInfo->setPath($oFile); return $assetInfo; }
protected function getTag(AssetInfo $assetInfo) { if ($assetInfo->getPath()) { $path = $this->getAssetPath($assetInfo->getPath()); $pubPath = $this->getPublicAssetPath($assetInfo->getPath()); if (file_exists($path)) { $pubPath .= '?c=' . substr(md5(filemtime($path)), 0, 6); } return sprintf('<script type="module" src="%s"></script>', $pubPath); } else { return sprintf(<<<EOF <script type="module"> %s </script> EOF , $assetInfo->getContent()); } }
/** * @param array $files * * @return AssetInfo */ public function compressFiles(array $files) { $md5String = ''; foreach ($files as $file) { $path = $this->getAssetPath($file); $md5String .= '.' . filemtime($path); } $fileUpToDate = false; $md5Line = '/* ' . md5($md5String) . " */\n"; $oFile = 'cache/compressed-js/' . md5($md5String) . '.js'; $handle = @fopen($this->getJarves()->getRootDir() . '/../web/' . $oFile, 'r'); if ($handle) { $line = fgets($handle); fclose($handle); if ($line == $md5Line) { $fileUpToDate = true; } } if (!$fileUpToDate) { // googles minifier // $result = $this->getJarves()->getUtils()->compressJs( // $files, // $oFile // ); $content = ''; foreach ($files as $assetPath) { $content .= "\n/* {$assetPath} */\n\n"; $path = $this->getAssetPath($assetPath); $content .= file_get_contents($path); } $content = $md5Line . $content; $this->webFilesystem->write($oFile, $content); } $assetInfo = new AssetInfo(); $assetInfo->setPath($oFile); return $assetInfo; }
public function compileFile(AssetInfo $assetInfo) { $assetPath = $assetInfo->getPath(); $localPath = $this->getAssetPath($assetPath); if (!file_exists($localPath)) { return null; } $publicPath = $this->getPublicAssetPath($assetPath); $targetPath = 'cache/scss/' . substr($publicPath, 0, strrpos($publicPath, '.')); if ('.css' !== substr($targetPath, -4)) { $targetPath .= '.css'; } $needsCompilation = true; $sourceMTime = filemtime($localPath); $dir = dirname($localPath); if ($this->filesystem->has('web/' . $targetPath)) { $fh = $this->filesystem->handle('web/' . $targetPath); if ($fh) { $firstLine = fgets($fh); $info = substr($firstLine, strlen('/* compiled at '), -3); $spacePosition = strpos($info, ' '); $lastSourceMTime = 0; $dependencies = []; if ($spacePosition > 0) { $lastSourceMTime = (int) substr($info, 0, $spacePosition); $dependencies = trim(substr($info, $spacePosition + 1)); if ($dependencies) { $dependencies = explode(',', trim(substr($info, $spacePosition + 1))); } else { $dependencies = []; } } else { //old format without dependencies $lastSourceMTime = (int) $info; } $needsCompilation = $lastSourceMTime !== $sourceMTime; if (!$needsCompilation) { //check dependencies foreach ($dependencies as $dependency) { list($path, $depLastMTime) = explode(':', $dependency); $depLastMTime = (int) $depLastMTime; if (!file_exists($dir . '/' . $path)) { //depended file does not exist anymore, so we need to recompile $needsCompilation = true; break; } $depSourceMTime = filemtime($dir . '/' . $path); if ($depLastMTime !== $depSourceMTime) { $needsCompilation = true; break; } } } } } if ($needsCompilation) { //resolve all dependencies $dependencies = []; $this->resolveDependencies($localPath, $dependencies); $processBuilder = new ProcessBuilder(); $processBuilder->setInput(file_get_contents($localPath))->add('sass')->add('--scss')->add('--no-cache')->add('--unix-newlines')->add('--load-path')->add(dirname($localPath))->add($localPath)->enableOutput(); $process = $processBuilder->getProcess(); $process->start(); while ($process->isRunning()) { } if (127 === $process->getExitCode()) { throw new \RuntimeException('sass binary not found. Please install sass first and make its in $PATH. ' . $process->getExitCodeText()); } if (0 !== $process->getExitCode()) { throw new \RuntimeException(sprintf("Error during scss compilation of %s:\n%s\n%s\n%s", $assetPath, $process->getExitCodeText(), $process->getErrorOutput(), $process->getOutput())); } $compiled = $process->getOutput(); $compiled = $this->replaceRelativePaths($publicPath, $targetPath, $compiled); $dependencies = implode(',', $dependencies); $info = "{$sourceMTime} {$dependencies}"; $compiled = "/* compiled at {$info} */\n" . $compiled; $this->filesystem->write('web/' . $targetPath, $compiled); } $assetInfo = new AssetInfo(); $assetInfo->setPath($targetPath); $assetInfo->setContentType('text/css'); return $assetInfo; }