function magic_assets_prepare_regex in Magic 7.2
Helper function for generating a regex from a list of paths.
Generates a single regex from a list of file paths that can be used to match JS or CSS files using preg_grep() for example in hook_css_alter() or hook_js_alter(). The '*' (asterisk) character can be used as a wild-card.
Parameters
$paths: An array of file paths.
Return value
string The generated regex.
See also
1 call to magic_assets_prepare_regex()
- magic_assets_regex_steps in includes/
magic.assets.inc - Groups include/exclude patterns together.
File
- includes/
magic.assets.inc, line 24 - A file to contain functions for the magic module to abuse.
Code
function magic_assets_prepare_regex($paths) {
$profile = drupal_get_profile();
$site = preg_quote(conf_path(), '/');
$themes = array();
foreach ($GLOBALS['base_theme_info'] as $info) {
$themes[] = preg_quote(dirname($info->filename), '/');
}
$tokens = array(
':all' => '.+',
':core' => '(?:misc|modules|themes)\\/.+',
':contrib' => "(?:sites\\/all\\/modules|{$site}\\/modules|profiles\\/{$profile}\\/modules)\\/.+",
':current-theme' => preg_quote(drupal_get_path('theme', $GLOBALS['theme_key']), '/') . "\\/.+",
':base-theme' => $themes ? '(?:' . implode('|', $themes) . ')\\/.+' : FALSE,
);
foreach ($paths as &$item) {
// The first segment (everything before the first slash) is the namespace.
// This rule only applies to local files... So if the namespace can not be
// mapped to a token, module, profile or theme engine we assume that we are
// trying to target an external file.
list($namespace) = explode('/', $item, 2);
// Process token namespaces.
if (isset($tokens[$namespace]) && empty($tokens[$namespace])) {
unset($item);
continue;
}
elseif ($namespace != '*' && !isset($tokens[$namespace])) {
// Check if it refers to a theme, module, profile or theme engine.
foreach (array(
'theme',
'module',
'profile',
'theme_engine',
) as $type) {
// We can't use drupal_get_path() directly because that uses dirname()
// internally which returns '.' if no filename was found.
if ($filename = drupal_get_filename($type, $namespace, NULL, FALSE)) {
$directory = preg_quote(dirname($filename), '/');
// Now that we know about this namespace we can add it to the tokens
// array for performance reasons.
$tokens[$namespace] = $directory;
break;
}
}
}
// Escape any regex characters and replace tokens and wildcards.
$item = isset($tokens[$namespace]) ? substr($item, strlen($namespace)) : $item;
$item = preg_quote($item, '/');
$item = str_replace('\\*', '.*', $item);
$item = isset($tokens[$namespace]) ? $tokens[$namespace] . $item : $item;
}
return empty($paths) ? FALSE : '/^' . implode('|', $paths) . '$/';
}