@@ -58,84 +58,39 @@ abstract class Module extends \LORIS\Router\PrefixRouter
5858
5959 $ rv = [];
6060 foreach ($ mnames as $ row ) {
61- $ rv [$ row ['ID ' ]] = self :: factory ( $ loris, $ row ['Name ' ]);
61+ $ rv [$ row ['ID ' ]] = $ loris-> getModule ( $ row ['Name ' ]);
6262 }
6363 return $ rv ;
6464 }
6565
66-
6766 /**
68- * Returns a Module instance for the module named $name. The returned
69- * value may be a subtype of the base LORIS module class and override
70- * it's method.
71- *
72- * This also sets up PHP class autoloading for the module that is loaded,
73- * such that files in the module/php directory can be autoloaded.
74- *
75- * @param \Loris\LorisInstance $loris The LORIS instance containing the module
76- * @param string $name The module name we'd like information about
77- *
78- * @throws \LorisNoSuchModuleException
79- * @throws \LorisModuleMissingException
80- * @throws \NotFound
67+ * Register an autoloader for this module's namespace.
8168 *
82- * @return \Module object
69+ * @return void
8370 */
84- static public function factory (
85- \Loris \LorisInstance $ loris ,
86- string $ name
87- ) : \Module {
88- $ factory = NDB_Factory::singleton ();
89- $ config = $ factory ->config ();
90- $ base = $ config ->getSetting ("base " );
91-
92- // TODO: Module search path should be a config option.
93- if (is_dir ($ base . "project/modules/ $ name " )) {
94- $ mpath = $ base . "project/modules/ $ name " ;
95- } else if (is_dir ($ base . "modules/ $ name " )) {
96- $ mpath = $ base . "modules/ $ name " ;
97- } else {
98- throw new LorisNoSuchModuleException ("No such module: $ name " );
99- }
100-
101- if (!file_exists ($ mpath . "/php/module.class.inc " )) {
102- // Check if there's a module class to see if the module has been
103- // updated. This can be removed once support for old Modules has
104- // been removed. If it hasn't been updated, fall back on the old
105- // style.
106- throw new LorisModuleMissingException ("$ name is missing Module class " );
107- }
71+ public function registerAutoloader () : void
72+ {
10873 // Manually do dependency injection for the module's php/ directory,
10974 // since composer doesn't know anything about our modules and we only
11075 // want *this* module's classes autoloaded anyways.
11176 spl_autoload_register (
112- function ($ class ) use ($ name ,
113- $ mpath
114- ) {
115- if (strpos ($ class , "LORIS \\$ name \\" ) === 0 ) {
116- $ fpath = $ mpath . "/php/ "
117- . strtolower (substr ($ class , strlen ("LORIS \\$ name \\" )))
77+ function ($ class ) {
78+ if (strpos ($ class , "LORIS \\{$ this ->name }\\" ) === 0 ) {
79+ $ fpath = $ this ->dir . "/php/ "
80+ . strtolower (substr ($ class , strlen ("LORIS \\{$ this ->name }\\" )))
11881 . ".class.inc " ;
11982 $ fpath = str_replace ('\\' , '/ ' , $ fpath );
12083 if (!file_exists ($ fpath )) {
12184 throw new \NotFound (
122- "Could not load module ` $ name`: file ` $ fpath` " .
85+ "Could not load module ` { $ this -> name } `: file ` $ fpath` " .
12386 "does not exist "
12487 );
12588 }
12689 include $ fpath ;
12790 }
12891 }
12992 );
130-
131- // Manually do the require for the module descriptor because of
132- // namespacing
133- //require_once $mpath . "/php/Module.class.inc";
134- $ className = "\LORIS \\$ name\Module " ;
135- $ cls = new $ className ($ loris , $ name , $ mpath );
136- return $ cls ;
13793 }
138-
13994 /**
14095 * Creates a new module instance
14196 *
0 commit comments