2017 © Pedro Peláez
 

library photon

Lumen Extension for SRP (Single Responsibility Principle)

image

moeen-basra/photon

Lumen Extension for SRP (Single Responsibility Principle)

  • Monday, May 28, 2018
  • by moeen-basra
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Photon

An Extension to Laravel with SRP Design Pattern., (*1)

How to use

install the package using the following command, (*2)

composer require moeen-basra/photon

Open the file public/index.php, (*3)

Replace the following code, (*4)

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

with, (*5)

$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, (*6)

use Illuminate\Routing\Controller as BaseController;, (*7)

with, (*8)

use Photon\Foundation\Controller as BaseController;, (*9)

Finally you can extends the app\Exceptions\Handler with the following class, (*10)

\Photon\Foundation\Exceptions\Handler\Handler, (*11)

or use the following traits in your existing exception handler, (*12)

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

and in the render method run the following job if request accepts application\json, (*13)

$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., (*14)

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

Here is a sample code for a controller serving the feature., (*15)

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, (*16)

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

The Versions

28/05 2018

dev-master

9999999-dev

Lumen Extension for SRP (Single Responsibility Principle)

  Sources   Download

MIT

The Requires

 

by Moeen Basra

laravel lumen photon