diff --git a/cli.php b/cli.php new file mode 100644 index 0000000..ea98dde --- /dev/null +++ b/cli.php @@ -0,0 +1,93 @@ +isConnected()) { + if ($db instanceof SQL) { + die($db->getLastError() . "\n"); + } else { + $msg = (is_string($db) ? $db : "Unknown Error"); + die("Database error: $msg\n"); + } + } + + return $db; +} + +function printHelp() { + +} + +function handleDatabase($argv) { + $action = $argv[2] ?? ""; + + switch ($action) { + case 'migrate': + $db = connectDatabase(); + break; + case 'dump': + $config = getDatabaseConfig(); + $output = $argv[3] ?? null; + $user = $config->getLogin(); + $password = $config->getPassword(); + $database = $config->getProperty("database"); + $command = ["mysqldump", "-u", $user, "--password=$password"]; + $descriptorSpec = [STDIN, STDOUT, STDOUT]; + + if ($database) { + $command[] = $database; + } + + if ($output) { + $descriptorSpec[1] = ["file", $output, "w"]; + } + + $process = proc_open($command, $descriptorSpec, $pipes); + proc_close($process); + break; + default: + die("Usage: cli.php db \n"); + } +} + +$argv = $_SERVER['argv']; +if (count($argv) < 2) { + die("Usage: cli.php [options...]\n"); +} + +$command = $argv[1]; +switch ($command) { + case 'help': + printHelp(); + exit; + case 'db': + handleDatabase($argv); + break; + case 'routes': + break; + default: + echo "Unknown command '$command'\n\n"; + printHelp(); + exit; +} \ No newline at end of file diff --git a/core/core.php b/core/core.php index 6f57d3c..04563cc 100644 --- a/core/core.php +++ b/core/core.php @@ -2,6 +2,17 @@ define("WEBBASE_VERSION", "1.2.3"); +spl_autoload_extensions(".php"); +spl_autoload_register(function($class) { + $full_path = getClassPath($class); + if(file_exists($full_path)) { + include_once $full_path; + } else { + include_once getClassPath($class, false); + } +}); + + function getProtocol(): string { return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http"; } diff --git a/index.php b/index.php index 294b30a..7830ffc 100644 --- a/index.php +++ b/index.php @@ -14,16 +14,6 @@ if (!is_readable(getClassPath(Configuration::class))) { die(json_encode(array( "success" => false, "msg" => "Configuration directory is not readable, check permissions before proceeding." ))); } -spl_autoload_extensions(".php"); -spl_autoload_register(function($class) { - $full_path = getClassPath($class, true); - if(file_exists($full_path)) { - include_once $full_path; - } else { - include_once getClassPath($class, false); - } -}); - $config = new Configuration(); $user = new Objects\User($config); $sql = $user->getSQL();