Skip to content

Commit

Permalink
[Tools] Enable PHPCS for manage_modules.php (#6787)
Browse files Browse the repository at this point in the history
See also #5977
  • Loading branch information
laemtl authored Jul 21, 2020
1 parent 4fdcf0f commit 36b0fe3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions test/run-php-linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ declare -a tools_list=(
'generate_tables_sql.php'
'lorisform_parser.php'
'populate_visit_windows.php'
'manage_modules.php'
)

vendor/bin/phpcs --standard=test/LorisCS.xml --extensions=php,inc \
Expand Down
46 changes: 32 additions & 14 deletions tools/manage_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
$dryrun = isset($flags['n']) || isset($flags['dry-run']);
$help = isset($flags['h']) || isset($flags['help']);

if($help) {
if ($help) {
usage();
exit(0);
}

if(!isset($flags['add']) && !isset($flags['remove'])) {
if (!isset($flags['add']) && !isset($flags['remove'])) {
usage();
fwrite(STDERR, "\nMust specify --add or --remove flag\n");
exit(1);
Expand All @@ -49,52 +49,64 @@
"SELECT Name FROM modules",
[]
);
if(isset($flags['remove'])) {
foreach($currentModules as $module) {
if (isset($flags['remove'])) {
foreach ($currentModules as $module) {
try {
Module::factory($module);
} catch(\LorisNoSuchModuleException | \LorisModuleMissingException $e) {
print "Removing $module\n";
if($dryrun) {
if ($dryrun) {
continue;
}
$DB->delete("modules", [ 'Name' => $module]);
}
}
}

if(isset($flags['add'])) {
if (isset($flags['add'])) {
addDir(__DIR__ . "/../modules");
addDir(__DIR__ . "/../project/modules");
}

function addDir(string $moduledir) {
/**
* Adds a new entry to the `modules` table for any subdirectories under $moduledir
* that are valid modules
*
* @param string $moduledir The path to a directory containing code for a
* LORIS module.
*
* @return void
*/
function addDir(string $moduledir): void
{
global $DB;
global $dryrun;
global $currentModules;

$modules = scandir($moduledir);
foreach ($modules as $module) {
if(in_array($module, [".", ".."], true)) {
if (in_array($module, [".", ".."], true)) {
continue;
}
if(!in_array($module, $currentModules, true)) {
if(is_dir("$moduledir/$module")) {
if (!in_array($module, $currentModules, true)) {
if (is_dir("$moduledir/$module")) {
try {
Module::factory($module);
} catch(\LorisNoSuchModuleException | \LorisModuleMissingException $e) {
} catch(\LorisNoSuchModuleException
| \LorisModuleMissingException $e
) {
// Wasn't a valid module, so don't add it.
continue;
}

print "Adding $module\n";
if($dryrun) {
if ($dryrun) {
continue;
}
$DB->insert(
"modules",
[
'Name' => $module,
'Name' => $module,
'Active' => 'Y'
]
);
Expand All @@ -103,7 +115,13 @@ function addDir(string $moduledir) {
}
}

function usage() {
/**
* Prints help text for this tool.
*
* @return void
*/
function usage(): void
{
global $argv;
print <<<ENDHELP
usage: $argv[0] [-n] [--add] [--remove]
Expand Down

0 comments on commit 36b0fe3

Please sign in to comment.