view src/Generator/Generator.php @ 36:76584181267a ls_dev_2025_09

Got factories working in a basic way, not sure how complex tables will handle it though
author Luka Sitas <sitas.luka.97@gmail.com>
date Sat, 20 Sep 2025 17:14:29 -0400
parents 1a717c7b211f
children 116b36f5e73b
line wrap: on
line source

<?php

namespace Wizard\MagicForger\Generator;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'mf')]
class Generator extends BaseGenerator
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $name = 'mf';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generates any (or all) of the available files.';

    /**
     * Execute the console command.
     */
    public function handle()
    {

        // First we need to ensure that the table exists, then we can
        if (! $this->tableExists($this->getTableInput())) {
            $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');

            return false;
        }

        /* $this->setCurrentTable($this->getTableInput()); */
        /* dd($this->getCurrentTable()->getForeignKeys()); */

        if ($this->option('all')) {
            $this->input->setOption('factory', true);
            /* $this->input->setOption('seed', true); */
            /* $this->input->setOption('migration', true); */
            $this->input->setOption('controller', true);
            $this->input->setOption('model', true);
            $this->input->setOption('request', true);
            $this->input->setOption('view', true);
            $this->input->setOption('route', true);
        }

        if ($this->option('factory')) {
            $this->createFactory();
        }

        /* if ($this->option('migration')) { */
        /*     $this->createMigration(); */
        /* } */

        /* if ($this->option('seed')) { */
        /*     $this->createSeeder(); */
        /* } */

        if ($this->option('controller')) {
            $this->createController();
        }

        if ($this->option('model')) {
            $this->createModel();
        }

        if ($this->option('request')) {
            $this->createRequest();
        }

        if ($this->option('view')) {
            $this->createView();
        }

        if ($this->option('route')) {
            $this->createRoute();
        }
    }

    /**
     * Get the console command options.
     */
    protected function getOptions(): array
    {
        return array_merge(parent::getOptions(), [
            ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the table.'],
            ['controller', 'c', InputOption::VALUE_NONE, 'Generate a controller class for the table.'],
            ['model', 'm', InputOption::VALUE_NONE, 'Generate a model class for the table.'],
            ['request', 'r', InputOption::VALUE_NONE, 'Generate base request classes for the table.'],
            ['view', '', InputOption::VALUE_NONE, 'Generate base views for the table.'],
            ['route', 'w', InputOption::VALUE_NONE, 'Generate base routes classes for the table.'],
            ['factory', 'f', InputOption::VALUE_NONE, 'Generate base factory classes for the table.'],
        ]);
    }

    /**
     * Interact further with the user if they were prompted for missing arguments.
     */
    protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void {}

    protected function getStub(): void {}

    protected function createController(): void
    {
        $this->call('mf:controller', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
    }

    protected function createModel()
    {
        $this->call('mf:model', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
    }

    protected function createRequest()
    {
        $this->call('mf:request', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh'), '--all' => true]);
    }

    protected function createView()
    {
        $this->call('mf:view', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh'), '--all' => true]);
    }

    protected function createRoute()
    {
        $this->call('mf:route', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
    }

    protected function createFactory()
    {
        $this->call('mf:factory', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
    }
}