PHP Classes

How to Implement a PHP Async API using Sapphire: Build an API that handles request asynchronously

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 38 All time: 10,935 This week: 69Up
Version License PHP version Categories
sapphire 1.0GNU General Publi...5PHP 5, Web services, Language
Description 

Author

This package can build an API that handles requests asynchronously.

It uses ReactPHP to listen to HTTP connections of requests sent to an API defined by the API application.

The package can take a dispatcher object created by the application to define how each access to the API URLs this package will dispatch to classes that handle each supported API endpoint.

Picture of Pierre-Henry Soria
  Performance   Level  
Name: Pierre-Henry Soria <contact>
Classes: 49 packages by
Country: United Kingdom
Innovation award
Innovation award
Nominee: 18x

Winner: 3x

Example

<?php

declare(strict_types=1);

use
Dotenv\Dotenv;
use
React\EventLoop\Factory;
use
React\Http\Server;
use
Sapphire\App\Core\ErrorHandler;
use
Sapphire\App\Core\JsonRequestDecoder;
use
Sapphire\App\Core\Router;
use
Sapphire\App\Orders\Controller\CreateOrder\Controller;
use
Sapphire\App\Orders\Controller\DeleteOrder;
use
Sapphire\App\Orders\Controller\GetAllOrders;
use
Sapphire\App\Orders\Controller\GetOrderById;
use
Sapphire\App\Orders\Storage as Orders;
use
Sapphire\App\Products\Controller\CreateProduct;
use
Sapphire\App\Products\Controller\DeleteProduct;
use
Sapphire\App\Products\Controller\GetAllProducts;
use
Sapphire\App\Products\Controller\GetProductById;
use
Sapphire\App\Products\Controller\UpdateProduct;
use
Sapphire\App\Products\Storage as Products;

require
__DIR__ . '/vendor/autoload.php';

$requiredEnvFields = [
   
'JWT_KEY',
   
'DB_HOST',
   
'DB_USER',
   
'DB_PWD',
   
'DB_NAME',

];
$env = Dotenv::createImmutable(__DIR__);
$env->load();
$env->required($requiredEnvFields)->notEmpty();

$loop = Factory::create();

$factory = new \React\MySQL\Factory($loop);
$uri = getenv('DB_USER') . ':' . getenv('DB_PWD') . '@' . getenv('DB_HOST') . '/' . getenv('DB_NAME');
$connection = $factory->createLazyConnection($uri);

$products = new Products($connection);
$orders = new Orders($connection);
$dispatcher = require __DIR__ . '/src/routes.php';

$middlewares = [
    new
ErrorHandler,
    new
JsonRequestDecoder,
    new
Router($dispatcher),
];

$server = new Server($middlewares);

$socket = new \React\Socket\Server(getenv('SERVER_URI'), $loop);
$server->listen($socket);

$server->on('error', static function (Throwable $error) {
    echo
'Error: ' . $error->getMessage() . PHP_EOL;
});

printf('Listening on %s', str_replace('tcp', 'http', $socket->getAddress()) . PHP_EOL);

$loop->run();


Details

? Sapphire API ?

Sapphire is an amazing, precious, and great,... asynchronous RESTful API using ReactPHP and PHP 7.4.

The application is built on top of the Action-Domain-Responder (ADR) architecture.

? The Requirements

??? The Cooker

Pierre-Henry Soria. A versatile, programmatic and passionate software engineer who enjoys programming and developing new skills on a daily basis! ???

Reachable at hi {{AT}} ph7 [[D0T]] me!

? Set It Up

  1. Run `composer install` to install the project dependencies.
  2. Located in the project root, rename `.env.dist` to `.env` and edit the app details.
  3. Start the app server `php server.php`

  Files folder image Files (17)  
File Role Description
Files folder imagesrc (1 file, 3 directories)
Accessible without login Plain text file .env.dist Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file server.php Example Example script

  Files folder image Files (17)  /  src  
File Role Description
Files folder imageCore (4 files)
Files folder imageException (2 files)
Files folder imageHttp (1 directory)
  Plain text file routes.php Class Class source

  Files folder image Files (17)  /  src  /  Core  
File Role Description
  Plain text file ErrorHandler.php Class Class source
  Plain text file JsonRequestDecoder.php Class Class source
  Plain text file JsonResponse.php Class Class source
  Plain text file Router.php Class Class source

  Files folder image Files (17)  /  src  /  Exception  
File Role Description
  Plain text file ProductNotFound.php Class Class source
  Plain text file UserAlreadyExists.php Class Class source

  Files folder image Files (17)  /  src  /  Http  
File Role Description
Files folder imageAction (3 directories)

  Files folder image Files (17)  /  src  /  Http  /  Action  
File Role Description
Files folder imageProduct (3 files)
Files folder imageUser (1 file)
Files folder imageUtility (1 file)

  Files folder image Files (17)  /  src  /  Http  /  Action  /  Product  
File Role Description
  Plain text file GetItemDetails.php Class Class source
  Plain text file GetItems.php Class Class source
  Plain text file Input.php Class Class source

  Files folder image Files (17)  /  src  /  Http  /  Action  /  User  
File Role Description
  Plain text file CreateUser.php Class Class source

  Files folder image Files (17)  /  src  /  Http  /  Action  /  Utility  
File Role Description
  Plain text file StaticFiles.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:38
This week:0
All time:10,935
This week:69Up