moeen-basra / photon by moeen-basra

Laravel Extension for SRP (Single Responsibility Principle)
10
3
3
Package Data
Maintainer Username: moeen-basra
Maintainer Contact: m.basra@live.com (Moeen Basra)
Package Create Date: 2018-05-27
Package Last Update: 2024-11-06
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:22:58
Package Statistics
Total Downloads: 10
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 3
Total Watchers: 3
Total Forks: 1
Total Open Issues: 2

Photon

An Extension to Laravel with SRP Design Pattern.

How to use

install the package using the following command

composer require moeen-basra/photon

Open the file public/index.php

Replace the following code

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

with

$app->alias('request', \Photon\Foundation\Http\Request::class);

$response = $kernel->handle(
    $request = \Photon\Foundation\Http\Request::capture()
);

Open the class App\Http\Controllers\Controller and replace the following code

use Illuminate\Routing\Controller as BaseController;

with

use Photon\Foundation\Controller as BaseController;

Finally you can extends the app\Exceptions\Handler with the following class

\Photon\Foundation\Exceptions\Handler\Handler

or use the following traits in your existing exception handler

use Photon\Foundation\Traits\MarshalTrait;
use Photon\Foundation\Traits\JobDispatcherTrait;

and in the render method run the following job if request accepts application\json

$message = $exception->getMessage();
$class = get_class($exception);
$code = $exception->getCode();

if ($request->expectsJson()) {
    return $this->run(JsonErrorResponseJob::class, [
        'message' => $message,
        'code' => $class,
        'status' => ($code < 100 || $code >= 600) ? 400 : $code,
    ]);
}

Now you can create the following directories in the app folder.

|-- app
|  |-- Domains
|  |-- Features
|  |-- Operations

Here is a sample code for a controller serving the feature.

namespace App\Http\Controllers\Api\Auth;

use Illuminate\Http\JsonResponse;
use Photon\Foundation\Controller;
use App\Features\Api\Auth\RegisterFeature;

class AuthController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:api', ['only' => 'me', 'logout', 'refresh']);
    }

    /**
     * Register user
     *
     * @return JsonResponse
     */
    public function register(): JsonResponse
    {
        return $this->serve(RegisterFeature::class);
    }
}

Here is sample code for Feature running the job

namespace App\Features\Api\Auth;

use Photon\Foundation\Feature;
use App\Operations\Auth\RegisterOperation;
use Photon\Domains\Http\Jobs\JsonResponseJob;
use App\Domains\Auth\Jobs\Register\ValidateRegisterRequestJob;

class RegisterFeature extends Feature
{
    public function handle()
    {
        $input = $this->run(ValidateRegisterRequestJob::class);

        $data = $this->run(RegisterOperation::class, compact('input'));

        return $this->run(new JsonResponseJob($data));
    }
}