An object oriented wrapper around PHP's built-in server.

Overview

Statix Server

Banner image

Requirements

  • PHP 8 minumum

Installation

composer require statix/server

Basic Usage

To get started, ensure the vendor autoload script is required and then create an instance of the Server class, once you have set any configuration options, you should call the start method to start the server.

use Statix\Server\Server;

require_once './vendor/autoload.php';

Server::new()->start();

// or 

(new Server)->start();

Advanced Usage

You can configure the several options with the server, such as the host, the port, the root directory and more. Please read more below for a detailed explanation of each configuration method.

Passing configuration via the constructor or Server::new()

You may pass most configuration options via the constructor. For example we are setting the host, port and root options in the code below.

use Statix\Server\Server;

Server::new([
    'host' => 'localhost',
    'port' => 8000,
    'root' => __DIR__ . '/content'
]);

// or 

new Server([
    'host' => 'localhost',
    'port' => 8000,
    'root' => __DIR__ . '/content'
]);

The complete list of configuration items that can be passed via the constructor can be found below.

$optionsSettableViaContructor = [
    'host' => 'string', // default: localhost
    'port' => 'string|int', // default: 8000
    'root' => 'string', // default: getcwd()
    'router' => 'string', // path to your routing script
    'executable' => 'string', // path to the desired PHP binary to use for the server
    'withEnvVars' => [
        'APP_DYNAMIC_ENV' => 'server'
    ],
    'withoutEnvVars' => [ 
        'APP_KEY'
    ]
];

Setting configuration via the named methods

You also have the option of calling named methods to set the configuration options as shown below.

use Statix\Server\Server;

Server::new()
    ->usePHP('path') 
    ->onHost('localhost') 
    ->onPort('8080') 
    ->root('./content')
    ->useRouter('./router.php')
    ->withEnvVars([
        'APP_DYNAMIC_ENV' => 'server'
    ])->withoutEnvVars([
        'APP_KEY',
    ]);

Capturing the output from the server process

If you want to show the output from the server process as it recieves and handles requests, you may call the output method and pass a callback function that will be called and passed any output of the process.

Server::new()
    ->output(function($output) {
        echo $output;
    })->start();

Running the process in the background

You may find it useful to run the server process in the background, you may call runInBackground(). The process will run as long as the parent script is running.

Server::new()->runInBackground();

Checking whether the process is running

You may check whether or not the server is currently running by calling the isRunning method.

$server = Server::new()->withEnvVars([
    'APP_NAME' => 'statix/server',
]);

$server->isRunning(); // false

$server->runInBackground();

$server->isRunning(); // true

Stopping the server

You may stop the process running the sever by calling the stop command on an instance of the server class. If the server is not currently running this method will return null otherwise it will return an array container first the process exit code and second the process exit text. Note this command can only be called when the server is running in the background.

$server = Server::new()->runInBackground();

// do work

$server->stop();

Contributing

Installation

  1. Clone repo
git clone https://github.com/statix-php/server.git
  1. Install php dependencies
composer install

Testing

We use Pest PHP for the test suite, please ensure before pushing changes you confirm there are no breaking changes by running the command below. Additionally, tests for new features are highly encouraged, changes will be considered without tests but it will increase the time to accept / merge.

./vendor/bin/pest

Style

We use Laravel Pint to automatically standardize code styling, before pushing changes please run pint using the command below.

./vendor/bin/pint
You might also like...
PHP FFmpeg - An Object Oriented library to convert video/audio files with FFmpeg / AVConv

PHP FFmpeg An Object Oriented library to convert video/audio files with FFmpeg / AVConv. Check another amazing repo: PHP FFMpeg extras, you will find

Strings Package provide a fluent, object-oriented interface for working with multibyte string

Strings Package provide a fluent, object-oriented interface for working with multibyte string, allowing you to chain multiple string operations together using a more readable syntax compared to traditional PHP strings functions.

Provides an object-oriented API to query in-memory collections in a SQL-style.

POQ - PHP Object Query Install composer require alexandre-daubois/poq 1.0.0-beta2 That's it, ready to go! 🎉 Usage Here is the set of data we're going

