PHP Nuclear Reactor: Asynchronous RESTful API using ReactPHP and PIMF

Recommend this page to a friend!
  Info   View files Documentation   View files View files (70)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2021-05-18 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 250 All time: 7,832 This week: 236Up
Version License PHP version Categories
reactphp-pimf 1.0.21Freeware5HTTP, PHP 5, Web services
Collaborate with this project Author

reactphp-pimf - github.com

Description

This package can implement an asynchronous RESTful API using ReactPHP and PIMF.

The PIMF framework handles API REST HTTP requests dispatched with the ReactPHP framework to process the API calls.

An example is provided to demonstrate how create REST API handlers with PIMF and ReactPHP for managing articles.

Innovation Award
PHP Programming Innovation award nominee
June 2016
Number 8
Asynchronous programming is a way to create programs that can execute multiple parallel tasks faster in the same process by executing code while other parts if the programs wait for I/O operations to finish, like database accesses, file accesses, network accesses, etc..

ReactPHP is a low level library similar to JavaScript Node.js that can be used to implement asynchronous programming in PHP, so you can write PHP code that can efficiently access multiple files, databases, or network computers in parallel, making everything faster.

The PHP Nuclear Reactor is library based on PIMF micro-framework that implements the MVC design pattern on top of ReactPHP.

This way you can continue to write PHP MVC components the way you are used to do, and still work well in an asynchronous programming environment based on ReactPHP.

Manuel Lemos
Picture of Gjero Krsteski
  Performance   Level  
Name: Gjero Krsteski <contact>
Classes: 4 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 2x

Details

Welcome to PHP Nuclear Reactor

This is an reactive RESTful API which uses ReactPHP and PIMF micro framework.

Build Status Code Climate

ReactPHP is one of the most promising libraries. It brings the powerful concept of event-driven, non-blocking I/O (Hi NodeJS) to PHP. With this technology in mind we are able to write our own HTTP stack in PHP and have control back over the memory without destroying it at the end of each request.

ReactPHP is responsible for establishing the TCP-Socket, transferring the raw HTTP Requests and sending the HTTP Responses back.

PIMF micro framework is responsible for managing the HTTP Resources using the HTTP request methods, validation and handling the persistence interactions.

Benchmark Results, over 19500 requests completed per second


[vagrant@gkrsteski php-reactor]$ loadtest http://10.0.49.227:5501/articles/10 -t 20 -c 20 --rps 1000
[Wed Feb 10 2016 12:24:47 GMT+0100 (CET)] INFO Requests: 0, requests per second: 0, mean latency: 0 ms
[Wed Feb 10 2016 12:24:52 GMT+0100 (CET)] INFO Requests: 4516, requests per second: 903, mean latency: 10 ms
[Wed Feb 10 2016 12:24:57 GMT+0100 (CET)] INFO Requests: 9515, requests per second: 1000, mean latency: 0 ms
[Wed Feb 10 2016 12:25:02 GMT+0100 (CET)] INFO Requests: 14515, requests per second: 1000, mean latency: 10 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Target URL:          http://10.0.49.227:5501/articles/10
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Max time (s):        20
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Concurrency level:   20
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Agent:               none
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Requests per second: 1000
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Completed requests:  19512
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Total errors:        0
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Total time:          20.000516531000002 s
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Requests per second: 976
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Total time:          20.000516531000002 s
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Percentage of the requests served within a certain time
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   50%      2 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   90%      9 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   95%      18 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   99%      30 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO  100%      36 ms (longest request)

Installation

  1. clone this repository
  2. run: `./init-app.sh`

Run using PHP

php run-server.php

Or run with Docker

Build a Docker image for your application by running:


docker build -t="reactphp-pimf-api" .

Finally, run your application as a Docker container by running:

docker run -d -p 1337:1337 reactphp-pimf-api

