主頁(yè) > 知識(shí)庫(kù) > Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法

Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法

熱門(mén)標(biāo)簽:亳州企業(yè)外呼系統(tǒng) 400 電話 辦理 打開(kāi)百度地圖標(biāo)注 山東電銷(xiāo)卡外呼系統(tǒng)原理是什么 蘇州外呼系統(tǒng)有效果嗎 兼職做地圖標(biāo)注好賺錢(qián)嗎 海南外呼系統(tǒng)方案 智能電銷(xiāo)語(yǔ)音機(jī)器人資訊 地圖標(biāo)注怎么做商戶驗(yàn)證

說(shuō)明:本文主要講述Laravel的Artisan命令來(lái)實(shí)現(xiàn)自定義模板,就如經(jīng)常輸入的php artisan make:controller ShopController就會(huì)自動(dòng)生成一個(gè)ShopController.php模板文件一樣,通過(guò)命令生成模板也會(huì)提高開(kāi)發(fā)效率。同時(shí),作者會(huì)將開(kāi)發(fā)過(guò)程中的一些截圖和代碼黏上去,提高閱讀效率。

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

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

個(gè)人研究了下Artisan命令行,是可以的。經(jīng)過(guò)開(kāi)發(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命令了。

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

在MakeRepositoryCommand.php命令執(zhí)行文件里寫(xiě)上模板自動(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)沒(méi)寫(xiě),則根據(jù)repository來(lái)生成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
/**
 * Created by PhpStorm.
 * User: liuxiang
 * Date: 16/6/22
 * Time: 17:06
 */

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è)圖:


It is working!!!

是可以生成RepositoryInterface和對(duì)應(yīng)的接口實(shí)現(xiàn)文件,這里一個(gè)是加了--model選項(xiàng)一個(gè)沒(méi)加的,沒(méi)加的話這里第一個(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;
  }

}

總結(jié):本文主要用Laravel的Artisan命令來(lái)自動(dòng)生成個(gè)人需要的模板,減少平時(shí)開(kāi)發(fā)中重復(fù)勞動(dòng)。就像Laravel自帶了很多模板生成命令,用起來(lái)會(huì)節(jié)省很多時(shí)間。這是作者在平時(shí)開(kāi)發(fā)中遇到的問(wèn)題,通過(guò)利用Laravel Artisan命令解決了,所以Laravel還是挺好玩的。有興趣的可以把代碼扒下來(lái)玩一玩,并根據(jù)你自己想要的模板做修改。這兩天想就Repository模式封裝Model邏輯的方法和好處聊一聊,到時(shí)見(jiàn)。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家

您可能感興趣的文章:
  • Laravel如何自定義command命令淺析
  • 詳細(xì)Laravel5.5執(zhí)行表遷移命令出現(xiàn)表為空的解決方案
  • laravel通過(guò)創(chuàng)建自定義artisan make命令來(lái)新建類文件詳解
  • laravel 創(chuàng)建命令行命令的圖文教程

標(biāo)簽:呼倫貝爾 綏化 清遠(yuǎn) 安康 紹興 萊蕪 金華 溫州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法》,本文關(guān)鍵詞  Laravel,學(xué)習(xí),筆記,之,Artisan,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Laravel學(xué)習(xí)筆記之Artisan命令生成自定義模板的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章