tricioandrade / oneshot
Laravel artisan 扩展命令
v3.3.3
2024-05-01 09:39 UTC
Requires
- php: >=8.2.0
Requires (Dev)
- illuminate/console: v10.21.0
- illuminate/support: v10.21.0
- dev-master
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.5
- v3.2.4
- v3.1.4
- v3.1.3
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.3.8
- v2.3.7
- v2.2.7
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.0
- v1.2.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- dev-task_oneshot_3.3.1
- dev-task_oneshot_v3.3.0
- dev-task_oneshot_v3.1
- dev-task_oneshot_v3.0
- dev-task_oneshot_v2.1
- dev-task_oneshot_v2.0
- dev-task_oneshot_v1.2
This package is auto-updated.
Last update: 2024-10-01 00:17:09 UTC
README
Laravel artisan 扩展命令
"OneShot" 是 Laravel 项目中的一个开发包,特别是用于 API。它是一个 资源 生成器,用于控制器、资源、请求、模型、迁移、特性和枚举(PHP 8.1)。
安装
打开您的终端并运行
composer require tricioandrade/oneshot
生成您的文件
创建 枚举 文件,您的文件将在您的 Laravel 项目的 app/Enum 文件夹中创建
枚举
php artisan make:enum EmployeeFunctions
将创建 EmployeeFuncionsEnum.php 文件,如下所示
EmployeeFunctionsEnum.php
<?php namespace App\Enums; enum EmployeeFunctions { /** * Return all cases values as array * * @return array */ public function values(): array { return array_column(self::cases(), 'values'); } }
特性
对于 特性 文件,您的文件将在您的 Laravel 项目的 app/Traits 文件夹中创建。
php artisan make:trait EmployeeFunctions
将创建 EmployeeFuncions.php 文件,如下所示
EmployeeFunctions.php
<?php namespace App\Traits; trait EmployeeFunctions { // }
服务
如果您想创建 服务,也可以这样做。但这个模板需要一个模型。例如这个示例
php artisan make:service EmployeeFunctionsService
将创建 EmployeeFunctionsService.php 文件,如下所示
app/Services/EmployeeFunctionsService.php
导入的类
use App\Models\EmployeeFunctionsService\EmployeeFunctionsServiceModel
导入模型 EmployeeFunctionsServiceModel 和其他类是可选的,创建 服务 后将不存在。您可以按需修改代码。在
stubs\create.service.stub
<?php namespace App\Services; use App\Exceptions\Auth\UnauthorizedException; use App\Models\Transport\FuelSupplyModel; use App\Traits\Essentials\Database\CrudTrait; use App\Traits\Essentials\VerifyTypeUserTrait; use Illuminate\Database\Eloquent\Collection; class FuelSupplyService { use CrudTrait, VerifyTypeUserTrait; public function __construct() { $this->relations = []; $this->model = new FuelSupplyModel(); } /** * Get all data from the database * * @throws UnauthorizedException */ public function getAll(): FuelSupplyModel|Collection { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->getAllData(); } /** * Create a new data in the database * * @throws UnauthorizedException */ public function create(array $attributes) { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->createData($attributes); } /** * Get a data from the database by id * * @param int $id * @return FuelSupplyModel|Collection * @throws UnauthorizedException */ public function getById(int $id): FuelSupplyModel|Collection { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->getByIdentity($id); } /** * Update a specific data in the database * * @param array $attributes * @param int $id * @return FuelSupplyModel|Collection * @throws UnauthorizedException */ public function update(array $attributes, int $id): FuelSupplyModel|Collection { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->updateData($attributes, $id); } /** * Trash a specified data in the database * * @param int $id * @return mixed * @throws UnauthorizedException */ public function delete(int $id): mixed { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->deleteData($id); } /** * Permanently delete a specific data in the database * * @param int $id * @return mixed * @throws UnauthorizedException */ public function forceDelete(int $id): mixed { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->forceDeleteData($id); } /** * Restore a specific data in the database * * @param int $id * @return mixed * @throws UnauthorizedException */ public function restore(int $id): mixed { if (!$this->verifyAdmin()) throw new UnauthorizedException(); return $this->restoreData($id); } }
API 资源
对于 资源,这有点奇怪
php artisan make:api-resources User/Employee
将创建一些资源文件,如下所示
1. 控制器
Oneshot 定制的控制器文件
app/Http/Controllers/User/EmployeeController.php
2. 请求
默认的 Laravel 请求文件
app/Http/Requests/User/EmployeeRequest.php
3. 资源
默认的 Laravel 资源文件
app/Http/Resource/User/EmployeeResource.php
4. 模型
默认的 Laravel 模型对象及其迁移在数据库/migrations 文件夹中
app/Models/User/EmployeeModel.php
对于那些喜欢节省时间的人来说,这个包的这个最后功能怎么样?
您可以查看生成的控制器的外观
<?php namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; use App\Http\Requests\User\EmployeeRequest; use App\Http\Resources\User\EmployeeResource; use App\Services\User\EmployeeService; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; class EmployeeController extends Controller { public function __construct( public EmployeeService $employeeService ){} /** * Display a listing of the resource. * * @return AnonymousResourceCollection * @throws UnauthorizedException */ public function index(): AnonymousResourceCollection { return EmployeeResource::collection($this->employeeService->getAll()); } /** * Store a newly created resource in storage. * * @param EmployeeRequest $employeeRequest * @return EmployeeResource * @throws UnauthorizedException */ public function store(EmployeeRequest $employeeRequest): EmployeeResource { $employeeRequest->validated($employeeRequest->all()); $employee = $this->employeeService->create($employeeRequest->all()); return new EmployeeResource($employee); } /** * Display the specified resource. * * @param int $id * @return EmployeeResource * @throws UnauthorizedException */ public function show(int $id): EmployeeResource { $employee = $this->employeeService->getById($id); return new EmployeeResource($employee); } /** * Update the specified resource in storage. * * @param EmployeeRequest $employeeRequest * @param int $id * @return EmployeeResource * @throws UnauthorizedException */ public function update(EmployeeRequest $employeeRequest, int $id): EmployeeResource { $employeeRequest->validated($employeeRequest->all()); $employee = $this->employeeService->getById($id); return new EmployeeResource($employee); } /** * Remove the specified resource from storage. * * @param int $id * @return mixed * @throws UnauthorizedException */ public function destroy(int $id): mixed { return $this->employeeService->delete($id); } /** * Remove the specified resource from storage. * * @param int $id * @return mixed * @throws UnauthorizedException */ public function forceDelete(int $id): mixed { return $this->employeeService->forceDelete($id); } /** * Restore the specified resource from storage. * * @param int $id * @return mixed * @throws UnauthorizedException */ public function restore(int $id): mixed { return $this->employeeService->restore($id); } }
新
CrudTrai.php added
<?php /** * From OneShot v3 * @link https://github.com/tricioandrade/oneshot */ namespace App\Traits\Essentials\Database; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; trait CrudTrait { private array | string $relations; private Model | Builder $model; /** * Get all data * * @return mixed */ private function getAllData(): mixed { return $this->model::withTrashed()->with($this->relations)->get(); } /** * Get data by Id * * @param int $id * @return mixed */ private function getByIdentity( int $id): mixed { return $this->model::withTrashed()->with($this->relations)->findOrFail($id); } /** * Get data by slug * * @param string $slug * @return mixed */ private function getBySlugInfo(string $slug): mixed { return $this->model::withTrashed()->with($this->relations)->where('slug', $slug)->get(); } /** * Get data by anonymous row * * @param string $anonymousRow * @param $value * @return mixed */ private function getByAnonymousInfo(string $anonymousRow, $value): mixed { return $this->model::withTrashed()->with($this->relations)->where($anonymousRow, $value)->get(); } /** * Create data * * @param array $attributes * @return mixed */ private function createData(array $attributes): mixed { $create = $this->model::create($attributes); return $create->load($this->relations); } /** * Update data * * @param int $id * @param array $attributes * @return mixed */ private function updateData(array $attributes, int $id): mixed { $update = $this->getByIdentity($id); $update->update($attributes); return $update->load($this->relations); } /** * Delete data | put on trash * * @param int $id * @return mixed */ private function deleteData(int $id): mixed { $target = $this->getByIdentity($id); return $target->delete($id); } /** * Delete data permanently * * @param int $id * @return mixed */ private function forceDeleteData(int $id): mixed { $target = $this->getByIdentity($id); return $target->forceDelete($id); } /** * Restore data from database * * @param int $id * @return mixed */ private function restoreData(int $id): mixed { $target = $this->getByIdentity($id); return $target->restore($id); } }