Try out the RESTful API

  • http://{reactive.hosts}:{reactive.port}/articles/
  • use HTTP method OPTIONS to find how to use the API

Expected API behavior for resource: /articles

HTTP Verb |  Entire Collection (e.g. /articles)                                          | Specific Item (e.g. /articles/{id})
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
GET       | 400 (Bad Request)                                                            | 200 (OK), single article.
          |                                                                              | 404 (Not Found), if ID not found or 400 (Bad Request) if invalid.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
PUT       | 405 (Method Not Allowed)                                                     | 200 (OK),
          | - unless you want to update/replace every resource in the entire collection. | 404 (Not Found) if ID not found or 400 (Bad Request) if invalid.
          | - 'Location' header with link to API documentation                           |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
POST      | 201 (Created),                                                               | 405 (Method Not Allowed)
          | - 'Location' header with link to /articles/{id} containing new ID.           | - 'Location' header with link to API documentation
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
DELETE    | 404 (Not Found) or 405 (Method NOt Allowed)                                  | 200 (OK)
          | - unless you want to delete the whole collection?not often desirable.        | 404 (Not Found) if ID not found or 400 (Bad Request) if ID invalid.
          | - 'Location' header with link to API documentation                           |


Run tests

Install a Composer to your project's root

php composer.phar require "codeception/codeception:*"
// or ...
composer require "codeception/codeception:*"

Execute tests


php vendor/codeception/codeception/codecept run --colors
// or if want to see steps ...
php vendor/codeception/codeception/codecept run --steps
// or if want to see fancy HTML report page
php vendor/codeception/codeception/codecept run --html
  Files folder image Files  
File Role Description
Files folder image.github (2 directories)
Files folder imageapp (3 files, 1 directory)
Files folder imagetests (2 files, 3 directories)
Accessible without login Plain text file .codeclimate.yml Data Auxiliary data
Accessible without login Plain text file .dockerignore Data Auxiliary data
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .gitattributes Data Auxiliary data
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file build.sh Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file create-table.php Example Example script
Accessible without login Plain text file Dockerfile Data Auxiliary data
Accessible without login Plain text file init-app.sh Data Auxiliary data
Accessible without login Plain text file local-pipeline.sh Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file run-server.php Example Example script
Accessible without login Plain text file SECURITY.md Data Auxiliary data
Accessible without login Plain text file sweep.sh Data Auxiliary data
Accessible without login Plain text file test.sh Data Auxiliary data
Accessible without login Plain text file _config.yml Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (1 file)
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file bug_report.md Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files  /  app  
File Role Description
Files folder imageArticles (5 directories)
  Accessible without login Plain text file autoload.app.php Aux. Auxiliary script
  Accessible without login Plain text file bootstrap.app.php Aux. Auxiliary script
  Accessible without login Plain text file config.app.php Aux. Auxiliary script

  Files folder image Files  /  app  /  Articles  
File Role Description
Files folder imageApplication (1 file)
Files folder imageDataMapper (1 file)
Files folder imageModel (1 file)
Files folder imageService (6 files)
Files folder image_database (2 files)

  Files folder image Files  /  app  /  Articles  /  Application  
File Role Description
  Plain text file Dispatcher.php Class Class source

  Files folder image Files  /  app  /  Articles  /  DataMapper  
File Role Description
  Plain text file Article.php Class Class source

  Files folder image Files  /  app  /  Articles  /  Model  
File Role Description
  Plain text file Article.php Class Class source

  Files folder image Files  /  app  /  Articles  /  Service  
File Role Description
  Plain text file CreateNewArticle.php Class Class source
  Plain text file DeleteExistingArticle.php Class Class source
  Plain text file FindExistingArticle.php Class Class source
  Plain text file ListApiUsageOptions.php Class Class source
  Plain text file UpdateExistingArticle.php Class Class source
  Plain text file WriteAllowedRequestMethods.php Class Class source

  Files folder image Files  /  app  /  Articles  /  _database  
