当前位置:Gxlcms > php框架 > Laravel学习笔记之Artisan命令生成自定义模板的方法

Laravel学习笔记之Artisan命令生成自定义模板的方法

时间:2021-07-01 10:21:17 帮助过:9人阅读

说明:本文主要讲述Laravel的Artisan命令来实现自定义模板,就如经常输入的php artisan make:controller ShopController就会自动生成一个ShopController.php模板文件一样,通过命令生成模板也会提高开发效率。同时,作者会将开发过程中的一些截图和代码黏上去,提高阅读效率。

备注:个人平时在写Repository代码时会这样写,如先写上ShopRepositoryInterface并定义好接口方法如all()create()update()delete()findBy()等等,然后再写上接口对应的实现ShopRepository并注入对应的Model即Shop。别的PostRepository、TagRepository也会是这么写(当然,对于很多重用的Repository方法可以集体拿到AbstractRepository抽象类里供子类继承,实现代码复用)。那能不能直接命令行生成模板文件呢,就不用自己一个个的写了,就像输入php artisan make:controller PostController给我一个Controller模板来。

关于使用Repository模式来封装下Model逻辑,不让Controller里塞满了很多Model逻辑,这样做是有很多好处的,最主要的就是好测试和代码架构清晰,也符合SOLID原则。如果使用PHPUnit来做测试就知道了为啥说好测试了。SegmentFault上也有相关的文章描述。作者也打算最近新开一篇文章聊一聊这个,PHPUnit也打算过段时间聊一聊。

个人研究了下Artisan命令行,是可以的。经过开发后,结果是输入自定义指令php artisan make:repository PostRepository --model=Post(这个option可要可不要),就会帮我生成一个PostRepositoryInterface和对应的接口实现PostRepository。

模板文件Stub

由于个人需要生成一个RepositoryInterface和对应接口实现Repository,那就需要两个模板文件了。在resources/stubs新建两个模板文件,以下是个人经常需要的两个模板文件(你可以自定义):

  1. /**
  2. * @param array $columns
  3. * @return \Illuminate\Database\Eloquent\Collection|static[]
  4. */
  5. public function all($columns = array('*'))
  6. {
  7. return $this->$model_var_name->all($columns);
  8. }
  9. /**
  10. * @param int $perPage
  11. * @param array $columns
  12. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  13. */
  14. public function paginate($perPage = 15, $columns = array('*'))
  15. {
  16. return $this->$model_var_name->paginate($perPage, $columns);
  17. }
  18. /**
  19. * Create a new $model_var_name
  20. * @param array $data
  21. * @return \$model_namespace
  22. */
  23. public function create(array $data)
  24. {
  25. return $this->$model_var_name->create($data);
  26. }
  27. /**
  28. * Update a $model_var_name
  29. * @param array $data
  30. * @param $id
  31. * @return \$model_namespace
  32. */
  33. public function update($data = [], $id)
  34. {
  35. return $this->$model_var_name->whereId($id)->update($data);
  36. }
  37. /**
  38. * Store a $model_var_name
  39. * @param array $data
  40. * @return \$model_namespace
  41. */
  42. public function store($data = [])
  43. {
  44. $this->$model_var_name->id = $data['id'];
  45. //...
  46. $this->$model_var_name->save();
  47. }
  48. /**
  49. * Delete a $model_var_name
  50. * @param array $data
  51. * @param $id
  52. * @return \$model_namespace
  53. */
  54. public function delete($data = [], $id)
  55. {
  56. $this->$model_var_name->whereId($id)->delete();
  57. }
  58. /**
  59. * @param $id
  60. * @param array $columns
  61. * @return array|\Illuminate\Database\Eloquent\Collection|static[]
  62. */
  63. public function find($id, $columns = array('*'))
  64. {
  65. $$model_name = $this->$model_var_name->whereId($id)->get($columns);
  66. return $$model_name;
  67. }
  68. /**
  69. * @param $field
  70. * @param $value
  71. * @param array $columns
  72. * @return \Illuminate\Database\Eloquent\Collection|static[]
  73. */
  74. public function findBy($field, $value, $columns = array('*'))
  75. {
  76. $$model_name = $this->$model_var_name->where($field, '=', $value)->get($columns);
  77. return $$model_name;
  78. }
  79. }

模板文件里包括参数,这些参数将会根据命令行中输入的参数和选项被相应替换:

代码如下:['$repository_namespace', '$model_namespace', '$repository_interface_namespace', '$repository_interface', '$class_name', '$model_name', '$model_var_name']

Artisan命令生成Repository模板文件

生成Artisan命令并注册

Laravel提供了Artisan命令自定义,输入指令:

  1. php artisan make:console MakeRepositoryCommand

然后改下签名和描述:

  1. // app/Console/Commands/MakeRepositoryCommand
  2. /**
  3. * The name and signature of the console command.
  4. *
  5. * @var string
  6. */
  7. protected $signature = 'make:repository {repository} {--model=}';
  8. /**
  9. * The console command description.
  10. *
  11. * @var string
  12. */
  13. protected $description = 'Make a repository and interface';

这里{repository}是必填参数并指明(选填参数加个?,就和路由参数一样),将会被$this->argument('repository')方法捕捉到,{--model=}是选项,可填可不填,将会被$this->option('model')方法捕捉到。填上这个命令的描述,最后在Console的Kernel里注册下命令:

  1. // app/Console/Kernel
  2. protected $commands = [
  3. // Commands\Inspire::class,
  4. // Commands\RedisSubscribe::class,
  5. // Commands\RedisPublish::class,
  6. // Commands\MakeTestRepositoryCommand::class,
  7. Commands\MakeRepositoryCommand::class,
  8. ];

然后输入php artisan命令后就能看到这个make:repository命令了。

自动化生成RepositoryInterface和Repository文件

