1. Go to this page and download the library: Download deefour/interactor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
deefour / interactor example snippets
use Deefour\Interactor\Interactor;
class CreateCar extends Interactor
{
public function call()
{
$c = $this->context();
$c->car = new Car([ 'make' => $c->make, 'model' => $c->model ]);
if ( ! $c->car->save()) {
$this->fail('Creating the car failed!');
}
}
}
$car = new Car;
$permitted = $this->context()->only($this->car->getFillable());
$car->fill($permitted);
$car->save();
public function __construct(array $attributes = [])
{
$this->attributes = $attributes;
}
use Deefour\Interactor\Context;
class CarContext extends Context
{
/**
* The owner of the vehicle.
*
* @var User
*/
public $user;
/**
* Constructor.
*
* @param User $user
* @param array $attributes
*/
public function __construct(User $user, array $attributes = [])
{
$this->user = $user;
parent::__construct($attributes);
}
}
public function __construct(CarContext $context)
{
parent::__construct($context);
}
public function create(CreateRequest $request)
{
$context = new CarContext($request->user(), $request->only('make', 'model'));
(new CreateCar($context))->call();
if ($context->ok()) {
echo 'Wow! Nice new ' . $context->car->make;
} else {
echo 'ERROR: ' . $context->status()->error();
}
}
namespace App\Controllers;
use App\Interactors\CreateCar;
use App\Contexts\CarContext;
use Deefour\Interactor\DispatchesInteractors;
class CarController extends BaseController
{
use DispatchesInteractors;
/**
* Create a new car.
*
* @param Request $request
* @return string
*/
public function store(Request $request)
{
$context = $this->dispatchInteractor(
CreateCar::class,
CarContext::class,
array_merge([ 'user' => $request->user() ], $request->only('make', 'model'))
);
if ($context->ok()) {
return 'Wow! Nice new ' . $context->car->make;
} else {
return 'ERROR: ' . $context->status()->error();
}
}
}
use Deefour\Interactor\Organizer;
class RegisterUser extends Organizer
{
public function __construct(RegisterUserContext $context)
{
parent::__construct($context);
}
public function organize()
{
$this->enqueue(function ($context) {
return new CreateUser(
new CreateUserContext($context->user['first_name'], $context->user['last_name'])
);
});
$this->enqueue(function ($context, $previous) {
return new CreateVehicle(
new CreateVehicleContext($previous->user, $context->vin)
);
});
}
}
namespace App\Jobs;
use App\Car;
use App\Contexts\CreateCarContext as CarContext;
use Deefour\Interactor\Interactor;
use Illuminate\Contracts\Redis\Database as Redis;
class CreateCar extends Interactor
{
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(CarContext $context)
{
parent::__construct($context);
}
/**
* Execute the command.
*
* @return void
*/
public function handle(Redis $redis)
{
$c = $this->context();
$c->car = Car::create($c->only('make', 'model'));
$redis->publish('A new' . (string)$c->car . ' was just added to the lot!');
return $this->context();
}
}
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Deefour\Interactor\DispatchesInteractors;
class Controller
{
use DispatchesJobs, DispatchesInteractors {
DispatchesJobs::dispatch insteadof DispatchesInteractors;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.