溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Laravel中使用Artisan命令生成自定義模板

發(fā)布時(shí)間:2021-05-17 15:54:54 來源:億速云 閱讀:209 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)如何在Laravel中使用Artisan命令生成自定義模板,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

備注:個(gè)人平時(shí)在寫Repository代碼時(shí)會(huì)這樣寫,如先寫上ShopRepositoryInterface并定義好接口方法如all()、create()update()、delete()、findBy()等等,然后再寫上接口對(duì)應(yīng)的實(shí)現(xiàn)ShopRepository并注入對(duì)應(yīng)的Model即Shop。別的PostRepository、TagRepository也會(huì)是這么寫(當(dāng)然,對(duì)于很多重用的Repository方法可以集體拿到AbstractRepository抽象類里供子類繼承,實(shí)現(xiàn)代碼復(fù)用)。那能不能直接命令行生成模板文件呢,就不用自己一個(gè)個(gè)的寫了,就像輸入php artisan make:controller PostController給我一個(gè)Controller模板來。

關(guān)于使用Repository模式來封裝下Model邏輯,不讓Controller里塞滿了很多Model邏輯,這樣做是有很多好處的,最主要的就是好測(cè)試和代碼架構(gòu)清晰,也符合SOLID原則。如果使用PHPUnit來做測(cè)試就知道了為啥說好測(cè)試了。SegmentFault上也有相關(guān)的文章描述。作者也打算最近新開一篇文章聊一聊這個(gè),PHPUnit也打算過段時(shí)間聊一聊。

個(gè)人研究了下Artisan命令行,是可以的。經(jīng)過開發(fā)后,結(jié)果是輸入自定義指令php artisan make:repository PostRepository --model=Post(這個(gè)option可要可不要),就會(huì)幫我生成一個(gè)PostRepositoryInterface和對(duì)應(yīng)的接口實(shí)現(xiàn)PostRepository。

模板文件Stub

由于個(gè)人需要生成一個(gè)RepositoryInterface和對(duì)應(yīng)接口實(shí)現(xiàn)Repository,那就需要兩個(gè)模板文件了。在resources/stubs新建兩個(gè)模板文件,以下是個(gè)人經(jīng)常需要的兩個(gè)模板文件(你可以自定義):

 /**
   * @param array $columns
   * @return \Illuminate\Database\Eloquent\Collection|static[]
   */
  public function all($columns = array('*'))
  {
    return $this->$model_var_name->all($columns);
  }

  /**
   * @param int $perPage
   * @param array $columns
   * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
   */
  public function paginate($perPage = 15, $columns = array('*'))
  {
    return $this->$model_var_name->paginate($perPage, $columns);
  }

  /**
   * Create a new $model_var_name
   * @param array $data
   * @return \$model_namespace
   */
  public function create(array $data)
  {
    return $this->$model_var_name->create($data);
  }

   /**
    * Update a $model_var_name
    * @param array $data
    * @param $id
    * @return \$model_namespace
    */
  public function update($data = [], $id)
  {
    return $this->$model_var_name->whereId($id)->update($data);
  }

  /**
   * Store a $model_var_name
   * @param array $data
   * @return \$model_namespace
   */
  public function store($data = [])
  {
    $this->$model_var_name->id = $data['id'];
    //...
    $this->$model_var_name->save();
  }

  /**
   * Delete a $model_var_name
   * @param array $data
   * @param $id
   * @return \$model_namespace
   */
  public function delete($data = [], $id)
  {
    $this->$model_var_name->whereId($id)->delete();
  }

  /**
   * @param $id
   * @param array $columns
   * @return array|\Illuminate\Database\Eloquent\Collection|static[]
   */
  public function find($id, $columns = array('*'))
  {
    $$model_name = $this->$model_var_name->whereId($id)->get($columns);
    return $$model_name;
  }

  /**
   * @param $field
   * @param $value
   * @param array $columns
   * @return \Illuminate\Database\Eloquent\Collection|static[]
   */
  public function findBy($field, $value, $columns = array('*'))
  {
    $$model_name = $this->$model_var_name->where($field, '=', $value)->get($columns);
    return $$model_name;
  }

}

