额,弄了一个生成 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';
}
}
本帖已被设为精华帖!
本帖由系统于 5年前 自动加精
:+1:
这个是干什么用的?