Skip to content

Instantly share code, notes, and snippets.

@emanuele45
Created April 10, 2014 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emanuele45/10376905 to your computer and use it in GitHub Desktop.
Save emanuele45/10376905 to your computer and use it in GitHub Desktop.
memo variants fallback (wrong)
26f9192d6547d253156fa230d03ffcd652152475
sources/Load.php | 53 +++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 39 insertions(+), 14 deletions(-)
diff --git a/sources/Load.php b/sources/Load.php
index c06e1b8..c4d7e2a 100644
--- a/sources/Load.php
+++ b/sources/Load.php
@@ -1994,11 +1994,17 @@ function loadJavascriptFile($filenames, $params = array(), $id = '')
* the closing <html> tag
* - ['fallback'] (true/false): if true will attempt to load the file from the
* default theme if not found in the current this is the default behavior
- * if this is not supplied
+ * if this is not supplied.
+ * Ignored if variant is true.
* - ['async'] (true/false): if the script should be loaded asynchronously (HTML5)
* - ['stale'] (true/false/string): if true or null, use cache stale, false do
* not, or used a supplied string
+ * - ['variant'] (true/false): if true will attempt to load the file
+ * from the theme variant falling back to the default theme if not available
* @param string $id = '' optional id to use in html id=""
+ *
+ * @todo separate adding from loading because variants may not be set yet at
+ * this point
*/
function loadAssetFile($filenames, $params = array(), $id = '')
{
@@ -2010,23 +2016,42 @@ function loadAssetFile($filenames, $params = array(), $id = '')
if (!is_array($filenames))
$filenames = array($filenames);
+ if (empty($context[$params['index_name']]))
+ $context[$params['index_name']] = array();
+
// Static values for all these settings
if (!isset($params['stale']) || $params['stale'] === true)
$staler_string = CACHE_STALE;
elseif (is_string($params['stale']))
- $staler_string = ($params['stale'][0] === '?' ? $params['stale'] : '?' . $params['stale'])
+ $staler_string = $params['stale'][0] === '?' ? $params['stale'] : '?' . $params['stale'];
else
$staler_string = '';
- $fallback = (!empty($params['fallback']) && ($params['fallback'] === false)) ? false : true;
- $dir = '/' . $params['subdir'] . '/';
+ if (empty($context['theme_variant']) || !isset($params['variant']) || ((bool) $params['variant'] === false))
+ {
+ $variant_dir = '';
+ $variant_url = '';
+ }
+ else
+ {
+ $variant_dir = '/' . $context['theme_variant'];
+ $variant_url = $context['theme_variant_url'];
+ }
+
+ // Always ignore fallback if using a variant
+ if (isset($params['variant']) && ((bool) $params['variant'] === true))
+ $fallback = false;
+ // Of course no fallback if it is unwanted
+ elseif (isset($params['fallback']) && ((bool) $params['fallback'] === false))
+ $fallback = false;
+ // In any other case: fallback to default theme
+ else
+ $fallback = true;
// Whoa ... we've done this before yes?
- $cache_name = 'load_' . $params['extension'] . '_' . md5($settings['theme_dir'] . implode('_', $filenames));
+ $cache_name = 'load_' . $params['extension'] . '_' . md5($variant_dir . $settings['theme_dir'] . implode('_', $filenames));
if (($temp = cache_get_data($cache_name, 600)) !== null)
{
- if (empty($context[$params['index_name']]))
- $context[$params['index_name']] = array();
$context[$params['index_name']] += $temp;
}
else
@@ -2055,24 +2080,24 @@ function loadAssetFile($filenames, $params = array(), $id = '')
if (!empty($params['local']) || (substr($filename, 0, 4) !== 'http' && substr($filename, 0, 2) !== '//'))
{
$params['local'] = true;
- $params['dir'] = $settings['theme_dir'] . $dir;
- $params['url'] = $settings['theme_url'];
+ $params['dir'] = $settings['theme_dir'] . '/' . $params['subdir'] . $variant_dir . '/';
+ $params['url'] = $settings['theme_url'] . $variant_url;
// Fallback if we are not already in the default theme
- if ($fallback && ($settings['theme_dir'] !== $settings['default_theme_dir']) && !file_exists($settings['theme_dir'] . $dir . $filename))
+ if ($fallback && ($settings['theme_dir'] !== $settings['default_theme_dir']) && !file_exists($settings['theme_dir'] . '/' . $params['subdir'] . $variant_dir . '/' . $filename))
{
// Can't find it in this theme, how about the default?
- if (file_exists($settings['default_theme_dir'] . $dir . $filename))
+ if (file_exists($settings['default_theme_dir'] . '/' . $params['subdir'] . $filename))
{
- $filename = $settings['default_theme_url'] . $dir . $filename . $cache_staler;
- $params['dir'] = $settings['default_theme_dir'] . $dir;
+ $filename = $settings['default_theme_url'] . '/' . $params['subdir'] . $filename . $cache_staler;
+ $params['dir'] = $settings['default_theme_dir'] . '/' . $params['subdir'];
$params['url'] = $settings['default_theme_url'];
}
else
$filename = false;
}
else
- $filename = $settings['theme_url'] . $dir . $filename . $cache_staler;
+ $filename = $settings['theme_url'] . '/' . $params['subdir'] . $variant_dir . '/' . $filename . $cache_staler;
}
// Add it to the array for use in the template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment