225 lines
6.7 KiB
PHP
225 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* MVC Project Bootstrapper
|
|
* Erstellt ein neues Projekt-Grundgerüst basierend auf der aktuellen Architektur.
|
|
*/
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
die("Dieses Script muss über die CLI ausgeführt werden.\n");
|
|
}
|
|
|
|
$projectName = $argv[1] ?? null;
|
|
if (!$projectName) {
|
|
echo "Bitte gib einen Projektnamen an: ";
|
|
$handle = fopen("php://stdin", "r");
|
|
$projectName = trim(fgets($handle));
|
|
fclose($handle);
|
|
}
|
|
|
|
if (!$projectName) die("Fehler: Kein Name angegeben.\n");
|
|
|
|
$baseDir = getcwd() . DIRECTORY_SEPARATOR . $projectName;
|
|
|
|
// 1. Verzeichnisstruktur erstellen
|
|
$dirs = [
|
|
'',
|
|
'WEBROOT',
|
|
'WEBROOT/static',
|
|
'WEBROOT/src',
|
|
'WEBROOT/src/MVC',
|
|
'WEBROOT/src/MVC/Core',
|
|
'WEBROOT/src/MVC/Controller',
|
|
'WEBROOT/src/MVC/Model',
|
|
'WEBROOT/src/MVC/View',
|
|
'WEBROOT/src/MVC/Layout',
|
|
'WEBROOT/src/MVC/Layout/Templates',
|
|
];
|
|
|
|
foreach ($dirs as $dir) {
|
|
$path = $baseDir . ($dir ? DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $dir) : '');
|
|
if (!is_dir($path)) {
|
|
mkdir($path, 0755, true);
|
|
echo "📁 Ordner erstellt: $dir\n";
|
|
}
|
|
}
|
|
|
|
// 2. Kern-Dateien definieren
|
|
$files = [];
|
|
|
|
// --- CORE: Controller ---
|
|
$files['WEBROOT/src/MVC/Core/Controller.php'] = <<<'PHP'
|
|
<?php
|
|
namespace MVC\Core;
|
|
class Controller {
|
|
protected $view;
|
|
public function __construct() {
|
|
$this->view = new View(__DIR__ . '/../View');
|
|
}
|
|
protected function render($template, $data = []) {
|
|
foreach ($data as $key => $value) { $this->view->assign($key, $value); }
|
|
return $this->view->render($template);
|
|
}
|
|
protected function json($data, $status = 200) {
|
|
header('Content-Type: application/json');
|
|
http_response_code($status);
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
// --- CORE: View ---
|
|
$files['WEBROOT/src/MVC/Core/View.php'] = <<<'PHP'
|
|
<?php
|
|
namespace MVC\Core;
|
|
class View {
|
|
protected $data = [];
|
|
protected $viewPath;
|
|
public function __construct($viewPath) { $this->viewPath = $viewPath; }
|
|
public function assign($key, $value) { $this->data[$key] = $value; }
|
|
public function render($template) {
|
|
extract($this->data);
|
|
$file = $this->viewPath . '/' . $template . '.php';
|
|
if (file_exists($file)) {
|
|
ob_start(); include $file; return ob_get_clean();
|
|
}
|
|
return "View $template not found.";
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
// --- CORE: Model ---
|
|
$files['WEBROOT/src/MVC/Core/Model.php'] = <<<'PHP'
|
|
<?php
|
|
namespace MVC\Core;
|
|
abstract class Model {
|
|
protected static $table;
|
|
public static function all() { /* Implement logic */ return []; }
|
|
public static function find($id) { /* Implement logic */ return null; }
|
|
public static function query($sql, ...$params) { /* Implement logic */ return null; }
|
|
}
|
|
PHP;
|
|
|
|
// --- CORE: Router ---
|
|
$files['WEBROOT/src/MVC/Core/Router.php'] = <<<'PHP'
|
|
<?php
|
|
namespace MVC\Core;
|
|
class Router {
|
|
protected $routes = [];
|
|
public function add($service, $controller, $method, $middlewares = []) {
|
|
$this->routes[$service] = ['controller' => $controller, 'method' => $method, 'middlewares' => $middlewares];
|
|
}
|
|
public function dispatch($service) {
|
|
if (!isset($this->routes[$service])) throw new \Exception("Service '$service' not found.");
|
|
$route = $this->routes[$service];
|
|
foreach ($route['middlewares'] as $mw) { $this->runMiddleware($mw); }
|
|
$controller = new $route['controller']();
|
|
$method = $route['method'];
|
|
return $controller->$method();
|
|
}
|
|
protected function runMiddleware($mw) {
|
|
if ($mw === 'auth' && empty($_SESSION['user_logged_in'])) { header('Location: login.php'); exit; }
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
// --- CORE: Layout ---
|
|
$files['WEBROOT/src/MVC/Layout/Layout.php'] = <<<'PHP'
|
|
<?php
|
|
namespace MVC\Layout;
|
|
class Layout {
|
|
protected $blocks = [];
|
|
protected $template;
|
|
public function __construct($template = 'main') { $this->template = $template; }
|
|
public function setBlock($name, $content) { $this->blocks[$name] = $content; }
|
|
public function getBlock($name) { return $this->blocks[$name] ?? ''; }
|
|
public function render() {
|
|
$layoutFile = __DIR__ . '/Templates/' . $this->template . '.php';
|
|
if (file_exists($layoutFile)) { ob_start(); include $layoutFile; return ob_get_clean(); }
|
|
return "Layout template not found.";
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
// --- CORE: Autoloader ---
|
|
$files['WEBROOT/src/MVC/autoloader.php'] = <<<'PHP'
|
|
<?php
|
|
spl_autoload_register(function ($class) {
|
|
$prefix = 'MVC\\'; $base_dir = __DIR__ . '/';
|
|
$len = strlen($prefix);
|
|
if (strncmp($prefix, $class, $len) !== 0) return;
|
|
$file = $base_dir . str_replace('\\', '/', substr($class, $len)) . '.php';
|
|
if (file_exists($file)) require $file;
|
|
});
|
|
PHP;
|
|
|
|
// --- LAYOUT TEMPLATE ---
|
|
$files['WEBROOT/src/MVC/Layout/Templates/main.php'] = <<<'HTML'
|
|
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8"><title><?= $this->getBlock('title') ?></title>
|
|
<style>
|
|
body { font-family: sans-serif; display: flex; margin: 0; }
|
|
.sidebar { width: 250px; background: #eee; height: 100vh; padding: 20px; }
|
|
.content { flex: 1; padding: 20px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="sidebar"><?= $this->getBlock('sidebar') ?></div>
|
|
<div class="content"><?= $this->getBlock('content') ?></div>
|
|
</body>
|
|
</html>
|
|
HTML;
|
|
|
|
// --- DEFAULT CONTROLLER ---
|
|
$files['WEBROOT/src/MVC/Controller/HomeController.php'] = <<<'PHP'
|
|
<?php
|
|
namespace MVC\Controller;
|
|
use MVC\Core\Controller;
|
|
class HomeController extends Controller {
|
|
public function index() {
|
|
return $this->render('home', ['welcome' => 'Willkommen in deinem neuen MVC Projekt!']);
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
// --- DEFAULT VIEW ---
|
|
$files['WEBROOT/src/MVC/View/home.php'] = <<<'HTML'
|
|
<h1>Dashboard</h1>
|
|
<p><?= htmlspecialchars($welcome) ?></p>
|
|
HTML;
|
|
|
|
// --- ENTRY POINT: index.php ---
|
|
$files['WEBROOT/index.php'] = <<<'PHP'
|
|
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/src/MVC/autoloader.php';
|
|
|
|
$service = $_GET['service'] ?? 'home';
|
|
|
|
try {
|
|
$router = new \MVC\Core\Router();
|
|
$router->add('home', \MVC\Controller\HomeController::class, 'index');
|
|
|
|
$layout = new \MVC\Layout\Layout('main');
|
|
$layout->setBlock('title', 'Mein Neues Projekt');
|
|
$layout->setBlock('sidebar', '<a href="?service=home">Home</a>');
|
|
$layout->setBlock('content', $router->dispatch($service));
|
|
|
|
echo $layout->render();
|
|
} catch (\Exception $e) {
|
|
echo "<h1>Fehler</h1><p>" . $e->getMessage() . "</p>";
|
|
}
|
|
PHP;
|
|
|
|
// 3. Dateien schreiben
|
|
foreach ($files as $file => $content) {
|
|
file_put_contents($baseDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file), $content);
|
|
echo "📄 Datei erstellt: $file\n";
|
|
}
|
|
|
|
echo "\n✨ Projekt '$projectName' wurde erfolgreich initialisiert!\n";
|
|
echo "Gehe in den Ordner und starte deinen Webserver in WEBROOT.\n";
|
|
PHP;
|