File tree Expand file tree Collapse file tree 3 files changed +71
-5
lines changed
Expand file tree Collapse file tree 3 files changed +71
-5
lines changed Original file line number Diff line number Diff line change 77透過在專案中的 ` resources/stubs ` 目錄下,放置類別的模板文件,然後透過命令快速注入並生成。相比原生的 ` artisan make:* ` 命令可大大減少編寫時間,且模板檔案可隨版控被 git 紀錄。
88
99目前支援的類別類型:
10- - 命名空間 App\\ * 開頭的類別
11- - 命名空間 Tests\\ * 開頭的類別
12- - 命名空間 Database\\ * 開頭的類別
10+
11+ - 命名空間 App\\\* 開頭的類別
12+ - 命名空間 Tests\\\* 開頭的類別
13+ - 命名空間 Database\\\* 開頭的類別
1314
1415## Installation | 安裝
1516
1617此套件尚未發布到 ** Packagist** 需透過下列方法安裝:
1718
18- ```
19+ ``` bash
1920composer config repositories.a2workspace/laravel-stubs vcs https://github.com/A2Workspace/laravel-stubs.git
2021composer require " a2workspace/laravel-stubs:*"
2122```
2223
24+ 接著使用 ` vendor:publish ` 命令生成配置文件與 ` resources/stubs ` 資料夾:
25+
26+ ``` bash
27+ php artisan vendor:publish --tag=@a2workspace/laravel-stubs
28+ ```
29+
30+ 預設的 ` resources/stubs ` 資料夾內附帶一個簡單的範例。
31+
2332## Usage | 如何使用
2433
2534現在你可以使用 ` make:a... ` [ Artisan 命令] ( https://laravel.com/docs/9.x/artisan ) 來生成類別。該命令將會讀取 ` resources/stubs ` 下的目錄或 ` .php ` 檔案,將佔位符依照格式替換為給定的名稱,並依照類別名稱自動生成檔案到相對的路徑。
2635
27- ```
36+ ``` bash
2837php artisan make:a...
2938```
39+
40+ 可以傳入一個搜尋參數:
41+
42+ ``` bash
43+ php artisan make:a... Example
44+ ```
45+
46+ ## Development | 如何編寫自己的類別模板
47+
48+ ** 類別模板** 預設被存放在 ` resources/stubs ` 目錄下,支援單一 ` .php ` 檔案或一整個目錄包。檔案名稱並不會影響注入過程,` laravel-stubs ` 將會以最終處理完的 ` namespace ` 作為依據,將生成的檔案放置到專案的相對路徑。
49+
50+ 你可以建立一個 ** 類別模板** 並透過 ` Dummy ` 佔位符填充類別名稱。
51+
52+ 一個簡單的 ` Model ` 模板如下:
53+
54+ ``` php
55+ <?php
56+
57+ namespace App\Models;
58+
59+ use Illuminate\Database\Eloquent\Model;
60+
61+ class Dummy extends Model
62+ {
63+ protected $table = 'dummies';
64+ }
65+ ```
66+
67+ 在生成時 ` laravel-stubs ` 將會依照以下規則替換:
68+
69+ - ` Dummy ` : 替換為開頭大寫格式的注入名稱
70+ - ` Dummies ` : 替換為複數型態,開頭大寫格式的注入名稱
71+ - ` dummy ` : 替換為小寫格式的注入名稱
72+ - ` dummies ` : 替換為複數型態,小寫格式的注入名稱
73+
74+ 處理的原始碼可參考 [ StubGeneratorCommand::replaceClass()] ( https://github.com/A2Workspace/laravel-stubs/blob/1.0.0/src/Commands/StubGeneratorCommand.php#L258 ) 。
75+
76+ 詳細則可參考 ` resources/stubs ` 下預設的模板。
Original file line number Diff line number Diff line change 44
55use App \Models \Dummy ;
66use App \Http \Resources \Dummy as DummyResource ;
7+ use App \Http \Controllers \Controller ;
78use Illuminate \Http \Request ;
89
910class DummyController extends Controller
Original file line number Diff line number Diff line change 22
33namespace App \Models ;
44
5+ use Database \Factories \DummyFactory ;
56use Illuminate \Database \Eloquent \Model ;
67use Illuminate \Database \Eloquent \Factories \HasFactory ;
78
89class Dummy extends Model
910{
1011 use HasFactory;
1112
13+ /**
14+ * The table associated with the model.
15+ *
16+ * @var string
17+ */
18+ protected $ table = 'dummies ' ;
19+
1220 /**
1321 * The attributes that are mass assignable.
1422 *
@@ -30,6 +38,16 @@ class Dummy extends Model
3038 // ...
3139 ];
3240
41+ /**
42+ * Create a new factory instance for the model.
43+ *
44+ * @return \Illuminate\Database\Eloquent\Factories\Factory
45+ */
46+ protected static function newFactory ()
47+ {
48+ return DummyFactory::new ();
49+ }
50+
3351 /* =========================================================================
3452 * = Scopes
3553 * =========================================================================
You can’t perform that action at this time.
0 commit comments