承接 tricioandrade/oneshot 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tricioandrade/oneshot

Composer 安装命令:

composer require tricioandrade/oneshot

包简介

Laravel artisan extra commands

README 文档

README

Laravel artisan extra commands

Latest Stable Version Total Downloads License PHP Version Require

"OneShot" is a development package in Laravel projects, particularly for APIs. It is a resource generator for items such as controllers, resources, requests, models, migrations, traits, and enums (PHP 8.1).

Installation

Open your terminal and run:

composer require tricioandrade/oneshot

Generate your files

Create Enum files, your file will be created at app/Enum in yor Laravel project

Enum

php artisan make:enum EmployeeFunctions

Will create EmployeeFuncionsEnum.php file, like this:

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');
    }
}

Traits

The same for Traits files, your file will be created at app/Traits in your Laravel project.

php artisan make:trait EmployeeFunctions

Will create EmployeeFuncions.php file, like this:

EmployeeFunctions.php
<?php

namespace App\Traits;

trait EmployeeFunctions
{
    //
}

Services

If you like to create services, you can also do do the same. But his template requires a model. Like this example:

php artisan make:service EmployeeFunctionsService

Will create EmployeeFunctionsService.php file, like this:

app/Services/EmployeeFunctionsService.php

The imported class:

use App\Models\EmployeeFunctionsService\EmployeeFunctionsServiceModel

Importing the model EmployeeFunctionsServiceModel and other classes is optional, it will not exist after creating the service. You can adapt the code however you want. Setup his template as you wish at:

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);
    }
}

Resources for APIs

For resources, this is a bit weird:

php artisan make:api-resources User/Employee

Will create some resources files like:

1. Controller

Oneshot customized controller file:
app/Http/Controllers/User/EmployeeController.php

2. Request

Default laravel request file:
app/Http/Requests/User/EmployeeRequest.php

3. Resource

Default laravel resource file:
app/Http/Resource/User/EmployeeResource.php

4. Model

Default laravel Model object and his migration at database/migrations folder:
app/Models/User/EmployeeModel.php

For those who like to save time, how about this last feature in this package?

You can see how the generated controller looks like:

<?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);
    }
}

New

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);
    }
}

tricioandrade/oneshot 适用场景与选型建议

tricioandrade/oneshot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 86 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tricioandrade/oneshot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tricioandrade/oneshot 我们能提供哪些服务?
定制开发 / 二次开发

基于 tricioandrade/oneshot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 86
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-01