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

2018-12-13 12:42:14 来源:互联网作者:x1036 人气: 次阅读 519 条评论

本文主要讲述Laravel的Artisan命令来实现自定义模板,就如经常输入的php artisan make:controller ShopController就会自动生成一个ShopController.php模板文件一样,通过命令生...

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

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

 

复制代码代码如下:
  1. ['$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.   /**
  10.    * The console command description.
  11.    *
  12.    * @var string
  13.    */
  14.   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.  
  6. class MakeRepositoryCommand extends Command
  7. {
  8.   /**
  9.    * The name and signature of the console command.
  10.    *
  11.    * @var string
  12.    */
  13.   protected $signature = 'make:repository {repository} {--model=}';
  14.  
  15.   /**
  16.    * The console command description.
  17.    *
  18.    * @var string
  19.    */
  20.   protected $description = 'Make a repository and interface';
  21.  
  22.   /**
  23.    * @var
  24.    */
  25.   protected $repository;
  26.  
  27.   /**
  28.    * @var
  29.    */
  30.   protected $model;
  31.  
  32.   /**
  33.    * Create a new command instance.
  34.    *
  35.    * @param Filesystem $filesystem
  36.    * @param Composer $composer
  37.    */
  38.   public function __construct(Filesystem $filesystem, Composer $composer)
  39.   {
  40.     parent::__construct();
  41.  
  42.     $this->files  = $filesystem;
  43.     $this->composer = $composer;
  44.   }
  45.  
  46.   /**
  47.    * Execute the console command.
  48.    *
  49.    * @return mixed
  50.    */
  51.   public function handle()
  52.   {
  53.     //获取repository和model两个参数值
  54.     $argument = $this->argument('repository');
  55.     $option  = $this->option('model');
  56.     //自动生成RepositoryInterface和Repository文件
  57.     $this->writeRepositoryAndInterface($argument, $option);
  58.     //重新生成autoload.php文件
  59.     $this->composer->dumpAutoloads();
  60.   }
  61.  
  62.   private function writeRepositoryAndInterface($repository, $model)
  63.   {
  64.     if($this->createRepository($repository, $model)){
  65.       //若生成成功,则输出信息
  66.       $this->info('Success to make a '.ucfirst($repository).' Repository and a '.ucfirst($repository).'Interface Interface');
  67.     }
  68.   }
  69.  
  70.   private function createRepository($repository, $model)
  71.   {
  72.     // getter/setter 赋予成员变量值
  73.     $this->setRepository($repository);
  74.     $this->setModel($model);
  75.     // 创建文件存放路径, RepositoryInterface放在app/Repositories,Repository个人一般放在app/Repositories/Eloquent里
  76.     $this->createDirectory();
  77.     // 生成两个文件
  78.     return $this->createClass();
  79.   }
  80.  
  81.   private function createDirectory()
  82.   {
  83.     $directory = $this->getDirectory();
  84.     //检查路径是否存在,不存在创建一个,并赋予775权限
  85.     if(! $this->files->isDirectory($directory)){
  86.       return $this->files->makeDirectory($directory, 0755, true);
  87.     }
  88.   }
  89.  
  90.   private function getDirectory()
  91.   {
  92.     return Config::get('repository.directory_eloquent_path');
  93.   }
  94.  
  95.   private function createClass()
  96.   {
  97.     //渲染模板文件,替换模板文件中变量值
  98.     $templates = $this->templateStub();
  99.     $class   = null;
  100.     foreach ($templates as $key => $template) {
  101.       //根据不同路径,渲染对应的模板文件
  102.       $class = $this->files->put($this->getPath($key), $template);
  103.     }
  104.     return $class;
  105.   }
  106.  
  107.   private function getPath($class)
  108.   {
  109.     // 两个模板文件,对应的两个路径
  110.     $path = null;
  111.     switch($class){
  112.       case 'Eloquent':
  113.         $path = $this->getDirectory().DIRECTORY_SEPARATOR.$this->getRepositoryName().'.php';
  114.         break;
  115.       case 'Interface':
  116.         $path = $this->getInterfaceDirectory().DIRECTORY_SEPARATOR.$this->getInterfaceName().'.php';
  117.         break;
  118.     }
  119.  
  120.     return $path;
  121.   }
  122.  
  123.   private function getInterfaceDirectory()
  124.   {
  125.     return Config::get('repository.directory_path');
  126.   }
  127.  
  128.   private function getRepositoryName()
  129.   {
  130.     // 根据输入的repository变量参数,是否需要加上'Repository'
  131.     $repositoryName = $this->getRepository();
  132.     if((strlen($repositoryName) < strlen('Repository')) || strrpos($repositoryName, 'Repository', -11)){
  133.       $repositoryName .= 'Repository';
  134.     }
  135.     return $repositoryName;
  136.   }
  137.  
  138.   private function getInterfaceName()
  139.   {
  140.     return $this->getRepositoryName().'Interface';
  141.   }
  142.  
  143.   /**
  144.    * @return mixed
  145.    */
  146.   public function getRepository()
  147.   {
  148.     return $this->repository;
  149.   }
  150.  
  151.   /**
  152.    * @param mixed $repository
  153.    */
  154.   public function setRepository($repository)
  155.   {
  156.     $this->repository = $repository;
  157.   }
  158.  
  159.   /**
  160.    * @return mixed
  161.    */
  162.   public function getModel()
  163.   {
  164.     return $this->model;
  165.   }
  166.  
  167.   /**
  168.    * @param mixed $model
  169.    */
  170.   public function setModel($model)
  171.   {
  172.     $this->model = $model;
  173.   }
  174.  
  175.   private function templateStub()
  176.   {
  177.     // 获取两个模板文件
  178.     $stubs    = $this->getStub();
  179.     // 获取需要替换的模板文件中变量
  180.     $templateData = $this->getTemplateData();
  181.     $renderStubs = [];
  182.     foreach ($stubs as $key => $stub) {
  183.       // 进行模板渲染
  184.       $renderStubs[$key] = $this->getRenderStub($templateData, $stub);
  185.     }
  186.  
  187.     return $renderStubs;
  188.   }
  189.  
  190.   private function getStub()
  191.   {
  192.     $stubs = [
  193.       'Eloquent' => $this->files->get(resource_path('stubs/Repository').DIRECTORY_SEPARATOR.'Eloquent'.DIRECTORY_SEPARATOR.'repository.stub'),
  194.       'Interface' => $this->files->get(resource_path('stubs/Repository').DIRECTORY_SEPARATOR.'repository_interface.stub'),
  195.     ];
  196.  
  197.     return $stubs;
  198.   }
  199.  
  200.   private function getTemplateData()
  201.   {
  202.     $repositoryNamespace     = Config::get('repository.repository_namespace');
  203.     $modelNamespace        = 'App\\'.$this->getModelName();
  204.     $repositoryInterfaceNamespace = Config::get('repository.repository_interface_namespace');
  205.     $repositoryInterface     = $this->getInterfaceName();
  206.     $className          = $this->getRepositoryName();
  207.     $modelName          = $this->getModelName();
  208.  
  209.     $templateVar = [
  210.       'repository_namespace'      => $repositoryNamespace,
  211.       'model_namespace'        => $modelNamespace,
  212.       'repository_interface_namespace' => $repositoryInterfaceNamespace,
  213.       'repository_interface'      => $repositoryInterface,
  214.       'class_name'           => $className,
  215.       'model_name'           => $modelName,
  216.       'model_var_name'         => strtolower($modelName),
  217.     ];
  218.  
  219.     return $templateVar;
  220.   }
  221.  
  222.   private function getRenderStub($templateData, $stub)
  223.   {
  224.     foreach ($templateData as $search => $replace) {
  225.       $stub = str_replace('$'.$search, $replace, $stub);
  226.     }
  227.  
  228.     return $stub;
  229.   }
  230.  
  231.   private function getModelName()
  232.   {
  233.     $modelName = $this->getModel();
  234.     if(isset($modelName) && !empty($modelName)){
  235.       $modelName = ucfirst($modelName);
  236.     }else{
  237.       // 若option选项没写,则根据repository来生成Model Name
  238.       $modelName = $this->getModelFromRepository();
  239.     }
  240.  
  241.     return $modelName;
  242.   }
  243.  
  244.   private function getModelFromRepository()
  245.   {
  246.     $repository = strtolower($this->getRepository());
  247.     $repository = str_replace('repository', '', $repository);
  248.     return ucfirst($repository);
  249.   }
  250.  
  251. }

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

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

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

  1. It is working!!!

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

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

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

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

您可能感兴趣的文章

相关文章