tricioandrade/oneshot

Laravel artisan extra commands

v3.3.3 2024-05-01 09:39 UTC

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