ahmard/php-server

A small library to help run PHP servers easily and quickly.

0.1.1 2024-03-26 21:10 UTC

This package is auto-updated.

Last update: 2024-11-26 22:49:00 UTC


README

A small library to help run PHP servers easily and quickly.

Installation

composer require ahmard/php-server --dev

Usage

PHP Built-In Server

An implementation of Built-In Server

  • With document root
use PHPServer\BuiltIn\Server;

Server::create('127.0.0.1', '9900')
    ->setDocumentRoot(__DIR__)
    ->start()
    ->logOutputToConsole();
  • Route request to single entry file
use PHPServer\BuiltIn\Server;

Server::create('127.0.0.1', '9900')
    ->setRouterScript(__DIR__ . 'public/index.php')
    ->start();
  • Provide callable to be invoked when request is received
use PHPServer\BuiltIn\Server;

Server::create('127.0.0.1', '9900')
    ->onRequest(fn() => var_dump('Request Received'))
    ->start();
  • Using multiple workers
use PHPServer\BuiltIn\Server;

Server::create('127.0.0.1', '9900')
    ->setWorkers(2)
    ->onRequest(fn() => var_dump('Request Received'))
    ->start();
  • Use preferred php version/executable
use PHPServer\BuiltIn\Server;

Server::create('127.0.0.1', '9900')
    ->setWorkers(2)
    ->setPHPExecutable('/usr/bin/php8.0')
    ->onRequest(fn() => var_dump('Request Received'))
    ->start();

Enjoy 😎