mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-03 16:51:40 +09:00
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:
parent
52d11c58fb
commit
42d09bde65
3 changed files with 51 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -34,10 +34,13 @@ if ($uid === 0)
|
|||
echo "This script must NOT be executed by the root user.\n";
|
||||
exit(2);
|
||||
}
|
||||
$web_server_uid = fileowner(RX_BASEDIR . 'files/config/config.php');
|
||||
if ($uid !== $web_server_uid)
|
||||
if (file_exists(\RX_BASEDIR . 'files/config/config.php'))
|
||||
{
|
||||
$web_server_uid = posix_getpwuid($web_server_uid);
|
||||
echo "This script must be executed by the same user as the usual web server process ({$web_server_uid['name']}).\n";
|
||||
exit(3);
|
||||
$web_server_uid = fileowner(\RX_BASEDIR . 'files/config/config.php');
|
||||
if ($uid !== $web_server_uid)
|
||||
{
|
||||
$web_server_uid = posix_getpwuid($web_server_uid);
|
||||
echo "This script must be executed by the same user as the usual web server process ({$web_server_uid['name']}).\n";
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
index.php
14
index.php
|
|
@ -47,6 +47,14 @@ Context::init();
|
|||
/**
|
||||
* Initialize and execute the requested module.
|
||||
*/
|
||||
$oModuleHandler = new ModuleHandler();
|
||||
$oModuleHandler->init() && $oModuleHandler->displayContent($oModuleHandler->procModule());
|
||||
Context::close();
|
||||
if (PHP_SAPI !== 'cli')
|
||||
{
|
||||
$oModuleHandler = new ModuleHandler();
|
||||
$oModuleHandler->init() && $oModuleHandler->displayContent($oModuleHandler->procModule());
|
||||
Context::close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Rhymix\Framework\Debug::disable();
|
||||
ModuleHandler::procCommandLineArguments($argv);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue