View source
<?php
function xautoload_boot() {
spl_autoload_register('_xautoload_autoload');
}
function _xautoload_autoload($name) {
$file = _xautoload_finder()
->findFile($name);
if ($file) {
require_once $file;
}
}
function _xautoload_finder() {
static $_finder;
if (!isset($_finder)) {
$modules = module_list();
foreach ($modules as $module => $m) {
$path = drupal_get_path('module', $module) . '/lib/';
if (is_dir($path)) {
$modules[$module] = $path;
}
}
$_finder = new xautoload_ClassFinder($modules);
}
return $_finder;
}
class xautoload_ClassFinder {
protected $modules = array();
function __construct($modules) {
$this->modules = $modules;
}
function findFile($class) {
if (preg_match('/^([a-z0-9_]+)_([A-Z].*)$/', $class, $m)) {
list(, $module, $name) = $m;
if (isset($this->modules[$module])) {
$path = strtr($name, '_', '/');
$path = $this->modules[$module] . $path . '.inc';
if (file_exists($path)) {
return $path;
}
}
}
}
}
function xautoload_autoload_info() {
$modules = array();
$query = db_query("SELECT * FROM {system} WHERE type = 'module'");
$locations = array();
$scanner = new xautoload_DirScanner($locations);
while ($module = db_fetch_object($query)) {
$info = unserialize($module->info);
if (!empty($info['autoload'])) {
$dir = dirname($module->filename);
$scanner
->scan("{$dir}/lib", $module->name);
}
}
return $locations;
}
class xautoload_DirScanner {
protected $locations;
function __construct(array &$locations) {
$this->locations =& $locations;
}
function scan($dir, $prefix) {
foreach (scandir($dir) as $candidate) {
if ($candidate == '.' || $candidate == '..') {
continue;
}
$path = $dir . '/' . $candidate;
if (preg_match('#^(.+)\\.inc$#', $candidate, $m)) {
if (is_file($path)) {
$name = $prefix . '_' . $m[1];
$this->locations[$name] = $path;
}
}
elseif (preg_match('#^(.+)$#', $candidate, $m)) {
if (is_dir($path)) {
$this
->scan($path, $prefix . '_' . $candidate);
}
}
}
}
}