From 08f8c465440765ea858c85bad4a9bbec34bb6e12 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 9 May 2014 10:48:05 +0200 Subject: [PATCH] Changed getDependenciesFiles format to make it easier to use on all systems. --- h5p.classes.php | 60 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 7c61655..088d4c2 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -1338,13 +1338,55 @@ class H5PCore { return $dependencies; } + /** + * Get all dependency assets of the given type + * + * @param array $dependency + * @param string $type + * @param array $assets + */ + private function getDependencyAssets($dependency, $type, &$assets) { + // Check if dependency has any files of this type + if (empty($dependency[$type]) || $dependency[$type][0] === '') { + return; + } + + // Check if we should skip CSS. + if ($type === 'preloadedCss' && (isset($dependency['dropCss']) && $dependency['dropCss'] === '1')) { + return; + } + + foreach ($dependency[$type] as $file) { + $assets[] = (object) array( + 'path' => $dependency['path'] . '/' . trim(is_array($file) ? $file['path'] : $file), + 'version' => $dependency['version'] + ); + } + } + + /** + * Combines path with cache buster / version. + * + * @param array $assets + * @return array + */ + public function getAssetsUrls($assets) { + $urls = array(); + + foreach ($assets as $asset) { + $urls[] = $asset->path . $asset->version; + } + + return $urls; + } + /** * Return file paths for all dependecies files. * * @param array $dependencies * @return array files. */ - public function getDependenciesFiles($dependencies, $addCacheBuster = TRUE) { + public function getDependenciesFiles($dependencies) { $files = array( 'scripts' => array(), 'styles' => array() @@ -1356,19 +1398,9 @@ class H5PCore { $dependency['preloadedCss'] = explode(',', $dependency['preloadedCss']); } - $version = $addCacheBuster ? "?ver={$dependency['majorVersion']}.{$dependency['minorVersion']}.{$dependency['patchVersion']}" : ''; - if (!empty($dependency['preloadedJs']) && $dependency['preloadedJs'][0] !== '') { - foreach ($dependency['preloadedJs'] as $file) { - $file = trim(is_array($file) ? $file['path'] : $file) . $version; - $files['scripts'][] = $dependency['path'] . '/' . $file; - } - } - if ((!isset($dependency['dropCss']) || $dependency['dropCss'] !== '1') && !empty($dependency['preloadedCss']) && $dependency['preloadedCss'][0] !== '') { - foreach ($dependency['preloadedCss'] as $file) { - $file = trim(is_array($file) ? $file['path'] : $file) . $version; - $files['styles'][] = $dependency['path'] . '/' . $file; - } - } + $dependency['version'] = "?ver={$dependency['majorVersion']}.{$dependency['minorVersion']}.{$dependency['patchVersion']}"; + $this->getDependencyAssets($dependency, 'preloadedJs', $files['scripts']); + $this->getDependencyAssets($dependency, 'preloadedCss', $files['styles']); } return $files; }