williamjss / layers
用于生成分层架构文件的 Laravel 扩展包
v1.3.3
2023-09-22 23:33 UTC
Requires
- php: ^8.0.2
- illuminate/console: ^9.0 || ^10.20
- illuminate/support: ^9.0 || ^10.20
- symfony/finder: ^6.3
README
用于生成分层架构文件和自动化接口绑定的 Laravel 扩展包。
推荐 Laravel 版本: ^9.0
前往 Laravel 文档 查看支持策略。
摘要
需求
"php": "^8.0.2" "symfony/finder": "^6.3" "illuminate/support": "^9.0 || ^10.20" "illuminate/console": "^9.0 || ^10.20"
安装
composer require williamjss/layers --dev
配置
php artisan vendor:publish --tag=layers
此命令将复制 Layers 配置到您的项目配置文件夹
<?php return [ 'namespace' => [ 'repositories' => 'Repositories', 'services' => 'Services', ], 'path' => [ 'models' => app_path('Models'), 'repositories' => app_path('Repositories'), 'services' => app_path('Services'), ] ];
在此文件中,您可以切换服务/存储库的默认路径。为此,请保持命名空间和路径键都相等。
使用
使用 layers
artisan 命令,我们可以为存储库(接口和 Eloquent)和服务生成文件。
php artisan layers
+ {option}
+ {模型名称}
可用选项
- -e 或 --eloquent : 为模型生成仓库 Eloquent
- -i 或 --interface : 为模型生成仓库接口
- -s 或 --service : 为模型生成服务
- -r 或 --repository : 为模型生成仓库接口和 Eloquent
- -a 或 --all : 为模型生成服务、仓库接口和仓库 Eloquent
- --wr : 指定服务的存储库
子命令
php artisan layers:repository --eloquent
: 与 php artisan layers --eloquent 相同php artisan layers:repository --interface
: 与 php artisan layers --interface 相同php artisan layers:service
: 与 php artisan layers --service 相同php artisan layers:binds
: 列出应用程序中的所有绑定
生成分层
php artisan layers --all User
此命令将生成 3 个文件
- app/Repositories/UserRepositoryInterface.php
- app/Repositories/UserRepositoryEloquent.php
- app/Services/UserService.php
UserRepositoryInterface.php
<?php namespace App\Repositories\User; use App\Models\User; interface UserRepositoryInterface { public function __construct(User $user); /** * Stores a new instance of User in the database * @param \Illuminate\Support\Collection|array|int|string $data * @return User */ public function store($data); /** * Returns all instances of User from the database * @param array|string $columns * @param array<array> $filters * @return \Illuminate\Database\Eloquent\Collection<int, static> */ public function getList($columns=['*'], $filters=null); /** * Returns an instance of User from the given id * @param int|string $id * @return User */ public function get($id); /** * Updates the data of an instance of User * @param \Illuminate\Support\Collection|array|int|string $data * @param int|string $id * @return User */ public function update($data, $id); /** * Removes an instance of User from the database * @param int|string $id * @return int */ public function destroy($id); }
UserRepositoryEloquent.php
<?php namespace App\Repositories; use App\Models\User; class UserRepositoryEloquent implements UserRepositoryInterface { protected $user; public function __construct(User $user) { $this->user = $user; } /** * Stores a new instance of User in the database * @param \Illuminate\Support\Collection|array|int|string $data * @return User */ public function store($data) { return $this->user->create($data); } /** * Returns all instances of User from the database * @param array|string $columns * @param array<array> $filters * @return \Illuminate\Database\Eloquent\Collection<int, static> */ public function getList($columns=['*'], $filters=null) { return $this->user->where($filters)->get($columns); } /** * Returns an instance of User from the given id * @param int|string $id * @return User */ public function get($id) { return $this->user->find($id); } /** * Updates the data of an instance of User * @param \Illuminate\Support\Collection|array|int|string $data * @param int|string $id * @return User */ public function update($data, $id) { $user = $this->user->find($id); $user->update($data); return $user; } /** * Removes an instance of User from the database * @param int|string $id * @return int */ public function destroy($id) { return $this->user->find($id)->delete(); } }
UserService.php
<?php namespace App\Services; use App\Repositories\UserRepositoryInterface; class UserService { private $repoUser; public function __construct( UserRepositoryInterface $repoUser, ) { $this->repoUser = $repoUser; } // Add your functions here... }
在子文件夹中生成分层
php artisan layers --repository User.Address
此命令将生成 2 个文件
- app/Repositories/User/AddressRepositoryInterface.php
- app/Repositories/User/AddressRepositoryEloquent.php
为多个存储库生成服务
php artisan layers --service --wr=Address --wr=User Person
此命令将生成以下文件
<?php namespace App\Services; use App\Repositories\UserRepositoryInterface; use App\Repositories\User\AddressRepositoryInterface; class PersonService { private $repoAddress; private $repoUser; public function __construct( AddressRepositoryInterface $repoAddress, UserRepositoryInterface $repoUser, ) { $this->repoAddress = $repoAddress; $this->repoUser = $repoUser; } // Add your functions here... }