function magic_generate_path_regex in Magic 7
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_generate_path_regex()
- magic_extension_assets_theme_settings_form_submit in ./
magic.module - Submit handler for the theme settings form.
File
- includes/
magic.inc, line 94 - A file to contain functions for the magic module to abuse.
Code
function magic_generate_path_regex($paths) {
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 module, profile or theme engine we assume that the we are
// trying to target an external file.
list($namespace) = explode('/', $item);
// Check if the namespace refers to a file residing in the 'misc' folder.
if ($namespace != '*') {
// Otherwise, 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)) {
$prefix = dirname($filename);
$item = substr_replace($item, $prefix, 0, strlen($namespace));
break;
}
}
}
// Escape any regex characters and turn asterisk wildcards into actual regex
// wildcards.
$item = preg_quote($item, '/');
$item = str_replace('\\*', '(.*)', $item);
}
return empty($paths) ? FALSE : '/^(' . implode('|', $paths) . ')$/';
}