在MakeRepositoryCommand.php命令执行文件里写上模板自动生成逻辑,代码也不长,有些逻辑也有注释,可看:

  1. use Config;
  2. use Illuminate\Console\Command;
  3. use Illuminate\Filesystem\Filesystem;
  4. use Illuminate\Support\Composer;
  5. class MakeRepositoryCommand extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'make:repository {repository} {--model=}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Make a repository and interface';
  19. /**
  20. * @var
  21. */
  22. protected $repository;
  23. /**
  24. * @var
  25. */
  26. protected $model;
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @param Filesystem $filesystem
  31. * @param Composer $composer
  32. */
  33. public function __construct(Filesystem $filesystem, Composer $composer)
  34. {
  35. parent::__construct();
  36. $this->files = $filesystem;
  37. $this->composer = $composer;
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return mixed
  43. */
  44. public function handle()
  45. {
  46. //获取repository和model两个参数值
  47. $argument = $this->argument('repository');
  48. $option = $this->option('model');
  49. //自动生成RepositoryInterface和Repository文件
  50. $this->writeRepositoryAndInterface($argument, $option);
  51. //重新生成autoload.php文件
  52. $this->composer->dumpAutoloads();
  53. }
  54. private function writeRepositoryAndInterface($repository, $model)
  55. {
  56. if($this->createRepository($repository, $model)){
  57. //若生成成功,则
输出信息 $this->info('Success to make a '.ucfirst($repository).' Repository and a '.ucfirst($repository).'Interface Interface'); } } private function createRepository($repository, $model) { // getter/setter 赋予成员变量值 $this->setRepository($repository); $this->setModel($model); // 创建文件存放路径, RepositoryInterface放在app/Repositories,Repository个人一般放在app/Repositories/Eloquent里 $this->createDirectory(); // 生成两个文件 return $this->createClass(); } private function createDirectory() { $directory = $this->getDirectory(); //检查路径是否存在,不存在创建一个,并赋予775权限 if(! $this->files->isDirectory($directory)){ return $this->files->makeDirectory($directory, 0755, true); } } private function getDirectory() { return Config::get('repository.directory_eloquent_path'); } private function createClass() { //渲染模板文件,替换模板文件中变量值 $templates = $this->templateStub(); $class = null; foreach ($templates as $key => $template) { //根据不同路径,渲染对应的模板文件 $class = $this->files->put($this->getPath($key), $template); } return $class; } private function getPath($class) { // 两个模板文件,对应的两个路径 $path = null; switch($class){ case 'Eloquent': $path = $this->getDirectory().DIRECTORY_SEPARATOR.$this->getRepositoryName().'.php'; break; case 'Interface': $path = $this->getInterfaceDirectory().DIRECTORY_SEPARATOR.$this->getInterfaceName().'.php'; break; } return $path; } private function getInterfaceDirectory() { return Config::get('repository.directory_path'); } private function getRepositoryName() { // 根据输入的repository变量参数,是否需要加上'Repository' $repositoryName = $this->getRepository(); if((strlen($repositoryName) < strlen('Repository')) || strrpos($repositoryName, 'Repository', -11)){ $repositoryName .= 'Repository'; } return $repositoryName; } private function getInterfaceName() { return $this->getRepositoryName().'Interface'; } /** * @return mixed */ public function getRepository() { return $this->repository; } /** * @param mixed $repository */ public function setRepository($repository) { $this->repository = $repository; } /** * @return mixed */ public function getModel() { return $this->model; } /** * @param mixed $model */ public function setModel($model) { $this->model = $model; } private function templateStub() { // 获取两个模板文件 $stubs = $this->getStub(); // 获取需要替换的模板文件中变量 $templateData = $this->getTemplateData(); $renderStubs = []; foreach ($stubs as $key => $stub) { // 进行模板渲染 $renderStubs[$key] = $this->getRenderStub($templateData, $stub); } return $renderStubs; } private function getStub() { $stubs = [ 'Eloquent' => $this->files->get(resource_path('stubs/Repository').DIRECTORY_SEPARATOR.'Eloquent'.DIRECTORY_SEPARATOR.'repository.stub'), 'Interface' => $this->files->get(resource_path('stubs/Repository').DIRECTORY_SEPARATOR.'repository_interface.stub'), ]; return $stubs; } private function getTemplateData() { $repositoryNamespace = Config::get('repository.repository_namespace'); $modelNamespace = 'App\\'.$this->getModelName(); $repositoryInterfaceNamespace = Config::get('repository.repository_interface_namespace'); $repositoryInterface = $this->getInterfaceName(); $className = $this->getRepositoryName(); $modelName = $this->getModelName(); $templateVar = [ 'repository_namespace' => $repositoryNamespace, 'model_namespace' => $modelNamespace, 'repository_interface_namespace' => $repositoryInterfaceNamespace, 'repository_interface' => $repositoryInterface, 'class_name' => $className, 'model_name' => $modelName, 'model_var_name' => strtolower($modelName), ]; return $templateVar; } private function getRenderStub($templateData, $stub) { foreach ($templateData as $search => $replace) { $stub = str_replace('$'.$search, $replace, $stub); } return $stub; } private function getModelName() { $modelName = $this->getModel(); if(isset($modelName) && !empty($modelName)){ $modelName = ucfirst($modelName); }else{ // 若option选项没写,则根据repository来生成Model Name $modelName = $this->getModelFromRepository(); } return $modelName; } private function getModelFromRepository() { $repository = strtolower($this->getRepository()); $repository = str_replace('repository', '', $repository); return ucfirst($repository); } }

这里把一些常量值放在config/repository.php配置文件里了:

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liuxiang
  5. * Date: 16/6/22
  6. * Time: 17:06
  7. */
  8. return [
  9. 'directory_path' => 'App'.DIRECTORY_SEPARATOR.'Repositories',
  10. 'directory_eloquent_path' => 'App'.DIRECTORY_SEPARATOR.'Repositories'.DIRECTORY_SEPARATOR.'Eloquent',
  11. 'repository_namespace' => 'App\Repositories\Eloquent',
  12. 'repository_interface_namespace' => 'App\Repositories',
  13. ];

运行一下看可不可以吧,这里截个图:


It is working!!!

是可以生成RepositoryInterface和对应的接口实现文件,这里一个是加了--model选项一个没加的,没加的话这里第一个指令就默认Model的名称是Shop。

生成的文件内容不截图了,看下新生成的ShopRepository.php文件,的确是我想要的模板文件:

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liuxiang
  5. */
  6. namespace App\Repositories\Eloquent;
  7. use App\Shop;
  8. use App\Repositories\ShopRepositoryInterface;
  9. class ShopRepository implements ShopRepositoryInterface
  10. {
  11. /**
  12. * @var \App\Shop
  13. */
  14. public $shop;
  15. public function __construct(Shop $shop)
  16. {
  17. $this->shop = $shop;
  18. }
  19. /**
  20. * @param array $columns
  21. * @return \Illuminate\Database\Eloquent\Collection|static[]
  22. */
  23. public function all($columns = array('*'))
  24. {
  25. return $this->shop->all($columns);
  26. }
  27. /**
  28. * @param int $perPage
  29. * @param array $columns
  30. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  31. */
  32. public function paginate($perPage = 15, $columns = array('*'))
  33. {
  34. return $this->shop->paginate($perPage, $columns);
  35. }
  36. /**
  37. * Create a new shop
  38. * @param array $data
  39. * @return \App\Shop
  40. */
  41. public function create(array $data)
  42. {
  43. return $this->shop->create($data);
  44. }
  45. /**
  46. * Update a shop
  47. * @param array $data
  48. * @param $id
  49. * @return \App\Shop
  50. */
  51. public function update($data = [], $id)
  52. {
  53. return $this->shop->whereId($id)->update($data);
  54. }
  55. /**
  56. * Store a shop
  57. * @param array $data
  58. * @return \App\Shop
  59. */
  60. public function store($data = [])
  61. {
  62. $this->shop->id = $data['id'];
  63. //...
  64. $this->shop->save();
  65. }
  66. /**
  67. * Delete a shop
  68. * @param array $data
  69. * @param $id
  70. * @return \App\Shop
  71. */
  72. public function delete($data = [], $id)
  73. {
  74. $this->shop->whereId($id)->delete();
  75. }
  76. /**
  77. * @param $id
  78. * @param array $columns
  79. * @return array|\Illuminate\Database\Eloquent\Collection|static[]
  80. */
  81. public function find($id, $columns = array('*'))
  82. {
  83. $Shop = $this->shop->whereId($id)->get($columns);
  84. return $Shop;
  85. }
  86. /**
  87. * @param $field
  88. * @param $value
  89. * @param array $columns
  90. * @return \Illuminate\Database\Eloquent\Collection|static[]
  91. */
  92. public function findBy($field, $value, $columns = array('*'))
  93. {
  94. $Shop = $this->shop->where($field, '=', $value)->get($columns);
  95. return $Shop;
  96. }
  97. }

总结:本文主要用Laravel的Artisan命令来自动生成个人需要的模板,减少平时开发中重复劳动。就像Laravel自带了很多模板生成命令,用起来会节省很多时间。这是作者在平时开发中遇到的问题,通过利用Laravel Artisan命令解决了,所以Laravel还是挺好玩的。有兴趣的可以把代码扒下来玩一玩,并根据你自己想要的模板做修改。这两天想就Repository模式封装Model逻辑的方法和好处聊一聊,到时见。希望对大家的学习有所帮助,也希望大家多多支持脚本之家

人气教程排行