ahmard/preact

Performs action before-hand, confirms action execution.

1.0.3 2020-10-23 00:41 UTC

This package is auto-updated.

Last update: 2024-11-23 09:38:10 UTC


README

Performs action before-hand, confirms action execution.
This program uses ReactPHP Promise for its promise implementation.

Installation

composer require ahmard/preact

Usage

  1. Event
  2. Preact

Event

An event system for simple event-driven programming.

use Preact\Event;

$event = new Event();

$event->on('user.created', function ($user){
    echo "User created: {$user['name']}";
});

$user = [
    'id' => 1,
    'name' => 'Admin'
];
$event->emit('user.created', [$user]);

You can use Preact\EventTrait trait directly in your class and have the functionality embedded in your code.

namespace App\User;

use Preact\EventTrait;

class User
{
    use EventTrait;
    
    public function create(array $userInfo)
    {
        //Save in DB
        $this->emit('created', [$userInfo]);
    }
}

$user = new User;

$user->on('created', function ($user){
    echo "User created: {$user['username']}\n";
});

$user->create([
    'username' => 'Admin',
    'email' => 'admin@test.local'
]);

Preact

Have confirmation before execution.

use React\Promise\PromisorInterface;
use Preact\PreactTrait;

class Animal
{
    use PreactTrait;
}

$animal = new Animal();

$animal->onPreact('can.create', function (PromisorInterface $promisor, $animalInfo){
    if($animalInfo['name'] == 'lion'){
        $promisor->resolve(true);
    }else{
        $promisor->reject(false);
    }
});

$animal->preact('can.create', ['lion'])
    ->then(function (){
        echo 'Animal creation allowed: lion';
    })
    ->otherwise(function (){
        echo 'Animal creation rejected: lion.\n';
    });

To see more use cases view examples.

Licence

Preact is MIT licenced.