self::$versionHash = md5('not installed');
}
- $useAssetPipeline = self::isAssetPipelineEnabled();
- if ($useAssetPipeline) {
+ // Add the js files
+ $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
+ $this->assign('jsfiles', array());
+ if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
$this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
- $this->generateAssets();
- } else {
- // Add the js files
- $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
- $this->assign('jsfiles', array());
- if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
- $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
- }
- foreach($jsFiles as $info) {
- $web = $info[1];
- $file = $info[2];
- $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
- }
+ }
+ foreach($jsFiles as $info) {
+ $web = $info[1];
+ $file = $info[2];
+ $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
+ }
- // Add the css files
- $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
- $this->assign('cssfiles', array());
- $this->assign('printcssfiles', []);
- foreach($cssFiles as $info) {
- $web = $info[1];
- $file = $info[2];
+ // Add the css files
+ $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
+ $this->assign('cssfiles', array());
+ $this->assign('printcssfiles', []);
+ foreach($cssFiles as $info) {
+ $web = $info[1];
+ $file = $info[2];
if (substr($file, -strlen('print.css')) === 'print.css') {
- $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
- } else {
- $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
- }
+ $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
+ } else {
+ $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
}
}
}
return $locator->getResources();
}
- public function generateAssets() {
- $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
- $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
- $jsHash = self::hashFileNames($jsFiles);
-
- if (!file_exists("$assetDir/assets/$jsHash.js")) {
- $jsFiles = array_map(function ($item) {
- $root = $item[0];
- $file = $item[2];
- // no need to minifiy minified files
- if (substr($file, -strlen('.min.js')) === '.min.js') {
- return new FileAsset($root . '/' . $file, array(
- new SeparatorFilter(';')
- ), $root, $file);
- }
- return new FileAsset($root . '/' . $file, array(
- new JSqueezeFilter(),
- new SeparatorFilter(';')
- ), $root, $file);
- }, $jsFiles);
- $jsCollection = new AssetCollection($jsFiles);
- $jsCollection->setTargetPath("assets/$jsHash.js");
-
- $writer = new AssetWriter($assetDir);
- $writer->writeAsset($jsCollection);
- }
-
- $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
-
- // differentiate between screen stylesheets and printer stylesheets
- $screenCssFiles = array_filter($cssFiles, function($cssFile) {
- return substr_compare($cssFile[2], 'print.css', -strlen('print.css')) !== 0;
- });
- $screenCssAsset = $this->generateCssAsset($screenCssFiles);
-
- $printCssFiles = array_filter($cssFiles, function($cssFile) {
- return substr_compare($cssFile[2], 'print.css', -strlen('print.css')) === 0;
- });
- $printCssAsset = $this->generateCssAsset($printCssFiles);
-
- $this->append('jsfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$jsHash.js"));
- $this->append('cssfiles', $screenCssAsset);
- $this->append('printcssfiles', $printCssAsset);
- }
-
- /**
- * generates a single css asset file from an array of css files if at least one of them has changed
- * otherwise it just returns the path to the old asset file
- * @param $files
- * @return string
- */
- private function generateCssAsset($files) {
- $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
- $hash = self::hashFileNames($files);
-
- if (!file_exists("$assetDir/assets/$hash.css")) {
- $files = array_map(function ($item) {
- $root = $item[0];
- $file = $item[2];
- $assetPath = $root . '/' . $file;
- $sourceRoot = \OC::$SERVERROOT;
- $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
- return new FileAsset(
- $assetPath,
- array(
- new CssRewriteFilter(),
- new CssMinFilter(),
- new CssImportFilter()
- ),
- $sourceRoot,
- $sourcePath
- );
- }, $files);
-
- $cssCollection = new AssetCollection($files);
- $cssCollection->setTargetPath("assets/$hash.css");
-
- $writer = new AssetWriter($assetDir);
- $writer->writeAsset($cssCollection);
-
- }
-
- return \OC::$server->getURLGenerator()->linkTo('assets', "$hash.css");
- }
-
/**
* Converts the absolute file path to a relative path from \OC::$SERVERROOT
* @param string $filePath Absolute path
return $relativePath[1];
}
-
- /**
- * @param array $files
- * @return string
- */
-
- private static function hashFileNames($files) {
- foreach($files as $i => $file) {
- try {
- $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2];
- } catch (\Exception $e) {
- $files[$i] = $file[0].'/'.$file[2];
- }
- }
-
- sort($files);
- // include the apps' versions hash to invalidate the cached assets
- $files[] = self::$versionHash;
- return hash('md5', implode('', $files));
- }
}
}
return 'HTTP/1.1';
}
-
- /**
- * @return bool
- */
- public static function isAssetPipelineEnabled() {
- try {
- if (\OCP\Util::needUpgrade()) {
- // Don't use the compiled asset when we need to do an update
- return false;
- }
- } catch (\Exception $e) {
- // Catch any exception, because this code is also called when displaying
- // an exception error page.
- return false;
- }
-
- // asset management enabled?
- $config = \OC::$server->getConfig();
- $useAssetPipeline = $config->getSystemValue('asset-pipeline.enabled', false);
- if (!$useAssetPipeline) {
- return false;
- }
-
- // assets folder exists?
- $assetDir = $config->getSystemValue('assetdirectory', \OC::$SERVERROOT) . '/assets';
- if (!is_dir($assetDir)) {
- if (!mkdir($assetDir)) {
- \OCP\Util::writeLog('assets',
- "Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
- return false;
- }
- }
-
- // assets folder can be accessed?
- if (!touch($assetDir."/.oc")) {
- \OCP\Util::writeLog('assets',
- "Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
- return false;
- }
- return $useAssetPipeline;
- }
-
}