Download the PHP package edgaralexanderfr/php-espresso without Composer
On this page you can find all versions of the php package edgaralexanderfr/php-espresso. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download edgaralexanderfr/php-espresso
More information about edgaralexanderfr/php-espresso
Files in edgaralexanderfr/php-espresso
Package php-espresso
Short Description Runtime web server for PHP.
License MIT
Informations about the package php-espresso
PHP Espresso Framework
PHP Espresso is a small PHP Framework I created to develop runtime web servers for PHP running CLI programs and scripts. Very similar to frameworks like Express for NodeJS, Gorilla Mux for Golang, etc.
IMPORTANT NOTE: This is just a proof of concept to test the reliability of a runtime web server for PHP, its use and implementation is discouraged for production-level projects as it's an experimental framework for learning purposes.
PHP was designed to be a Single-Threaded Non-Asynchronous programming language, hence, the implementation of these type of web servers is very difficult as there will be always blocking processes for each request, hence, this server/framework is non-scalable.
Table of contents 📖
- Requirements
- Installation
- Usage
- 3.1 Creating a basic web server
- 3.2 Serving a basic static HTML Page
- 3.3 Create a POST request
- 3.4 Complete Rest API CRUD example
- 3.5 Defining middlewares
- 3.6 Asynchronous programming
Requirements
- PHP 8.0.0 or major
- Have PHP sockets module installed and enabled
- Composer
- Have a initted Composer project
Installation
Install PHP Espresso via Composer:
Usage
Creating a basic web server
Create a server.php file inside your project with the following program:
Run the server:
Visit http://localhost or execute:
And voila! 🎉
Serving a basic static HTML Page
Visit http://localhost/php-espresso-page in your browser.
Create a POST request:
Execute a POST request:
Complete Rest API CRUD example:
Create a couple of users:
Retrieve all created users:
Retrieve user with id
2:
Update user with id
1:
Delete user with id
2:
Defining middlewares
PHP Espresso supports global and route middlewares. You can assign as much middlewares to a single route as you want.
To do so, you can create a new middlewares.php file and add the following code:
If you run:
And do:
Or:
You will get the following message:
If you kill the previous server with CTRL+C and then run:
You will be able to retrieve the users list now, e.g:
To create a new user you need to be authenticated, to do so, assign an encoded Bearer Token using base64
to a variable and then pass the Authorization Header
to curl
command:
Asynchronous programming
It's still possible to do asynchronous programming with PHP Espresso by creating an asynchronous server and using the async
and $next
functions and callables:
The async
function initiates an Event Looper inside of the listen
method when running in async mode by setting $server->async(true);
.
async
may return a boolean value (false) when the async call is not done yet and returns true or nothing when it's finished.
In this example, the async call inside of the read_file
function will return false as long as the requested file is not completed yet, this by reading $bytes
as a step for each chunk read through every call inside the Event Loop as an asynchronous process.
Once the whole file is read, the $callable
callback will be called, passing in the content of the file on the async call by returning nothing at the very end of the function.
If you execute:
The smaller file request will respond earlier than the larger file request despite of being executed right at the same time.
This could be a way to implement asynchronous programs and libraries for streaming, networking, databases, files, I/O operations, etc, although it's not perfect, it would require a vast work to implement lots of PHP libraries that were designed initially to be Single-Threaded and Synchronous.
Maybe the future of PHP is promising for this purpose with the introduction of tools like Fibers
and stuff, but yet, we will see how it goes. 🙂🐘