Aggregate and cache content assets to improve performance

pull/17/head
Frode Petterson 2016-01-12 18:09:31 +01:00
parent f4ba0af1ef
commit b4fd0e098c
1 changed files with 39 additions and 0 deletions

View File

@ -1909,10 +1909,24 @@ class H5PCore {
* @return array files. * @return array files.
*/ */
public function getDependenciesFiles($dependencies, $prefix = '') { public function getDependenciesFiles($dependencies, $prefix = '') {
$aggregateAssets = TRUE;
if ($aggregateAssets) {
// Get aggregated files for assets
$key = self::getDependenciesHash($dependencies);
$files = $this->fs->getCachedAssets($key);
if ($files) {
return $files; // Using cached assets
}
}
// Build files list for assets
$files = array( $files = array(
'scripts' => array(), 'scripts' => array(),
'styles' => array() 'styles' => array()
); );
// Using content dependencies
foreach ($dependencies as $dependency) { foreach ($dependencies as $dependency) {
if (isset($dependency['path']) === FALSE) { if (isset($dependency['path']) === FALSE) {
$dependency['path'] = 'libraries/' . H5PCore::libraryToString($dependency, TRUE); $dependency['path'] = 'libraries/' . H5PCore::libraryToString($dependency, TRUE);
@ -1923,9 +1937,34 @@ class H5PCore {
$this->getDependencyAssets($dependency, 'preloadedJs', $files['scripts'], $prefix); $this->getDependencyAssets($dependency, 'preloadedJs', $files['scripts'], $prefix);
$this->getDependencyAssets($dependency, 'preloadedCss', $files['styles'], $prefix); $this->getDependencyAssets($dependency, 'preloadedCss', $files['styles'], $prefix);
} }
if ($aggregateAssets) {
// Aggregate and store assets
$this->fs->cacheAssets($files, $key);
// TODO: Update cache table
}
return $files; return $files;
} }
private static function getDependenciesHash(&$dependencies) {
// Build hash of dependencies
$toHash = array();
// Use unique identifier for each library version
for ($i = 0, $s = count($dependencies); $i < $s; $i++) {
$dep = $dependencies[$i];
$toHash[] = "{$dep['machineName']}-{$dep['majorVersion']}.{$dep['minorVersion']}.{$dep['patchVersion']}";
}
// Sort in case the same dependencies comes in a different order
sort($toHash);
// Calculate hash sum
return hash('sha1', implode('', $toHash));
}
/** /**
* Load library semantics. * Load library semantics.
* *