熟練使用 artisan 命令如 php artisan make:model 等來建立檔案的 laravel 盆友們,肯定不習慣點選右鍵新建檔案,尤其是大量重複這樣的操作真的很繁瑣有沒有!
所以想著折騰自定義一條 artisan 命令,實現新建檔案功能。網上查了查,發現 laravel 已經把輪子造好了,只要繼承基類 GeneratorCommand,便可簡單實現功能。題外話:習慣不好,很多時候寧願 Google 教程做伸手黨,也不願意帶著問題通讀程式碼。在此共勉!
原始碼解讀
核心程式碼
public function fire()
{
//獲取拼接名稱空間後的全稱
$name = $this->qualifyClass($this->getNameInput());
//獲取檔案的實際儲存路徑
$path = $this->getPath($name);
//若檔案已存在,直接給出錯誤提示而不會覆蓋原來的檔案
if ($this->alreadyExists($this->getNameInput())) {
$this->error($this->type.' already exists!');
return false;
}
//生成檔案
$this->makeDirectory($path);
//適當地替換模版檔案中的資料,如名稱空間和類名
$this->files->put($path, $this->buildClass($name));
//控制檯提示
$this->info($this->type.' created successfully.');
}
由上述程式碼可以看出,輪子已經造的很完備了。其他部分都比較簡單,具體看下模版檔案替換部分的程式碼,從而按需求編輯模版檔案。
編輯模版檔案
protected function buildClass($name)
{
//獲取模版檔案
$stub = $this->files->get($this->getStub());
//替換名稱空間和類名
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}
DummyNamespace=名稱空間
protected function replaceNamespace(&$stub, $name)
{
$stub = str_replace(
['DummyNamespace', 'DummyRootNamespace'],
[$this->getNamespace($name), $this->rootNamespace()],
$stub
);
return $this;
}
根據自定義的名稱空間替換模版檔案中的名稱空間,所以模版檔案中的名稱空間用 DummyNamespace 來代替。這點在讀原始碼之前看 migrate 的 create.stub 程式碼時,真心一臉懵逼啊。
DummyClass=類名
protected function replaceClass($stub, $name)
{
$class = str_replace($this->getNamespace($name).'\\', '', $name);
return str_replace('DummyClass', $class, $stub);
}
同理可得,模版檔案中的類名用 DummyClass 代替。
實現
很明確分成兩步:
- 建立模版檔案 xxx.stub
- 建立命令——生成檔案
模版
簡單例子 public/stubs/repo.stub
,可根據需要新增建構函式等共性程式碼:
<?php
namespace DummyNamespace;
class DummyClass
{
}
command
class MakeRepo extends GeneratorCommand
{
//執行命令
protected $signature = 'make:repo {name}';
//命令說明
protected $description = 'Create a new repo';
//模版檔案儲存路徑
public function getStub()
{
return public_path('stub/repo.stub');
}
//為生成檔案指定名稱空間
public function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Repos';
}
}
看起來有沒有很簡單,若對 artisan 命令不熟的小夥伴先移步 artisan 命令列看看文件瞭解下。希望能養成記筆記的習慣把,方便以後自己查閱!
本作品採用《CC 協議》,轉載必須註明作者和本文連結