Provide a single entry point for all command-line scripts

Core example:

php index.php common.clean_empty_dirs
  -> executes common/scripts/clean_empty_dirs.php

Third-party example:

php index.php module_name.script_name
  -> executes modules/module_name/scripts/script_name.php

This should be the preferred method of executing PHP scripts included with
any third-party module, because it ensures that the working environment is
correct. For example, it will set the current working directory to Rhymix root,
check that the UID matches the usual web user, include all core libraries and
initialize the Context properly.

Over time, we will expand the command-line functionality similar to
the 'artisan' command in Laravel.
This commit is contained in:
Kijin Sung 2023-08-16 23:07:03 +09:00
parent 52d11c58fb
commit 42d09bde65
3 changed files with 51 additions and 8 deletions

View file

@ -1226,6 +1226,38 @@ class ModuleHandler extends Handler
$oDisplayHandler->printContent($oModule);
}
/**
* Process command line arguments.
*
* @param array $args
* @return void
*/
public static function procCommandLineArguments(array $args)
{
$command = count($args) > 1 ? $args[1] : '';
$args = count($args) > 2 ? array_slice($args, 2) : [];
if (!$command)
{
echo 'Command not supplied.' . PHP_EOL;
exit(1);
}
if (!preg_match('/^([a-z0-9_-]+)\.([a-z0-9_-]+)$/', $command, $matches))
{
echo 'Invalid command: ' . $command . PHP_EOL;
exit(1);
}
$path = sprintf('%s%s/scripts/%s.php', \RX_BASEDIR , $matches[1] === 'common' ? 'common' : ('modules/' . $matches[1]), $matches[2]);
if (!file_exists($path) || !is_readable($path))
{
echo 'Command not found: ' . $command . PHP_EOL;
exit(1);
}
require_once \RX_BASEDIR . 'common/scripts/common.php';
require_once $path;
}
/**
* returns module's path
* @param string $module module name