kampernet / action-chain
Action chain helper classes for implementing commands
Installs: 1 031
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
This package is not auto-updated.
Last update: 2024-09-25 12:14:51 UTC
README
This is a simple, dependency free implementation of the Command Pattern in PHP. Useful for creating an orchestration layer
Usage
This will create a group of Commands with a certain name.
class RegisterAction extends ActionChain {
public function __construct() {
$this->add(new RegisterUserAction());
$this->add(new SendWelcomeEmailAction());
}
}
These are the individual command examples
class RegisterUserAction extends Command {
private $request;
private $response;
public function execute($request, $response = null) {
$this->request = $request;
$this->response = $response;
// eg: code to take the request data and persist the user
// probably more likely to call a service or domain model method here.
}
public function undo() {
// eg: code to "undo" anything this command would have done
}
}
Once your commands are built, you can just call them from the routes: ( example using Symfony's Request )
Route::get('/register', function() {
$response = new Response();
$reg = new RegisterAction();
if (!$reg->execute(Request::createFromGlobals(), $response)) {
$reg->undo();
}
return $response;
});