phabelio / phabel

PHP transpiler - Write and deploy modern PHP 8 code, today.
MIT License
242 stars 8 forks source link
babel php transpiler

Phabel phabel.io - PHP transpiler

Write and deploy modern PHP 8 code, today.

This is a transpiler that allows native usage of PHP 8+ features and especially syntax in projects and libraries, while allowing maintainers to publish a version targeting lower versions of php.

The transpiler seamlessly hooks into composer to transpile the package (and all dependencies down the current branch of the dependency tree!) on installation, on the user's machine, targeting the user's specific PHP version.

Syntax/feature support:

Created by Daniil Gentili

Usage

composer require --dev phabel/phabel

You can now publish your packagist package, and it will be automatically transpiled to any PHP version supported by phabel.

After git tagging a new release, just run:

vendor/bin/phabel publish

πŸ’‘ Your PHP 7 users can now install your PHP 8 library πŸ’‘
πŸ’‘ All your dependencies will also be transpiled to the correct PHP version. πŸ’‘

Supported PHP versions

Syntax/feature support:

Target:

No additional commands are required to add support for older versions: just composer update πŸ˜„

Examples

Async/await syntax

Phabel also supports async/await syntax, powered by Amp.
Parallelize your code, using native async/await syntax and the async Amp libraries for fully concurrent networking, I/O, database access in pure, native PHP!

File I/O

This example uses the amphp/file library:

<?php

// Write and read three files on your filesystem, in parallel
// Async/await syntax powered by phabel.io
// Concurrency powered by amphp.org

require 'vendor/autoload.php';

use Amp\Loop;
use function Amp\File\read;
use function Amp\File\write;

Loop::run(function () {
    // This is done in parallel!
    await [
        write('file1', 'contents1'),
        write('file2', 'contents2'),
        write('file3', 'contents3'),
    ];

    // This is also done in parallel!
    var_dump(await [
        read('file1'),
        read('file2'),
        read('file3'),
    ]);
});

You can publish this code as a Composer package and have it automatically transpile on installation, or even transpile it manually:

composer require amphp/file phabel/phabel
vendor/bin/phabel run input.php output.php
php output.php

CLI API

When you run:

composer require --dev phabel/phabel

Phabel automatically edits composer.json, adding some configuration parameters, and raising the minimum supported PHP version to 8.0.

Then, the following commands are available:

publish

Publishes a transpiled version of your package+dependencies.

πŸ’‘ Your PHP 7 users can now install your PHP 8 library πŸ’‘
πŸ’‘ All your dependencies will also be transpiled to the correct PHP version. πŸ’‘

Internally, this command takes the newest (or provided) git tag, and then creates+pushes two subtags:

An example, say you have this composer.json:

{
    "name": "phabel/package",
    "description": "An example package.",
    "type": "project",
    "require": {
        "php": ">=8.0",
        "vendor/new-php8-package": "^1",
        "vendor/old-php7-package": "^1"
    },
    "require-dev": {
        "phabel/phabel": "^1"
    }
}

Note that new-php8-package only supports PHP 8.0+, and old-php7-package supports PHP 7.0+.

Here's what happens when a user requires phabel/package:^tag on:

Note that as phabel adds support for lower and lower PHP versions, your package will automatically gain support for those versions, too, no need to republish it!

Usage:

vendor/bin/phabel publish [options] [--] [<source>]

Arguments:

Options:

run

This commands simply transpiles the specified file or directory towards the specified PHP version.
This command is useful for playing around with phabel, or creating a transpiled phar file.
Always make sure to also transpile the vendor directory when creating a phar, to automatically resolve covariance and contravariance conflicts.

publish is a much simpler version of this command, which automatically transpiles the package (and all dependencies!) upon Composer installation.

Usage:

vendor/bin/phabel run [options] [--] <input> <output>

Arguments:

Options:

Projects using phabel

Ping us if you want your project to be added to this list!