heartbeat/loris

A ReactPHP API Application Template in order to create very fast APIs!

1.0.0 2020-12-14 11:08 UTC

This package is auto-updated.

Last update: 2024-03-28 16:06:05 UTC


README

A ReactPHP API Application Template in order to create very fast APIs!

Latest Stable Version Total Downloads Latest Unstable Version License

Heartbeat Loris

Loris is one of the slowest animals alive, the opposite of this library!

Includes:

  • Action based Routing (route XYZ should open Action XYZ)
  • CORS Middleware.
  • DI Injection and Container Defintion.
  • HTTP Browser to fetch data from another API.
  • Helpers to work with JSON Data.

Installation

The package is only available trough composer

composer require heartbeat/loris

Application

Create an index.php file which holds the LORIS Application:

<?php

use Heartbeat\Loris\Application;

require_once '../vendor/autoload.php';

$app = new Application('0.0.0.0:8001');
$app->addAction(MyAction::class);
// builds the server, register informations
$app->server();
// start the loop run
$app->run();

Create the MyAction Action:

<?php

namespace App\Actions;

use Heartbeat\Loris\Action;
use Heartbeat\Loris\Response\JsonResponse;

class MyAction extends Action
{
    public function route(): string
    {
        return '/my-super-action';
    }

    public function run(array $params)
    {
        return new JsonResponse(200, [
            'message' => [
                'hello world!'
            ]    
        ]);
    }
}

Or an example which generates an action which makes another call to an API:

<?php

namespace App\Actions;

use Heartbeat\Loris\Action;
use Heartbeat\Loris\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Clue\React\Buzz\Browser;

class MyAction extends Action
{
    public function route(): string
    {
        return '/my-super-action';
    }

    public function run(array $params)
    {
        $client = new Browser($this->loop);
        return $client->get('https://api.example.com')->then(function(ResponseInterface $reponse) {
            // create the response from 
            $content = (string) $reponse->getBody();

            // ... do something with content :-)
        
            return new JsonResponse(200, ['message' => $content]);
        });
    }
}

In the action you can do whatever you like, in our example we gatter informations from an api and return those informations.

You are now able to access localhost:8001/my-upser-action which returns the content from the action above.

Using DI

In order to register and reuse any objects inside your application you can use the following simple di mechanism:

$app = new Application('0.0.0.0:8001');
$app->addDefintion(MyDiExample::class, function() {
    return New MyDiExample();
});

now you are able to access the di defintion in the actions trough construct param:

class TestAction extends Action
{
    public function __construct(MyDiExample $myDiExample)
    {
        var_dump($myDiExample);
    }
}