novadaemon/larafeat

Laravel package to implements Domain Driven Design pattern. Inspired by lucidarch/lucid

v1.0.1 2023-06-15 21:03 UTC

This package is auto-updated.

Last update: 2024-04-15 22:47:52 UTC


README

Larafeat is a simplified modification of the Lucidarch. It allows you to build and serve functionalities in your Laravel application.

Instalation

You can install the package via composer:

composer require novadaemon/larafeat

This package supports Laravel 9 and Laravel 10.

Usage

Create a new feature using the command make:feature

php artisan make:feature MyAwesomeFeature

You can write the name without the "Feature" prefix, the command prepend it to the class and file names.

php artisan make:feature MyAwesome

The above command create the file MyAwesomeFeature class in the directory app/Features.

<?php

namespace App\Features;

use Illuminate\Http\Request;
use Novadaemon\Larafeat\Feature;

class MyAwsomeFeature extends Feature
{
    public function handle(Request $request)
    {
        $order = $request->input('order');

        //...
    }
}

Also, the command generates the file MyAwesomeFeatureTest in the tests directory.

<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Features\MyAwsomeFeature;

class MyAwsomeFeatureTest extends TestCase
{
    public function test_my_awsome_feature()
    {
        $this->markTestIncomplete();
    }
}

The make:fature command accept the --pest option. If this option is present, the file create for test will be a Pest file.

<?php

namespace Tests\Feature;

test('example', function () {
    $response = $this->get('/');

    $response->assertStatus(200);
});

Usage

To start serves features, you must extends your controller from Larafeat controller.

<?php

namespace App\Controllers;

use Novadaemon\Larafeat\Controllers\Controller;
use App\Features\MyAwesomeFeature;

class MyController extends Controller
{
    public function get()
    {
        return $this->serve(MyAwesomeFeature::class);
    }
}

You can define class properties in the Feature constructor, and then, you can pass then in the arguments parameter of the serve method.

<?php

namespace App\Features;

use Illuminate\Http\Request;
use Novadaemon\Larafeat\Feature;

class MyAwsomeFeature extends Feature
{
    public function __construct(private string $name)
    {
    }

    public function handle(Request $request)
    {
        $name = $this->name;

        //...
    }
}
<?php

namespace App\Controllers;

use Novadaemon\Larafeat\Controllers\Controller;
use App\Features\MyAwesomeFeature;

class MyController extends Controller
{
    public function get()
    {
        return $this->serve(MyAwesomeFeature::class, ['name' => 'Jesús']);
    }
}

Inside the feature yo can disptach Laravel Jobs using the run method.

<?php

namespace App\Features;

use App\Jobs\GreetingJob;
use Illuminate\Http\Request;
use Novadaemon\Larafeat\Feature;

class MyAwsomeFeature extends Feature
{
    public function __construct(private string $name)
    {
    }

    public function handle(Request $request)
    {
        return $this->run(new GrettingJob($this->name));
    }
}

Contributing

Contributing is pretty chill and is highly appreciated! Just send a PR and/or create an issue!

Credits

License

The MIT License (MIT). Please see License File for more information.