Skip to content

MakeCommand — 生成类命令基类

  • 文件位置: kernel/Commands/MakeCommand.php
  • 命名空间: kernel\Commands
  • 是否可继承: 是(抽象基类)

make:model / make:controller / make:middleware 提供公共能力:子目录解析、命名空间拼接、骨架文件写入。不定义 $name,不会被注册为命令。

方法

方法说明
split(string $arg): array解析 "Admin/User"[子目录, 类短名]
joinNamespace(string $base, string $subDir): string拼接命名空间
write(string $targetDir, string $className, string $namespace, string $body, bool $force, $console): bool写入骨架(已存在保护,--force 覆盖)
tableName(string $className): stringUserModel → userOrderItemModel → order_item

使用

php
use kernel\Commands\MakeCommand;

class MakeSomethingCommand extends MakeCommand
{
    public function handle($console, $args, $options): int
    {
        [$subDir, $shortName] = $this->split($args[0]);
        $namespace = $this->joinNamespace("app\Xxx", $subDir);
        return $this->write(Path::root() . "/Xxx", "$subDir/$shortName", $namespace, $body, !empty($options["force"]), $console) ? 0 : 1;
    }
}