熟練使用 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 命令行看看文檔了解下。希望能養成記筆記的好習慣,供以后自己閱讀吧。