view src/Generator/Generator.php @ 5:b0b2e79ad8e6

Not exatly sure what was changed but commiting to it :)
author luka
date Thu, 12 Oct 2023 19:41:04 -0400
parents 6468684362c2
children 769a17898cc0
line wrap: on
line source

<?php

namespace Wizzard\MagicForger\Generator;

use DB;

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

use Wizzard\MagicForger\Generator\BaseGenerator;

#[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('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('route')) {
            $this->createRoute();
        }

    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        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.'],
            ['route', 'w', InputOption::VALUE_NONE, 'Generate base routes classes for the table.'],
        ]);
    }

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

    protected function getStub()
    {
    }

    protected function createController()
    {
        $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 createRoute()
    {
        $this->call('mf:route', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
    }
}