Ways 2.5.0 · Library for PHP

Control points, reached from anywhere

Ways routes requests, but it thinks in control points rather than routes: pieces of work that run when a way matches. The same control point answers an HTTP request, a command line or a call from your own PHP code. Rules decide whether it runs, hooks add behaviour without touching it, and control points pass data to each other.

Free software (GPL 3) · PHP 8.0 or newer · one class in one file, no dependencies

Three ways in

Write it once, call it from the web, the terminal or PHP

A control point is a closure or a class method listening on a way. {name} captures part of the way as an argument. The example app loads the same file from three entry scripts. These are their real outputs with Ways 2.5.0.

Control point points/hello.php
<?php

use divengine\ways;

ways::listen('/hello/{name}', function ($data, $args) {
    echo "Hello {$args['name']}\n";
}, 'hello');

Command line

Entry cli.php
<?php
// php cli.php hello Ada  ->  the way /hello/Ada
require __DIR__ . '/ways.php';

divengine\ways::bootstrap();
echo '[control points run: ' . divengine\ways::getTotalExecutions() . "]\n";
Terminal
$ php cli.php hello Ada
Hello Ada
[control points run: 1]

HTTP

Entry index.php
<?php
// Web entry. With the PHP server: php -S localhost:8000 index.php
require __DIR__ . '/ways.php';

divengine\ways::bootstrap();
Response PHP's built-in server
GET /hello/Ada  ->  200
Hello Ada

PHP code

Caller direct.php
<?php
// Call a control point from PHP code, with data, and read what it returns.
require __DIR__ . '/ways.php';

use divengine\ways;

// The same control point the command line and the web reach
ways::invoke('/hello/Ada');

// Pass data in, get data back
ways::listen('/price/{amount|is_int}', function ($data, $args) {
    return ['net' => (int) $args['amount'], 'gross' => (int) $args['amount'] * (1 + $data['vat'])];
}, 'price');

$result = ways::invoke('/price/100', ['vat' => 0.21]);
echo json_encode($result), "\n";
Output php direct.php
Hello Ada
{"vat":0.21,"net":100,"gross":121}

Matching

Ways that check what they accept

An argument can name a checker after a bar: any PHP function or static method that returns true or false. A way can also be tied to a request method, such as post://. When nothing matches, nothing runs.

An argument checker

is_int lets 42 through and stops abc.

points/orders.php
<?php

use divengine\ways;

// {id|is_int} matches only when is_int() accepts the value
ways::listen('/orders/{id|is_int}', function ($data, $args) {
    echo "Order #{$args['id']}\n";
}, 'order');
Terminal
$ php cli.php orders 42
Order #42
[control points run: 1]

$ php cli.php orders abc
[control points run: 0]

A request method

The same path, asked with GET and with POST.

points/contact.php
<?php

use divengine\ways;

// Only an HTTP POST reaches this way
ways::listen('post://contact', function () {
    echo "Message received\n";
}, 'contact');
Responses
GET /contact  ->  200
(empty body: no control point matched)

POST /contact  ->  200
Message received

Working together

Control points share data, depend on each other, and can be extended

Every control point that matches a way runs, in the order they were registered. What one returns is merged into the data the next one receives. The line in brackets is printed by cli.php after each run, with ways::getTotalExecutions().

Shared data

Two control points on /report: one loads, the other shows.

points/report.php
<?php

use divengine\ways;

// Two control points on one way: what the first returns, the second receives
ways::listen('/report', function () {
    return ['orders' => 3, 'total' => 120];
}, 'load-report');

ways::listen('/report', function ($data) {
    echo "{$data['orders']} orders, {$data['total']} EUR\n";
}, 'show-report');
Terminal
$ php cli.php report
3 orders, 120 EUR
[control points run: 2]

Requirements

A control point can require another one, which runs first even though its own way was not asked for.

points/invoice.php
<?php

use divengine\ways;

// "require" runs load-customer before invoice, whatever way was asked
ways::listen('/invoice', function ($data) {
    echo "Invoice for {$data['customer']}\n";
}, [
    ways::PROPERTY_ID => 'invoice',
    'require' => 'load-customer',
]);

ways::listen('/customers/current', function () {
    return ['customer' => 'Ada Lovelace'];
}, 'load-customer');
Terminal
$ php cli.php invoice
Invoice for Ada Lovelace
[control points run: 2]

Rules

A rule receives the data and the arguments and returns true or false. Rules are named, so many control points can share one.

points/login.php
<?php

use divengine\ways;

// The control point runs only when every rule returns true
ways::rule('is-admin-section', function ($data, $args) {
    return $args['section'] === 'admin';
});

ways::listen('/{section}/login', function ($data, $args) {
    echo "Login form of the {$args['section']} section\n";
}, [
    ways::PROPERTY_ID => 'login',
    ways::PROPERTY_RULES => ['is-admin-section'],
]);
Terminal
$ php cli.php admin login
Login form of the admin section
[control points run: 1]

$ php cli.php shop login
[control points run: 0]

Hooks

A plugin hooks into a control point that does not know about it. Hooks can run before a control point's file is included or before it runs.

welcome.php and plugin.php
<?php

use divengine\ways;

// The control point does not know that a plugin exists
ways::listen('/welcome', function ($data) {
    echo "Welcome, {$data['user']}\n";
}, 'welcome');

<?php

use divengine\ways;

// Runs before "welcome" and changes the data it receives
ways::hook(ways::BEFORE_RUN, 'welcome', function ($data) {
    $data['user'] = 'Grace (set by the plugin)';
    return $data;
});
Terminal
$ php cli.php welcome
Welcome, Grace (set by the plugin)
[control points run: 1]

Controller classes

Classes that say where they listen

For larger applications, a control point can be a class. ways::register() reads its id and its ways from comments in the file, and includes the file only when one of its ways is asked for. #listen runs Run(); #listen@Bye runs another method.

Controller Greeter.php
<?php

#id = greeter
#listen = /greeter

class Greeter
{
    // The way in #listen runs Run()
    public static function Run()
    {
        echo "Greeter::Run()\n";
    }

    #listen@Bye = /greeter/bye
    public static function Bye()
    {
        echo "Greeter::Bye()\n";
    }
}
Registration in ways.php
divengine\ways::register(__DIR__ . '/Greeter.php');
Terminal
$ php cli.php greeter
Greeter::Run()
[control points run: 1]

$ php cli.php greeter bye
Greeter::Bye()
[control points run: 1]

At a glance

Small, and easy to embed

One class, one file

No dependencies besides PHP's JSON extension. Install it with Composer or include the file.

Web and terminal

On the web, the way comes from the URL (or a _url parameter behind a rewrite rule). In a terminal, from the command's arguments.

Control points, not controllers

Several control points can answer one way, and one control point can call, require or hook into others. That suits plugin architectures.

Checked

Version 2.5.0 added tests of matching, data, hooks and rules, and static analysis with PHPStan, run on every pull request.

Install

Try it in a minute

1. Add Ways to a project

With Composer, in an empty folder.

composer require divengine/ways:2.5.0

2. Write a control point

index.php:

<?php
require __DIR__ . '/vendor/autoload.php';

use divengine\ways;

ways::listen('/hello/{name}', function ($data, $args) {
    echo "Hello {$args['name']}\n";
});

ways::bootstrap();

3. Run it

From the terminal:

php index.php hello Ada
Hello Ada

Or on the web, with PHP's server, and open http://localhost:8000/hello/Ada:

php -S localhost:8000 index.php

Keep the name index.php: under PHP's server, Ways reads the way from the URL relative to the script, and a request to a file that does not exist is given to index.php.

Every example on this page, with the commands to run it again with Ways 2.5.0, is in the site's repository.