模板文件里包括參數(shù),這些參數(shù)將會(huì)根據(jù)命令行中輸入的參數(shù)和選項(xiàng)被相應(yīng)替換:

復(fù)制代碼 代碼如下:

['$repository_namespace', '$model_namespace', '$repository_interface_namespace', '$repository_interface', '$class_name', '$model_name', '$model_var_name']

Artisan命令生成Repository模板文件

生成Artisan命令并注冊(cè)

Laravel提供了Artisan命令自定義,輸入指令:

php artisan make:console MakeRepositoryCommand

然后改下簽名和描述:

// app/Console/Commands/MakeRepositoryCommand
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'make:repository {repository} {--model=}';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Make a repository and interface';

這里{repository}是必填參數(shù)并指明(選填參數(shù)加個(gè)?,就和路由參數(shù)一樣),將會(huì)被$this->argument('repository')方法捕捉到,{--model=}是選項(xiàng),可填可不填,將會(huì)被$this->option('model')方法捕捉到。填上這個(gè)命令的描述,最后在Console的Kernel里注冊(cè)下命令:

// app/Console/Kernel
protected $commands = [
    // Commands\Inspire::class,
//    Commands\RedisSubscribe::class,
//    Commands\RedisPublish::class,
//    Commands\MakeTestRepositoryCommand::class,
    Commands\MakeRepositoryCommand::class,
  ];

然后輸入php artisan命令后就能看到這個(gè)make:repository命令了。

如何在Laravel中使用Artisan命令生成自定義模板

自動(dòng)化生成RepositoryInterface和Repository文件

在MakeRepositoryCommand.php命令執(zhí)行文件里寫上模板自動(dòng)生成邏輯,代碼也不長(zhǎng),有些邏輯也有注釋,可看:

use Config;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;