File Role Description
  Accessible without login Plain text file blog-production.db Data Auxiliary data
  Accessible without login Plain text file create-table.sql Data Auxiliary data

  Files folder image Files  /  tests  
File Role Description
Files folder imageapi (1 file, 4 directories)
Files folder image_data (1 file, 1 directory)
Files folder image_support (4 files, 2 directories)
  Accessible without login Plain text file api.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  api  
File Role Description
Files folder imageCreate (4 files)
Files folder imageDelete (3 files)
Files folder imageFind (3 files)
Files folder imageUpdate (1 file)
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  api  /  Create  
File Role Description
  Accessible without login Plain text file AndFindArticleCept.php Example Example script
  Accessible without login Plain text file ArticleCept.php Example Example script
  Accessible without login Plain text file ArticleWithBadParamsNamesCept.php Example Example script
  Accessible without login Plain text file ArticleWithBadParamsValuesCept.php Example Example script

  Files folder image Files  /  tests  /  api  /  Delete  
File Role Description
  Accessible without login Plain text file AnExistingArticleCept.php Example Example script
  Accessible without login Plain text file ArticleWithBadIdFormatCept.php Example Example script
  Accessible without login Plain text file NotExistingArticleCept.php Example Example script

  Files folder image Files  /  tests  /  api  /  Find  
File Role Description
  Accessible without login Plain text file ArticleThatIsNotAtSystemCept.php Example Example script
  Accessible without login Plain text file ArticleUsingBadParamsCept.php Example Example script
  Accessible without login Plain text file ArticleUsingNoParamsCept.php Example Example script

  Files folder image Files  /  tests  /  api  /  Update  
File Role Description
  Accessible without login Plain text file AnExistingArticleCept.php Example Example script

  Files folder image Files  /  tests  /  _data  
File Role Description
Files folder imagescenarios (1 directory)
  Accessible without login Plain text file dump.sql Data Auxiliary data

  Files folder image Files  /  tests  /  _data  /  scenarios  
File Role Description
Files folder imageapi (8 files)

  Files folder image Files  /  tests  /  _data  /  scenarios  /  api  
File Role Description
  Accessible without login Plain text file Create_And_Find_Article.txt Doc. Documentation
  Accessible without login Plain text file Create_Article.txt Doc. Documentation
  Accessible without login Plain text file Create_Article_With_Bad_Params_Names.txt Doc. Documentation
  Accessible without login Plain text file Create_Article_Wit...d_Params_Values.txt Doc. Documentation
  Accessible without login Plain text file Create_Article_With_No_Params.txt Doc. Documentation
  Accessible without login Plain text file Find_Article_That_Is_Not_At_System.txt Doc. Documentation
  Accessible without login Plain text file Find_Article_Using_Bad_Params.txt Doc. Documentation
  Accessible without login Plain text file Find_Article_Using_No_Params.txt Doc. Documentation

  Files folder image Files  /  tests  /  _support  
File Role Description
Files folder imageHelper (4 files)
Files folder image_generated (4 files)
  Plain text file AcceptanceTester.php Class Class source
  Plain text file ApiTester.php Class Class source
  Plain text file FunctionalTester.php Class Class source
  Plain text file UnitTester.php Class Class source

  Files folder image Files  /  tests  /  _support  /  Helper  
File Role Description
  Plain text file Acceptance.php Class Class source
  Plain text file Api.php Class Class source
  Plain text file Functional.php Class Class source
  Plain text file Unit.php Class Class source

  Files folder image Files  /  tests  /  _support  /  _generated  
File Role Description
  Plain text file AcceptanceTesterActions.php Class Class source
  Plain text file ApiTesterActions.php Class Class source
  Plain text file FunctionalTesterActions.php Class Class source
  Plain text file UnitTesterActions.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:250
This week:0
All time:7,832
This week:236Up
For more information send a message to info at phpclasses dot org.