[FRAMEWORK][AUTOLOAD] Fix autoloads

This commit is contained in:
Miguel Dantas 2019-08-23 23:25:36 +01:00 committed by Diogo Cordeiro
parent b41f9620fa
commit 6acc75ccff

View File

@ -121,19 +121,33 @@ function _have_config()
function GNUsocial_class_autoload($cls) function GNUsocial_class_autoload($cls)
{ {
if (file_exists(INSTALLDIR.'/classes/' . $cls . '.php')) { if (mb_substr($cls, -6) == 'Action' &&
require_once(INSTALLDIR.'/classes/' . $cls . '.php'); file_exists(($file = INSTALLDIR . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'))) {
} elseif (file_exists(INSTALLDIR . '/lib/modules/' . $cls . '.php')) { require_once $file;
require_once INSTALLDIR . '/lib/modules/' . $cls . '.php'; }
} else if (file_exists(INSTALLDIR.'/lib/' . strtolower($cls) . '.php')) {
require_once(INSTALLDIR.'/lib/' . strtolower($cls) . '.php'); $lib_path = INSTALLDIR . '/lib/';
} else if (mb_substr($cls, -6) == 'Action' && $lib_dirs = array_map(function ($dir) {
file_exists(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php')) { return '/lib/' . $dir . '/';
require_once(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'); },
} else if ($cls === 'OAuthRequest' || $cls === 'OAuthException') { array_filter(scandir($lib_path),
require_once('OAuth.php'); function ($dir) use ($lib_path) {
} else { // Filter out files and both hidden and implicit folders
Event::handle('Autoload', array(&$cls)); return $dir[0] != '.' && is_dir($lib_path . $dir);
}));
$found = false;
foreach (array_merge(['/classes/'], $lib_dirs) as $dir) {
$file = (in_array($dir, ['/classes/', '/lib/modules/'])) ? $cls : strtolower($cls);
$inc = INSTALLDIR . $dir . $file . '.php';
if (file_exists($inc)) {
$found = (require_once $inc);
break;
}
}
if (!$found) {
Event::handle('Autoload', [&$cls]);
} }
} }
@ -149,7 +163,11 @@ spl_autoload_register('GNUsocial_class_autoload');
* and is available here: http://www.php-fig.org/psr/psr-0/ * and is available here: http://www.php-fig.org/psr/psr-0/
*/ */
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
if ($class === 'OAuthRequest' || $class === 'OAuthException') {
$class_base = 'OAuth.php';
} else {
$class_base = preg_replace('{\\\\|_(?!.*\\\\)}', DIRECTORY_SEPARATOR, ltrim($class, '\\')); $class_base = preg_replace('{\\\\|_(?!.*\\\\)}', DIRECTORY_SEPARATOR, ltrim($class, '\\'));
}
$file = INSTALLDIR . "/extlib/{$class_base}.php"; $file = INSTALLDIR . "/extlib/{$class_base}.php";
if (file_exists($file)) { if (file_exists($file)) {
require_once $file; require_once $file;