Skip to content

Provide a convenient way creating single action handlers. The idea of a action handler is a single action controller that means a unique class handles each action

Notifications You must be signed in to change notification settings

patiencemanzen/patienceman-custom-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CustomHandler | Patienceman

Provide a convenient way creating single action handlers. The idea of a action handler is a single action controller that means a unique class handles each action

Installation

Install the package doesn't require much requirement except to use the following command in the laravel terminal, and you're good to go.

composer require patienceman/custom-handler

Usage

To start working with handler, u need to run command 🎉 in your custom directories:

php artisan make:handler NewStartupHandler

so it will create the filter file for u, Just in

    App\Handlers
namespace App\Handlers;

use Patienceman\CustomHandler\Handler;

class NewStartupHandler extends Handler {
    /**
     * Custom execution from Handler Pipeline
     * @return Exception|void
     */
    public function execute() {
        // do whatever action inside handler
    }
}

So you may want even to specify the custom path for your Handler, Just relax and add it in front of your Handler name. Let's take again our current example.

php artisan make:handler Model/DatabaseHandler

So far so good, Let see how you can your handlers anywhere in controller or services or ...:

namespace App\Http\Controllers;

use App\Handlers\NewStartupHandler;
use Patienceman\CustomHandler\CustomHandler;

class TestBuilderController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $handler = CustomHandler::handle(new NewStartupHandler());
    }
}

But don't worry, you chain many handlers as you want, what you need to do is keep add ->handle(), let see it in action:

namespace App\Http\Controllers;

use App\Handlers\NewStartupHandler;
use App\Handlers\NewCompanyHandler;
use Patienceman\CustomHandler\CustomHandler;

class TestBuilderController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $handler = CustomHandler::handle(new NewStartupHandler())
                                ->handle(new NewCompanyHandler());
    }
}

And now once your controller get execution, all your handler will run.

Exchanges of data

There might a time you need to store and exchange tou data throughout your handlers, This package provide convinient way to do so, by just use ->collect(array data) to collect data and ->collection() to get collected data, let take example:

In our NewStartupHandler:

namespace App\Handlers;

use Patienceman\CustomHandler\Handler;
use App\Models\Startup;

class NewStartupHandler extends Handler {
    /**
     * Custom execution from Handler Pipeline
     * @return Exception|void
     */
    public function execute() {
        $startup = Startup::create([ 'name' => "MorganTv" ]);
        $this->collect([ 'startup' => $startup ]);
    }
}

That look to clear 🎉, so Let see how you can call the collected data anywhere your want, for example in our NewCompanyHandler class:

namespace App\Handlers;

use Patienceman\CustomHandler\Handler;

class NewCompanyHandler extends Handler {
    /**
     * Custom execution from Handler Pipeline
     * 
     * @return Exception|void
     */
    public function execute() {
        $startup = $this->collection()->get('startup');
    }
}

🚨 You've seen where we are using ->collection() function, and this is coming from Laravel collection, you can read more about it to their website: Laravel collection avalibale method for more about getting and filtering data.

There is also another way to get collected data, for instance when you're outside handler, and to apply this you use same functionality as ->collection() after your handler, take a look:

namespace App\Http\Controllers;

use App\Handlers\NewStartupHandler;
use App\Handlers\NewCompanyHandler;
use Patienceman\CustomHandler\CustomHandler;

class TestBuilderController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $handler = CustomHandler::handle(new NewStartupHandler())
                                ->handle(new NewCompanyHandler())
                                ->collection()
                                ->get('startup') // or ->get(), ->filter() ->sort();
    }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Connect with Me

Linkedin Github Instagram Twitter

About

Provide a convenient way creating single action handlers. The idea of a action handler is a single action controller that means a unique class handles each action

Topics

Resources

Stars

Watchers

Forks

Languages