isConsole check for 404Page, Fixed CLI Args for get... Made BuildSQL.

main
Robert 1 month ago
parent 254cc88a42
commit 01d0138f43
  1. 88
      src/Framework/Database/BuildSQL.php
  2. 4
      src/Framework/Http/App/App.php
  3. 36
      src/Framework/Http/HttpFactory.php

@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
/**
* @author Robert Strutts
* @copyright (c) 2026, Robert Strutts
* @license MIT
*/
namespace IOcornerstone\Framework\Database;
/**
* Description of BuildSQL
*
* @author Robert Strutts
*/
class BuildSQL
{
/**
* Generate CREATE TABLE SQL from an existing MySQL table.
*/
public function generateCreateTable(\PDO $pdo, string $table): string
{
$sql = "
SELECT
COLUMN_NAME,
COLUMN_TYPE,
IS_NULLABLE,
COLUMN_DEFAULT,
EXTRA,
COLUMN_KEY,
COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = ?
ORDER BY ORDINAL_POSITION
";
$stmt = $pdo->prepare($sql);
$stmt->execute([$table]);
$columns = [];
$primary = [];
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$line = '`' . $row['COLUMN_NAME'] . '` ' . $row['COLUMN_TYPE'];
if ($row['IS_NULLABLE'] === 'NO') {
$line .= ' NOT NULL';
}
if ($row['COLUMN_DEFAULT'] !== null) {
$default = $row['COLUMN_DEFAULT'];
if (strtoupper($default) === 'CURRENT_TIMESTAMP') {
$line .= ' DEFAULT CURRENT_TIMESTAMP';
} else {
$line .= ' DEFAULT ' . $pdo->quote($default);
}
}
if (!empty($row['EXTRA'])) {
$line .= ' ' . strtoupper($row['EXTRA']);
}
if (!empty($row['COLUMN_COMMENT'])) {
$line .= ' COMMENT ' . $pdo->quote($row['COLUMN_COMMENT']);
}
$columns[] = $line;
if ($row['COLUMN_KEY'] === 'PRI') {
$primary[] = '`' . $row['COLUMN_NAME'] . '`';
}
}
if (!empty($primary)) {
$columns[] = 'PRIMARY KEY (' . implode(', ', $primary) . ')';
}
return "CREATE TABLE IF NOT EXISTS `{$table}` (\n "
. implode(",\n ", $columns)
. "\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
}
}

@ -198,6 +198,10 @@ class App implements MiddlewareAwareInterface
private function local404(): ResponseInterface private function local404(): ResponseInterface
{ {
if (Console::isConsole()) {
return (new HttpFactory())->createResponse(404, [], "404-Page NOT Found!");
}
$view = new View(); $view = new View();
$view->addView("OnError/404Page"); $view->addView("OnError/404Page");
$myView = $view->fetch($this); $myView = $view->fetch($this);

@ -28,7 +28,10 @@ final class HttpFactory
); );
} }
/**
* CLI: $ ./main.php GET /Folder/File/Method id=123 name=john
*
*/
public function createCliRequest(): ServerRequestInterface public function createCliRequest(): ServerRequestInterface
{ {
global $argv; global $argv;
@ -41,6 +44,29 @@ final class HttpFactory
$method = 'GET'; $method = 'GET';
} }
$queryParams = [];
$args = array_slice($argv, 3);
// Support both key=value and --key=value formats
foreach ($args as $arg) {
if (strpos($arg, '=') !== false) {
// Remove leading -- if present
$arg = ltrim($arg, '-');
list($key, $value) = explode('=', $arg, 2);
$queryParams[$key] = $value;
}
}
// Parse query string from path
if (strpos($path, '?') !== false) {
$pathParts = explode('?', $path, 2);
$path = $pathParts[0];
parse_str($pathParts[1], $queryStringParams);
$queryParams = array_merge($queryParams, $queryStringParams);
}
$_GET = $queryParams;
return new Request( return new Request(
$method, $method,
Uri::fromString($path ?? '/'), Uri::fromString($path ?? '/'),
@ -50,10 +76,10 @@ final class HttpFactory
], ],
Stream::fromString(file_get_contents('php://input')), Stream::fromString(file_get_contents('php://input')),
$_SERVER, $_SERVER,
queryParams: $_GET ?? [], queryParams: $queryParams,
parsedBody: $_POST ?? null, parsedBody: null,
cookies: $_COOKIE ?? [], cookies: [],
uploadedFiles: $_FILES ?? [], uploadedFiles: [],
attributes: [], attributes: [],
protocol: $this->detectProtocol() protocol: $this->detectProtocol()
); );

Loading…
Cancel
Save