Skip to content

Commit 1ec65d5

Browse files
authored
[core] Remove \Module::factory, update remaining instances to $loris->getModule() (#8287)
This finishes what was started in #8221 and eliminates the \Module::factory method. All remaining instances are updated to use $loris->getModule instead, which takes that LORISInstance module paths into consideration.
1 parent 3929f99 commit 1ec65d5

File tree

9 files changed

+42
-81
lines changed

9 files changed

+42
-81
lines changed

htdocs/AjaxHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@
8989

9090
$public = false;
9191
try {
92-
// Anything that's still in an ajax directory isn't using the lorisinstance
93-
// object, so for now just make a fake one to pass to the factory.
92+
// GetModule doesn't use the database or ndb_config, so for now
93+
// just pass a fake one to the constructor
9494
$loris = new \LORIS\LorisInstance(
9595
new \Database(),
9696
new \NDB_Config(),
97-
[]
97+
[__DIR__ . "/../modules", __DIR__ . "/../project/modules"]
9898
);
99-
$m = Module::factory($loris, $Module);
99+
$m = $loris->getModule($Module);
100100

101101
$public = $m->isPublicModule();
102102
} catch (LorisModuleMissingException $e) {

modules/candidate_list/php/candidate_list.class.inc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ class Candidate_List extends \DataFrameworkMenu
6969
*/
7070
function getFieldOptions() : array
7171
{
72-
// Relying on a side-effect of the the server process module to autoload
73-
// its namespace.
74-
\Module::factory($this->loris, 'candidate_parameters');
72+
$this->loris->getModule('candidate_parameters')->registerAutoloader();
7573

7674
// create user object
7775
$factory = \NDB_Factory::singleton();

modules/candidate_parameters/ajax/getData.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,9 @@ function getFamilyInfoFields()
302302
*/
303303
function getParticipantStatusFields()
304304
{
305-
// All we care about is the namespace loading, so we just need to ensure
306-
// that the directory path has this module in it
307-
$loris = new \LORIS\LorisInstance(
308-
new Database(),
309-
new NDB_Config(),
310-
[
311-
__DIR__ . '../'
312-
]
313-
);
305+
global $loris;
314306

315-
\Module::factory($loris, 'candidate_parameters');
307+
$loris->getModule('candidate_parameters')->registerAutoloader();
316308
$candID = new CandID($_GET['candID']);
317309

318310
$db = \NDB_Factory::singleton()->database();

modules/help_editor/ajax/help.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
$moduleName = $_REQUEST['testName'] ?? null;
2727
$subpageName = $_REQUEST['subtest'] ?? null;
28-
$m = Module::factory($loris, $moduleName);
28+
$m = $loris->getModule($moduleName);
2929
// Load help data. Try to load subpage first as its more specific and
3030
// will only be present some of the time. Fallback to the module name if
3131
// no subpage present.

php/libraries/Module.class.inc

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

src/LorisInstance.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,19 @@ public function hasModule(string $name) : bool
126126
*/
127127
public function getModule(string $name) : \Module
128128
{
129-
return \Module::factory($this, $name);
129+
foreach ($this->modulesDirs as $modulesDir) {
130+
$mpath = "$modulesDir/$name";
131+
132+
$moduleclasspath = "$mpath/php/module.class.inc";
133+
134+
if (file_exists($moduleclasspath)) {
135+
include_once $moduleclasspath;
136+
$className = "\LORIS\\$name\Module";
137+
$cls = new $className($this, $name, $mpath);
138+
return $cls;
139+
}
140+
}
141+
throw new \LorisNoSuchModuleException("No such module: $name");
130142
}
131143

132144
/**

src/Router/BaseRouter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
120120

121121
$factory->setBaseURL($baseurl);
122122

123-
$module = $this->loris->getModule($modulename);
123+
$module = $this->loris->getModule($modulename);
124+
$module->registerAutoloader();
125+
124126
$mr = new ModuleRouter($module);
125127
$request = $request->withURI($suburi);
126128
return $ehandler->process($request, $mr);
@@ -137,7 +139,9 @@ public function handle(ServerRequestInterface $request) : ResponseInterface
137139
->withAttribute("baseurl", $baseurl->__toString())
138140
->withAttribute("CandID", $components[0]);
139141
$module = $this->loris->getModule("timepoint_list");
140-
$mr = new ModuleRouter($module);
142+
$module->registerAutoloader();
143+
144+
$mr = new ModuleRouter($module);
141145
return $ehandler->process($request, $mr);
142146
}
143147
}

test/unittests/UserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ public function testGetPermissionsVerbose()
11321132
$loris = new \LORIS\LorisInstance(
11331133
$this->_dbMock,
11341134
new \NDB_Config(),
1135-
[],
1135+
[__DIR__ . "/../../modules"],
11361136
);
11371137
$this->assertEquals(
11381138
$this->_user->getPermissionsVerbose($loris),

tools/manage_modules.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
* have a valid \Module descriptor to the modules table.
1010
*
1111
* If the --remove parameter is provided, it will remove anything in the
12-
* modules table that can not be instantiated with Module::factory from
13-
* the modules table.
12+
* modules table that can not be instantiated with from the modules table.
1413
*
1514
* If the -n flag is provided, it will not actually add/delete from the
1615
* table, but only tell you what it would otherwise do.
@@ -52,7 +51,7 @@
5251
if (isset($flags['remove'])) {
5352
foreach ($currentModules as $module) {
5453
try {
55-
Module::factory($module);
54+
$lorisInstance->getModule($module);
5655
} catch (\LorisNoSuchModuleException | \LorisModuleMissingException $e) {
5756
print "Removing $module\n";
5857
if ($dryrun) {
@@ -80,6 +79,7 @@
8079
function addDir(string $moduledir): void
8180
{
8281
global $DB;
82+
global $lorisInstance;
8383
global $dryrun;
8484
global $currentModules;
8585

@@ -91,7 +91,7 @@ function addDir(string $moduledir): void
9191
if (!in_array($module, $currentModules, true)) {
9292
if (is_dir("$moduledir/$module")) {
9393
try {
94-
Module::factory($module);
94+
$lorisInstance->getModule($module);
9595
} catch (\LorisNoSuchModuleException
9696
| \LorisModuleMissingException $e
9797
) {

0 commit comments

Comments
 (0)