Console 自动生成 Model 文件

直接看代码 OR 放在自己的 Console 即可执行了

<?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 协议》,转载必须注明作者和本文链接
讨论数量: 1

需要依赖 composer

composer require doctrine/dbal
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!