class MakeRepositoryCommand extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'make:repository {repository} {--model=}';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Make a repository and interface';

  /**
   * @var
   */
  protected $repository;

  /**
   * @var
   */
  protected $model;

  /**
   * Create a new command instance.
   *
   * @param Filesystem $filesystem
   * @param Composer $composer
   */
  public function __construct(Filesystem $filesystem, Composer $composer)
  {
    parent::__construct();

    $this->files  = $filesystem;
    $this->composer = $composer;
  }

  /**
   * Execute the console command.
   *
   * @return mixed
   */
  public function handle()
  {
    //獲取repository和model兩個(gè)參數(shù)值
    $argument = $this->argument('repository');
    $option  = $this->option('model');
    //自動(dòng)生成RepositoryInterface和Repository文件
    $this->writeRepositoryAndInterface($argument, $option);
    //重新生成autoload.php文件
    $this->composer->dumpAutoloads();
  }

  private function writeRepositoryAndInterface($repository, $model)
  {
    if($this->createRepository($repository, $model)){
      //若生成成功,則輸出信息
      $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);
    // 創(chuàng)建文件存放路徑, RepositoryInterface放在app/Repositories,Repository個(gè)人一般放在app/Repositories/Eloquent里
    $this->createDirectory();
    // 生成兩個(gè)文件
    return $this->createClass();
  }

  private function createDirectory()
  {
    $directory = $this->getDirectory();
    //檢查路徑是否存在,不存在創(chuàng)建一個(gè),并賦予775權(quán)限
    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) {
      //根據(jù)不同路徑,渲染對(duì)應(yīng)的模板文件
      $class = $this->files->put($this->getPath($key), $template);
    }
    return $class;
  }

  private function getPath($class)
  {
    // 兩個(gè)模板文件,對(duì)應(yīng)的兩個(gè)路徑
    $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()
  {
    // 根據(jù)輸入的repository變量參數(shù),是否需要加上'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()
  {
    // 獲取兩個(gè)模板文件
    $stubs    = $this->getStub();
    // 獲取需要替換的模板文件中變量
    $templateData = $this->getTemplateData();
    $renderStubs = [];
    foreach ($stubs as $key => $stub) {
      // 進(jìn)行模板渲染
      $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選項(xiàng)沒寫,則根據(jù)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配置文件里了:

<?php

return [

  'directory_path' => 'App'.DIRECTORY_SEPARATOR.'Repositories',
  'directory_eloquent_path' => 'App'.DIRECTORY_SEPARATOR.'Repositories'.DIRECTORY_SEPARATOR.'Eloquent',
  'repository_namespace' => 'App\Repositories\Eloquent',
  'repository_interface_namespace' => 'App\Repositories',

];

運(yùn)行一下看可不可以吧,這里截個(gè)圖:

如何在Laravel中使用Artisan命令生成自定義模板

如何在Laravel中使用Artisan命令生成自定義模板

It is working!!!

是可以生成RepositoryInterface和對(duì)應(yīng)的接口實(shí)現(xiàn)文件,這里一個(gè)是加了--model選項(xiàng)一個(gè)沒加的,沒加的話這里第一個(gè)指令就默認(rèn)Model的名稱是Shop。

生成的文件內(nèi)容不截圖了,看下新生成的ShopRepository.php文件,的確是我想要的模板文件:

<?php
/**
 * Created by PhpStorm.
 * User: liuxiang
 */
namespace App\Repositories\Eloquent;

use App\Shop;
use App\Repositories\ShopRepositoryInterface;

class ShopRepository implements ShopRepositoryInterface
{
  /**
   * @var \App\Shop
   */
  public $shop;

  public function __construct(Shop $shop)
  {
    $this->shop = $shop;
  }

  /**
   * @param array $columns
   * @return \Illuminate\Database\Eloquent\Collection|static[]
   */
  public function all($columns = array('*'))
  {
    return $this->shop->all($columns);
  }

  /**
   * @param int $perPage
   * @param array $columns
   * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
   */
  public function paginate($perPage = 15, $columns = array('*'))
  {
    return $this->shop->paginate($perPage, $columns);
  }

  /**
   * Create a new shop
   * @param array $data
   * @return \App\Shop
   */
  public function create(array $data)
  {
    return $this->shop->create($data);
  }

   /**
    * Update a shop
    * @param array $data
    * @param $id
    * @return \App\Shop
    */
  public function update($data = [], $id)
  {
    return $this->shop->whereId($id)->update($data);
  }

  /**
   * Store a shop
   * @param array $data
   * @return \App\Shop
   */
  public function store($data = [])
  {
    $this->shop->id = $data['id'];
    //...
    $this->shop->save();
  }

  /**
   * Delete a shop
   * @param array $data
   * @param $id
   * @return \App\Shop
   */
  public function delete($data = [], $id)
  {
    $this->shop->whereId($id)->delete();
  }

  /**
   * @param $id
   * @param array $columns
   * @return array|\Illuminate\Database\Eloquent\Collection|static[]
   */
  public function find($id, $columns = array('*'))
  {
    $Shop = $this->shop->whereId($id)->get($columns);
    return $Shop;
  }

  /**
   * @param $field
   * @param $value
   * @param array $columns
   * @return \Illuminate\Database\Eloquent\Collection|static[]
   */
  public function findBy($field, $value, $columns = array('*'))
  {
    $Shop = $this->shop->where($field, '=', $value)->get($columns);
    return $Shop;
  }

}

Laravel 是什么

Laravel 是一套簡(jiǎn)潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構(gòu)建一個(gè)完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡(jiǎn)潔、富于表達(dá)力。

看完上述內(nèi)容,你們對(duì)如何在Laravel中使用Artisan命令生成自定義模板有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI