sehr gutes init

This commit is contained in:
Bruno Eckhardt committed 2026-05-22 12:49:04 +02:00
1 parent 7b332543a2
commit e1c2ef46c5
1 file changed
+212 -23
+212 -23
View File
@@ -34,6 +34,7 @@ $dirs = [
'WEBROOT/src/utils',
'WEBROOT/src/MVC',
'WEBROOT/src/MVC/Core',
'WEBROOT/src/MVC/Classes',
'WEBROOT/src/MVC/Controller',
'WEBROOT/src/MVC/Model',
'WEBROOT/src/MVC/View',
@@ -369,10 +370,16 @@ PHP;
$files['WEBROOT/src/config/env.ini'] = <<<INI
[database]
APP_ENV = "dev"
DB_DEV_HOST = "localhost"
DB_DEV_USERNAME = "root"
DB_DEV_HOST = ""
DB_DEV_USERNAME = ""
DB_DEV_PASSWORD = ""
DB_DEV_DATABASE = "mein_projekt"
DB_DEV_DATABASE = ""
DB_LIVE_HOST = ""
DB_LIVE_USERNAME = ""
DB_LIVE_PASSWORD = ""
DB_LIVE_DATABASE = ""
INI;
// --- DB: databaseConnect.php ---
@@ -398,6 +405,148 @@ function db_connect() {
}
}
PHP;
// -- CLASSES --
$files['WEBROOT/src/MVC/Classes/Api.php'] = <<<'PHP'
<?php
namespace MVC\Classes;
class Api {
public static function jsonResponse($data, int $statusCode = 200) {
header('Content-Type: application/json');
http_response_code($statusCode);
echo json_encode(
[
'status' => $statusCode === 200 ? 'success' : 'error',
'data' => $data,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
);
exit;
}
}
PHP;
$files['WEBROOT/src/MVC/Classes/Client.php'] = <<<'PHP'
<?php
namespace MVC\Classes;
class Client {
public static function ip() {
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
return trim($ipList[0]);
} else if (!empty($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
} else {
return 'UNKNOWN';
}
}
public static function port() {
return $_SERVER['REMOTE_PORT'] ?? 'UNKNOWN';
}
public static function country() {
$response = file_get_contents('http://ip-api.com/json/{' . self::ip() . '}?fields=country,countryCode');
$data = json_decode($response);
return $data->country ?? 'UNKNOWN';
}
public static function userAgent() {
return $_SERVER['HTTP_USER_AGENT'] ?? 'UNKNOWN';
}
}
PHP;
$files['WEBROOT/src/MVC/Classes/Cookie.php'] = <<<'PHP'
<?php
namespace MVC\Classes;
class Cookie {
public function set(string $CookieName, int $CookieValue, int $CookieTime) {
setcookie($CookieName, $CookieValue, time()+($CookieTime*60));
}
public function read(string $CookieName) {
if (isset($_COOKIE[$CookieName])) {
return $_COOKIE[$CookieName];
}
return 'Cookie not set';
}
public function Delete(string $CookieName) {
setcookie($CookieName, "", time()-(3600*24));
}
}
PHP;
$files['WEBROOT/src/MVC/Classes/Request.php'] = <<<'PHP'
<?php
namespace MVC\Classes;
class Request {
public static function method() {
return $_SERVER['REQUEST_METHOD'] ?? 'UNKNOWN';
}
public static function uri() {
return $_SERVER['REQUEST_URI'] ?? 'UNKNOWN';
}
public static function headers() {
return getallheaders();
}
public static function body() {
return file_get_contents('php://input');
}
public static function queryParams() {
return $_GET;
}
public static function time() {
return $_SERVER['REQUEST_TIME'] ?? 'UNKNOWN';
}
public static function referrer() {
return $_SERVER['HTTP_REFERER'] ?? 'UNKNOWN';
}
}
PHP;
$files['WEBROOT/src/MVC/Classes/Server.php'] = <<<'PHP'
<?php
namespace MVC\Classes;
class Server {
public static function documentRoot() {
return $_SERVER['DOCUMENT_ROOT'] ?? 'UNKNOWN';
}
public static function currentDir() {
return __DIR__;
}
public static function currentScript() {
return $_SERVER['SCRIPT_NAME'] ?? 'UNKNOWN';
}
public static function name() {
return $_SERVER['SERVER_NAME'] ?? 'UNKNOWN';
}
public static function port() {
return $_SERVER['SERVER_PORT'] ?? 'UNKNOWN';
}
}
PHP;
// --- CORE ---
$files['WEBROOT/src/MVC/Core/Controller.php'] = <<<'PHP'
@@ -666,6 +815,7 @@ $files['WEBROOT/src/MVC/Layout/Templates/main.php'] = <<<'HTML'
.btn-toggle:hover { background: rgba(255,255,255,0.1); }
nav a { display: block; padding: 12px; text-decoration: none; color: inherit; border-radius: 5px; margin-bottom: 5px; }
nav a:hover { background: rgba(0,0,0,0.05); }
nav a.active { background: rgba(0,0,0,0.05); }
.logo { font-size: 1.4rem; font-weight: bold; }
</style>
</head>
@@ -692,10 +842,31 @@ $files['WEBROOT/src/MVC/Layout/Templates/main.php'] = <<<'HTML'
HTML;
// --- CONTROLLERS ---
$files['WEBROOT/src/MVC/Controller/ApiController.php'] = <<<'PHP'
<?php
namespace MVC\Controller;
use MVC\Core\Controller;
use MVC\Classes\Api;
class ApiController extends Controller {
public static function get(){
return Api::jsonResponse([
'data' => 'DummyData',
]);
}
}
PHP;
$files['WEBROOT/src/MVC/Controller/HomeController.php'] = <<<'PHP'
<?php
namespace MVC\Controller;
use MVC\Core\Controller;
use MVC\Classes\Client;
use MVC\Classes\Cookie;
use MVC\Classes\Request;
use MVC\Classes\Server;
class HomeController extends Controller {
public function index() {
@@ -718,6 +889,10 @@ $files['WEBROOT/src/MVC/Controller/TestController.php'] = <<<'PHP'
<?php
namespace MVC\Controller;
use MVC\Core\Controller;
use MVC\Classes\Client;
use MVC\Classes\Cookie;
use MVC\Classes\Request;
use MVC\Classes\Server;
class TestController extends Controller {
public function service1() {
@@ -744,23 +919,30 @@ class TestController extends Controller {
}
PHP;
// --- VIEWS ---
$files['WEBROOT/src/MVC/View/home.php'] = "<h1>Dashboard</h1><p>Willkommen in deinem neuen MVC-System mit integriertem Header.</p>";
$files['WEBROOT/src/MVC/View/test1.php'] = "<h1>Service 1</h1><p><?= \$msg ?></p>";
$files['WEBROOT/src/MVC/View/test2.php'] = "<h1>System Check</h1><ul><?php foreach(\$data as \$k => \$v): ?><li><b><?= \$k ?>:</b> <?= \$v ?></li><?php endforeach; ?></ul>";
// --- API ---
$files['WEBROOT/api/v1/get.php'] = <<<'PHP'
<?php
header('Content-Type: application/json');
echo json_encode(
[
'status' => 'online'
]
);
require_once __DIR__ . '/../../src/MVC/autoloader.php';
use MVC\Controller\ApiController;
$api = (new ApiController());
try {
echo ($api->get());
} catch (\Exception $e) {
echo ($e->getMessage());
}
PHP;
// --- VIEWS ---
$files['WEBROOT/src/MVC/View/home.php'] = "<h1>Dashboard</h1><p>Willkommen in deinem neuen MVC-System mit integriertem Header.</p>";
$files['WEBROOT/src/MVC/View/test1.php'] = "<h1>Service 1</h1><p><?= \$msg ?></p>";
$files['WEBROOT/src/MVC/View/test2.php'] = "<h1>System Check</h1><ul><?php foreach(\$data as \$k => \$v): ?><li><b><?= \$k ?>:</b> <?= \$v ?></li><?php endforeach; ?></ul>";
// --- ENTRY POINT ---
$files['WEBROOT/index.php'] = <<<'PHP'
<?php
@@ -781,11 +963,16 @@ try {
// Sidebar Block
ob_start();
$navItems = [
'home' => '🏠 Home',
'test1' => '🛠️Test 1',
'test2' => '📊 Test 2',
];
?>
<nav>
<a href="?service=home">🏠 Home</a>
<a href="?service=test1">🛠️ Test 1</a>
<a href="?service=test2">📊 Test 2</a>
<?php foreach ($navItems as $navKey => $navLabel): ?>
<a href="?service=<?= $navKey ?>" class="<?= $service === $navKey ? 'active' : '' ?>"><?= $navLabel ?></a>
<?php endforeach; ?>
</nav>
<?php
$layout->setBlock('sidebar', ob_get_clean());
@@ -918,6 +1105,10 @@ $controllerContent = <<<PHPCTRL
namespace MVC\Controller;
use MVC\Core\Controller;
use MVC\Classes\Client;
use MVC\Classes\Cookie;
use MVC\Classes\Request;
use MVC\Classes\Server;
// use MVC\Model\YourModel; // Importiere dein Model
class {$className}Controller extends Controller {
@@ -967,11 +1158,8 @@ if (!file_exists($viewFile)) {
// 3. Anleitung ausgeben
echo "\n--- NÄCHSTE SCHRITTE ---\n";
echo "1. Registriere das Modul in WEBROOT/index.php beim Router:\n";
echo " Registriere das Modul in WEBROOT/index.php beim Router:\n";
echo " \$router->add('{$key}', \\MVC\\Controller\\{$className}Controller::class, '{$key}', ['auth']);\n\n";
echo "2. Füge den Service in der Seitenverwaltung (Admin) oder in config.php hinzu:\n";
echo " ID: {$key}\n";
echo " Name: " . ucfirst($name) . "\n";
echo "------------------------\n";
PHP;
@@ -982,6 +1170,7 @@ foreach ($files as $file => $content) {
echo "📄 Datei erstellt: $file\n";
}
echo "\n✨ Projekt '$projectName' wurde mit HEADER initialisiert!\n";
echo "1. Starten: php server.php\n";
echo "2. Öffnen: http://localhost:8000\n";
echo "\n✨ Projekt '$projectName' wurde initialisiert!\n";
echo "1. Richte deine Datenbankverbindung in WEBROOT/src/config/env.ini ein.\n";
echo "2. Starten: php server.php\n";
echo "3. Öffnen: http://localhost:8000\n";