PHP code example of guennichi / performist

1. Go to this page and download the library: Download guennichi/performist 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/ */

    

guennichi / performist example snippets


class RegisterUser
{
    protected string $username;
    protected string $password;
    
    public function __construct(string $username, string $password) 
    {
        $this->username = $username;
        $this->password = $password;
    }
    
   public  function getUsername(): string
   {
        return $this->username;
   }
   
   public  function getPassword(): string
   {
        return $this->password;
   }
}

class RegisterUserHandler implements \Guennichi\Performist\HandlerInterface
{
    // Inject DB and other services here...
    
    public function __invoke(RegisterUser $action)
    {
        $user = new User($action->getUsername(), $action->getPassword());
        
        // Encode password
        
        $this->repository->add($user);
        
        // Send email notification etc...
        
        return $user;
    }
}

// Register actions/handlers
$registry = new \Guennichi\Performist\Registry();
$registry->add(RegisterUser::class, new RegisterUserHandler());
$registry->add(OtherAction::class, new OtherActionHandler());

// Instantiate the performer.
$performer = new \Guennichi\Performist\Performer($registry, new \Guennichi\Performist\HandlerPeeler());

// Do the job
$user = $performer->perform(new RegisterUser('[email protected]', 'password'), [
    new DoctrineTransactionMiddleware(),
    new LoggerMiddleware(),
    // ...
]);

// Generic job
$result = $performer->perform(new OtherAction(/** some data */), [
    new BetweenMiddleware(),
    new BeforeMiddleware(),
    new AfterMiddleware() 
]);

class MyMiddleware implements \Guennichi\Performist\MiddlewareInterface
{
    public function handle($action, Closure $next)
    {
        // Do something here before performing the action...
        // ...
        
        $result = $next($action);
        
        // Do something here after performing the action...
        // ...
        
        return $result;
    }
}

$result = $performer->perform(new MyAction(/** some data */), [
    new MyMiddleware()
]);