Framework/src/commands/create_model.php
Funky Waddle c845868f41 feat: implement M9 & M10 (CLI, Scaffolding, Security, JWT) and standardize middleware
- Implement full suite of 'phred' CLI generators and utility commands (M9).
- Refactor scaffolding logic to use external stubs in 'src/stubs'.
- Add security hardening via SecureHeaders, Csrf, and CORS middleware (M10).
- Implement JWT token issuance and validation service with lcobucci/jwt.
- Integrate 'getphred/flagpole' for feature flag support.
- Introduce abstract 'Middleware' base class for standardized PSR-15 implementation.
- Add robust driver validation to OrmServiceProvider.
- Fix JwtTokenService claims access and validation constraints.
- Update MILESTONES.md status.
2025-12-22 15:52:41 -06:00

85 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
use Phred\Console\Command;
use Symfony\Component\Console\Input\InputInterface as Input;
use Symfony\Component\Console\Output\OutputInterface as Output;
return new class extends Command {
protected string $command = 'create:model';
protected string $description = 'Scaffold a new model in a module.';
protected array $options = [
'name' => [
'mode' => 'argument',
'required' => true,
'description' => 'Model name (e.g., Post)',
],
'module' => [
'mode' => 'argument',
'required' => false,
'description' => 'Target module name (e.g., Blog). Optional if using create:<module>:model',
],
];
public function handle(Input $input, Output $output): int
{
$module = null;
if (preg_match('/^create:([^:]+):model$/', $this->getName(), $matches)) {
$module = $matches[1];
}
if (!$module) {
$module = $input->hasArgument('module') ? $input->getArgument('module') : null;
}
$module = trim((string) $module);
$name = trim((string) $input->getArgument('name'));
if ($module === '' || $name === '') {
$output->writeln('<error>Module and Name are required.</error>');
return 1;
}
// Case-insensitive module directory lookup
$modulesDir = getcwd() . '/modules';
$moduleDir = null;
if (is_dir($modulesDir)) {
foreach (scandir($modulesDir) as $dir) {
if (strtolower($dir) === strtolower($module)) {
$moduleDir = $modulesDir . '/' . $dir;
$module = $dir; // Use actual casing
break;
}
}
}
if (!$moduleDir || !is_dir($moduleDir)) {
$output->writeln("<error>Module '$module' does not exist.</error>");
return 1;
}
$modelsDir = $moduleDir . '/Models';
if (!is_dir($modelsDir)) {
@mkdir($modelsDir, 0777, true);
}
$path = $modelsDir . '/' . $name . '.php';
if (file_exists($path)) {
$output->writeln("<error>Model '$name' already exists in module '$module'.</error>");
return 1;
}
$namespace = "Project\\Modules\\$module\\Models";
$stub = file_get_contents(dirname(__DIR__) . '/stubs/model.stub');
$template = strtr($stub, [
'{{namespace}}' => $namespace,
'{{class}}' => $name,
]);
file_put_contents($path, $template);
$output->writeln("<info>Model '$name' created</info> at modules/$module/Models/$name.php");
return 0;
}
};