Ratings | | Unique User Downloads | | Download Rankings |
Not yet rated by the users | | Total: 38 | | All time: 10,935 This week: 69 |
|
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. | |
|
|
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
-
Run `composer install` to install the project dependencies.
-
Located in the project root, rename `.env.dist` to `.env` and edit the app details.
-
Start the app server `php server.php`
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.