Console 自動生成 Model 檔案

h6play發表於2020-01-02
<?php

namespace App\Console\Commands\Once;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class MakeTableModelCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'once:make_table_model';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '[Once] 生成資料庫表模型';

    protected $modelPath = "";
    protected $modelBasePath = "";

    protected $ignore = [
        "admin_menu",
        "admin_operation_log",
        "admin_permissions",
        "admin_role_permissions",
        "admin_role_menu",
        "admin_role_users",
        "admin_roles",
        "admin_user_permissions",
        "admin_users",
        "failed_jobs",
        "migrations",
    ];

    protected $alias = [
        "users" => "User"
    ];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->ignore = array_merge($this->ignore, ["model"]);
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Make Dir
        $this->makeDir();

        // Create Base Model
        $this->makeBaseModel();

        // Get Table Array
        $tables = $this->tables();

        // Make Base Model And Model
        foreach ($tables as $table) {
            if(!in_array($table, $this->ignore)) {
                if(isset($this->alias[$table])) {
                    $class = $this->alias[$table];
                } else {
                    $class = $this->convertUnderline($table);
                }
                $this->makeBaseModelItem($table, $class);
                $this->makeModelItem($table, $class);
            }
        }

        return null;
    }

    protected function makeDir() {
        $this->modelPath = app_path("/Models/");
        $this->modelBasePath = app_path("/Models/Base/");

        !is_dir($this->modelPath) && @mkdir($this->modelPath, 0777, true);
        !is_dir($this->modelBasePath) && @mkdir($this->modelBasePath, 0777, true);
    }

    protected function makeBaseModel() {
        $content = <<<EOT
<?php

namespace App\Models\Base;

/**
 * Class Model
 * @package App\Models\Base
 */
class Model extends \Illuminate\Database\Eloquent\Model
{

}
EOT;
        !file_exists($this->modelBasePath . "Model.php") && file_put_contents($this->modelBasePath . "Model.php", $content);
    }

    protected function tables() {
        return DB::connection()->getDoctrineSchemaManager()->listTableNames();
    }

    protected function makeBaseModelItem($table, $class) {

        // Get Table Column Array
        $columns = DB::getDoctrineSchemaManager()->listTableDetails($table)->getColumns();

        // Get Table Column notes
        $notes = "";
        foreach ($columns as $column) {
            $name = $column->getName();
            $comment = $column->getComment();
            $notes = $notes . " * @property mixed \${$name}  {$comment}\n";
        }
        $notes = $notes . " * ";

        $content = <<<EOT
<?php

namespace App\Models\Base;

/**
 * Class {$class}
 * @package App\Models\Base
{$notes}
 */
class {$class} extends Model
{
    protected \$table = "{$table}";
}
EOT;

        file_put_contents($this->modelBasePath . "{$class}.php", $content);
    }

    protected function makeModelItem($table, $class) {
        $content = <<<EOT
<?php

namespace App\Models;

/**
 * Class {$class}
 * @package App\Models\Base
 */
class {$class} extends \App\Models\Base\\{$class}
{

}
EOT;
        !file_exists($this->modelPath . "{$class}.php") && file_put_contents($this->modelPath . "{$class}.php", $content);
    }

    protected function convertUnderline( $str , $ucFirst = true)
    {
        while(($pos = strpos($str , '_'))!==false) {
            $str = substr($str , 0 , $pos).ucfirst(substr($str , $pos+1));
        }
        return $ucFirst ? ucfirst($str) : $str;
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章