View source
<?php
$autoload = array(
'paths' => autoload_paths(),
'extensions' => autoload_extensions(),
);
spl_autoload_register(function ($namespace) use ($autoload) {
list($base) = explode('\\', $namespace);
if (!empty($_SESSION['autoload_paths'])) {
$autoload['paths'] = array_merge($autoload['paths'], $_SESSION['autoload_paths']);
}
if (!empty($autoload['paths'][$base])) {
$relative = autoload_namespace_to_path($namespace);
foreach ($autoload['paths'][$base] as $path => $data) {
$_relative = $relative;
if ('' !== $data['psr-4']) {
$_relative = str_replace($data['psr-4'], '', $_relative);
}
foreach ($autoload['extensions'] as $extension) {
$file = "{$path}/{$_relative}{$extension}";
if (file_exists($file)) {
require_once $file;
return;
}
}
}
}
});
function autoload_entity_info() {
autoload_paths_recompute();
return array();
}
function autoload_module_implements_alter(array &$implementations, $hook) {
if ('entity_info' === $hook && isset($implementations['autoload'])) {
$implementations = array(
'autoload' => $implementations['autoload'],
) + $implementations;
}
}
function autoload_paths_recompute() {
drupal_static_reset('autoload_paths');
$_SESSION['autoload_paths'] = autoload_paths();
}
function autoload_paths() {
$processed =& drupal_static(__FUNCTION__, FALSE);
static $map = array();
if (!$processed) {
$processed = TRUE;
foreach (system_list('module_enabled') as $module_name => $data) {
if (!empty($data->info['autoload'])) {
$module_path = dirname($data->filename);
if (!is_array($data->info['autoload'])) {
$data->info['autoload'] = array_fill_keys(array(
"lib/Drupal/{$module_name}",
'src',
), array(
"Drupal\\{$module_name}",
));
}
foreach ($data->info['autoload'] as $subdirectory => $namespaces) {
if (!is_array($namespaces)) {
continue;
}
$path = rtrim("{$module_path}/{$subdirectory}", '/');
if (file_exists($path)) {
foreach ($namespaces as $namespace) {
$parts = explode('\\', $namespace);
$map[$parts[0]][$path] = array(
'name' => $module_name,
'path' => $module_path,
'psr-4' => count($parts) > 1 ? autoload_namespace_to_path($namespace) : '',
);
}
}
}
}
}
}
return $map;
}
function autoload_extensions() {
$extensions = array_filter(explode(',', spl_autoload_extensions()), 'trim');
$extensions[] = '.php';
$extensions[] = '.inc';
return array_unique($extensions);
}
function autoload_seek_classes($namespace_base, $subtype = NULL, $include_file_info = FALSE) {
static $cache;
if (NULL === $cache) {
$cache = cache_get("{$namespace_base}:{$subtype}");
}
if (FALSE === $cache || empty($cache->data)) {
$paths = autoload_paths();
$list = array();
if (isset($paths[$namespace_base])) {
$mask = implode('|', array_map('preg_quote', autoload_extensions()));
foreach ($paths[$namespace_base] as $path => $data) {
foreach (file_scan_directory($path, "/({$mask})\$/") as $uri => $file) {
$blockers = array(
$path,
'.' . pathinfo($uri, PATHINFO_EXTENSION),
);
$class = explode('/', trim(str_replace($blockers, '', $uri), '/'));
if ('' !== $data['psr-4']) {
array_unshift($class, rtrim($data['psr-4'], '/'));
}
elseif ($class[0] !== $namespace_base) {
array_unshift($class, $namespace_base);
}
$class = autoload_path_to_namespace(implode('/', $class));
if (class_exists($class)) {
$file->path = $path;
$file->module_name = $data['name'];
$file->module_path = $data['path'];
$list[$class] = $include_file_info ? $file : $class;
}
}
}
if (isset($subtype) && (class_exists($subtype) || interface_exists($subtype))) {
foreach ($list as $class => $item) {
if (!is_subclass_of($class, $subtype)) {
unset($list[$class]);
}
}
}
}
if (!empty($list)) {
cache_set("{$namespace_base}:{$subtype}", $list);
}
return $list;
}
return $cache->data;
}
function autoload_namespace_to_path($namespace) {
return str_replace('\\', '/', $namespace);
}
function autoload_path_to_namespace($path) {
return str_replace('/', '\\', $path);
}