From 30df49bbb363d47b4a3961bb3ec07cdecea7619b Mon Sep 17 00:00:00 2001 From: ewinslow Date: Thu, 14 Oct 2010 07:52:31 +0000 Subject: Fixes #2547: Plugins can now participate in autoloading classes. git-svn-id: http://code.elgg.org/elgg/trunk@7074 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/elgglib.php | 28 +++++++++++++++++++++++++--- engine/lib/plugins.php | 4 ++++ engine/start.php | 5 ++++- 3 files changed, 33 insertions(+), 4 deletions(-) (limited to 'engine') diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index 4376ca53f..5b224e0be 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -2200,8 +2200,30 @@ function js_page_handler($page) { } } -function __autoload($class) { - require_once dirname(dirname(__FILE__))."/classes/$class.php"; +function __elgg_autoload($class) { + global $CONFIG; + + if (!include($CONFIG->classes[$class])) { + throw new Exception("Failed to autoload $class"); + } +} + +function elgg_register_classes($dir) { + $classes = elgg_get_file_list($dir, array(), array(), array('.php')); + + foreach ($classes as $class) { + elgg_register_class(basename($class, '.php'), $class); + } +} + +function elgg_register_class($class, $location) { + global $CONFIG; + + if (!isset($CONFIG->classes)) { + $CONFIG->classes = array(); + } + + $CONFIG->classes[$class] = $location; } /** @@ -2233,7 +2255,7 @@ function __elgg_shutdown_hook() { */ function elgg_init() { global $CONFIG; - + register_action('comments/add'); register_action('comments/delete'); register_action('likes/add'); diff --git a/engine/lib/plugins.php b/engine/lib/plugins.php index a36f2bf6d..52f498bf7 100644 --- a/engine/lib/plugins.php +++ b/engine/lib/plugins.php @@ -191,6 +191,10 @@ function load_plugins() { if (is_dir($CONFIG->pluginspath . $mod . "/languages")) { register_translations($CONFIG->pluginspath . $mod . "/languages/"); } + + if (is_dir($CONFIG->pluginspath . "$mod/classes")) { + elgg_register_classes($CONFIG->pluginspath . "$mod/classes"); + } } } } diff --git a/engine/start.php b/engine/start.php index a84a19e0d..0bf652cb6 100644 --- a/engine/start.php +++ b/engine/start.php @@ -77,7 +77,6 @@ foreach ($required_files as $file) { set_error_handler('__elgg_php_error_handler'); set_exception_handler('__elgg_php_exception_handler'); - /** * Load the system settings */ @@ -113,6 +112,10 @@ foreach($lib_files as $file) { } } +// prep core classes to be autoloadable +spl_autoload_register('__elgg_autoload'); +elgg_register_classes(dirname(__FILE__).'/classes'); + // confirm that the installation completed successfully verify_installation(); -- cgit v1.2.3