PHP Unoconv - An Object Oriented library which allow easy to use file conversion with Unoconv.

An Object Oriented library which allow easy to use file conversion with Unoconv. Install The recommended way to install PHP-Unoconv is thr

The XP Framework is an all-purpose, object oriented PHP framework.

XP Framework Core This is the XP Framework's development checkout. Installation If you just want to use the XP Framework, grab a release using compose

This is a plugin for pocketmine-mp, when locking a player's items helps players not to lose items or throw things around causing server lag.

[] LockedItem| v1.0.0 Player's item lock Features Player's item lock Players aren't afraid of losing items For Devolopers You can access to LockedItem

An article about alternative solution for convert object into a JSON Object for your api.

Do we really need a serializer for our JSON API? The last years I did build a lot of JSON APIs but personally was never happy about the magic of using

Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

PHP exercises from my course at ETEC and some of my own play-around with PHP

etec-php-exercises PHP exercises from my course at ETEC and some of my own play-around with PHP Translations: Português (BR) Projects Project Descript

Comments
  • How to reload env file on restart?

    How to reload env file on restart?

    Hey

    When I restart the server, I would like to also reload the .env file. It doesn't seem to work sadly.

    $server = Server::new()
        // ->php('path')
        ->host('localhost')
        ->port('85')
        ->root(__DIR__ . '/public')
        ->withEnvFile('.env');
    
    // ...
    
    $server->runInBackground();
    
    

    then I do a simple while loop and restart the server if a file was changed, but the ".env" file is not reloaded, even though the file was changed before the $server->restart().

    I have tried different things, but can't get it to work.

    opened by xy2z 2
Releases(0.4.1)
Owner
Statix PHP
Statix PHP
A simple Object Oriented wrapper for Linear API, written with PHP.

PHP Linear API A simple Object Oriented wrapper for Linear API, written with PHP. NOTE You should take a look Linear GraphQL API Schema for all nodes

Mustafa KÜÇÜK 6 Sep 2, 2022
WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate is simply a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate 1.7k Dec 24, 2022
This is a simple Wrapper around the ZipArchive methods with some handy functions

Note I haven't updated this package in a long time except merging PRs. The last time I was using this package was with PHP5. I archived the repository

Nils Plaschke 845 Dec 13, 2022
This is a simple Wrapper around the ZipArchive methods with some handy functions

Note I haven't updated this package in a long time except merging PRs. The last time I was using this package was with PHP5. I archived the repository

Nils Plaschke 836 Jan 26, 2022
A wrapper around faker for factory muffin

Factory Muffin Faker 2.3 The goal of this package is to wrap Faker to make it super easy to use with Factory Muffin. Note that this library does not a

The League of Extraordinary Packages 36 Nov 29, 2022
A PHP wrapper around Libreoffice for converting documents from one format to another.

Document Converter A PHP wrapper around Libreoffice for converting documents from one format to another. For example: Microsoft Word to PDF OpenOffice

Lukas White 0 Jul 28, 2022
A wrapper around symplify/config-transformer used to update recipes and using easy coding standard for generating readable config files.

Symfony Recipes Yaml to PHP Converter This is a wrapper around the symplify/config-transformer used to convert Symfony core recipes which uses .yaml c

Alexander Schranz 3 Nov 24, 2022
A simple wrapper around vlucas' PHP dotenv library for Kirby CMS.

kirby-phpdotenv A simple wrapper around vlucas' PHP dotenv library for Kirby CMS. Why? I've been using .env in my Kirby projects for a while, but I go

Steve Jamesson 5 Feb 6, 2020
the examples of head first object oriented analysis & design - in PHP

Head First object oriented analysis & design in (PHP) after cloning the repository, you have to install the project's dependancies by running the foll

Muhammed ElFeqy 3 Oct 16, 2021
Object-Oriented API for PHP streams

Streamer Streamer is an Object-Oriented API for PHP streams. Why should I use Streams? A stream is a flow of bytes from one container to the other. Yo

Francois Zaninotto 270 Dec 21, 2022