Change autoloader to support case-sensitive class files + fix lang path for namespaced modules

This commit is contained in:
Kijin Sung 2022-12-26 02:30:59 +09:00
parent 7218e4fac6
commit a07307d343
2 changed files with 36 additions and 23 deletions

View file

@ -802,19 +802,16 @@ class Context
* Load language file according to language type * Load language file according to language type
* *
* @param string $path Path of the language file * @param string $path Path of the language file
* @param string $plugin_name
* @return void * @return void
*/ */
public static function loadLang($path) public static function loadLang($path, $plugin_name = null)
{ {
if (preg_match('@/(modules|addons|plugins|widgets)/([a-zA-Z0-9_-]+)/lang/?(?:lang\.xml)?$@', str_replace('\\', '/', $path), $matches)) if ($plugin_name === null && preg_match('@/(modules|addons|plugins|widgets)/([a-zA-Z0-9_-]+)/lang/?(?:lang\.xml)?$@', str_replace('\\', '/', $path), $matches))
{ {
$path = \RX_BASEDIR . $matches[1] . '/' . $matches[2] . '/lang'; $path = \RX_BASEDIR . $matches[1] . '/' . $matches[2] . '/lang';
$plugin_name = $matches[2]; $plugin_name = $matches[2];
} }
else
{
$plugin_name = null;
}
if (!(($GLOBALS['lang'] ?? null) instanceof Rhymix\Framework\Lang)) if (!(($GLOBALS['lang'] ?? null) instanceof Rhymix\Framework\Lang))
{ {

View file

@ -102,39 +102,55 @@ $GLOBALS['RX_AUTOLOAD_FILE_MAP'] = array_change_key_case(array(
*/ */
spl_autoload_register(function($class_name) spl_autoload_register(function($class_name)
{ {
$filename = false; $class_name = str_replace('\\', '/', $class_name);
$langpath = false; $filename1 = null;
$lc_class_name = str_replace('\\', '/', strtolower($class_name)); $filename2 = null;
if (preg_match('!^rhymix/(framework|addons|modules|plugins)/(.+)$!', $lc_class_name, $matches)) $lang_plugin = null;
$lang_path = null;
// Try namespaced classes, legacy classes, and module classes.
if (preg_match('!^Rhymix/(Framework|Addons|Modules|Plugins|Themes|Widgets)/((\w+)/(?:\w+/)*)?(\w+)$!', $class_name, $matches))
{ {
$filename = RX_BASEDIR . ($matches[1] === 'framework' ? 'common/framework' : $matches[1]) . '/' . $matches[2] . '.php'; $dir = RX_BASEDIR . ($matches[1] === 'Framework' ? 'common/framework' : strtolower($matches[1])) . '/' . strtolower($matches[2]);
if ($matches[1] !== 'framework') $filename1 = $dir . $matches[4] . '.php';
$filename2 = $dir . strtolower($matches[4]) . '.php';
if ($matches[1] !== 'Framework' && !empty($matches[3]))
{ {
$langpath = RX_BASEDIR . $matches[1] . '/lang'; $lang_plugin = strtolower($matches[3]);
$lang_path = RX_BASEDIR . strtolower($matches[1]) . '/' . $lang_plugin . '/lang';
} }
} }
elseif (isset($GLOBALS['RX_AUTOLOAD_FILE_MAP'][$lc_class_name])) elseif (isset($GLOBALS['RX_AUTOLOAD_FILE_MAP'][$lc_class_name = strtolower($class_name)]))
{ {
$filename = RX_BASEDIR . $GLOBALS['RX_AUTOLOAD_FILE_MAP'][$lc_class_name]; $filename1 = RX_BASEDIR . $GLOBALS['RX_AUTOLOAD_FILE_MAP'][$lc_class_name];
} }
elseif (preg_match('/^([a-zA-Z0-9_]+?)(Admin)?(View|Controller|Model|Item|Api|Wap|Mobile)?$/', $class_name, $matches)) elseif (preg_match('/^([a-zA-Z0-9_]+?)(Admin)?(View|Controller|Model|Item|Api|Wap|Mobile)?$/', $class_name, $matches))
{ {
$module = strtolower($matches[1]); $module = strtolower($matches[1]);
$filename = RX_BASEDIR . 'modules/' . $module . '/' . $module . $filename1 = RX_BASEDIR . 'modules/' . $module . '/' . $module .
(!empty($matches[2]) ? '.admin' : '') . (!empty($matches[2]) ? '.admin' : '') .
(!empty($matches[3]) ? ('.' . strtolower($matches[3])) : '.class') . '.php'; (!empty($matches[3]) ? ('.' . strtolower($matches[3])) : '.class') . '.php';
if ($module !== 'module') if ($module !== 'module')
{ {
$langpath = RX_BASEDIR . 'modules/' . $module . '/lang'; $lang_plugin = $module;
$lang_path = RX_BASEDIR . 'modules/' . $module . '/lang';
} }
} }
if ($filename && file_exists($filename))
// Load the PHP file.
if ($filename1 && file_exists($filename1))
{ {
include $filename; include $filename1;
if ($langpath) }
{ elseif ($filename2 && file_exists($filename2))
Context::loadLang($langpath); {
} include $filename2;
}
// Load the lang file for the plugin.
if ($lang_plugin)
{
Context::loadLang($lang_path, $lang_plugin);
} }
}); });