额,弄了一个生成 Transformer 的命令

就两个文件,针对该课程用来生成transformer的,除了名字,没有任何定制化参数,,,
使用如下:
artisan bbs:make-transformer User
会生成一个UserTransformer.php文件

文件app\Console\Commands\stubs\transformer

<?php

namespace App\Transformers;

use App\Models\DummyClass;
use League\Fractal\TransformerAbstract;

class DummyClassTransformer extends TransformerAbstract
{
    public function transform(DummyClass $dummyInstance)
    {
        return [
            'id' => $dummyInstance->id,
        ];
    }
}

文件app\Console\Commands\MakeTransformer.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;

class MakeTransformer extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'bbs:make-transformer {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建 transformer';

    protected function getStub()
    {
        return __DIR__.'/stubs/transformer';
    }

    protected function getPath($name)
    {
        $name = Str::replaceFirst($this->rootNamespace(), '', $name);

        return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Transformer.php';
    }

    protected function buildClass($name)
    {
        $stub = $this->files->get($this->getStub());

        return $this->replaceClass($this->replaceInstance($stub, $name), $name);
    }

    protected function replaceInstance($stub, $name)
    {
        $class = str_replace($this->getNamespace($name).'\\', '', $name);

        return str_replace('dummyInstance', lcfirst($class), $stub);
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Transformers';
    }
}
本帖已被设为精华帖!
本帖由系统于 4年前 自动加精
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

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