Skip to content

Commit 40533a4

Browse files
authored
增加如何編寫類別模板說明 (#5)
* 調整範例 * 編寫 Development 說明
1 parent 5d227f9 commit 40533a4

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

README.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,70 @@
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
1920
composer config repositories.a2workspace/laravel-stubs vcs https://github.com/A2Workspace/laravel-stubs.git
2021
composer 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
2837
php 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` 下預設的模板。

publishes/resources/ExampleController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Dummy;
66
use App\Http\Resources\Dummy as DummyResource;
7+
use App\Http\Controllers\Controller;
78
use Illuminate\Http\Request;
89

910
class DummyController extends Controller

publishes/resources/ExamplePack/Model.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace App\Models;
44

5+
use Database\Factories\DummyFactory;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Eloquent\Factories\HasFactory;
78

89
class 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
* =========================================================================

0 commit comments

Comments
 (0)