Getting Started

Install

composer require patrikmalmstrom/hyperphp

Basic Evaluation

<?php

use Hyperphp\HyperScript;

$runtime = new HyperScript();
$session = $runtime->evaluate(<<<SCRIPT
    set value to 40
    return value + 2
SCRIPT);

echo $session->result(); // 42

evaluate() returns a HyperScriptSession.

Register an Application Model

<?php

use Hyperphp\Host\HyperApplication;
use Hyperphp\HyperScript;

final class DemoApp extends HyperApplication
{
    public function ping(string $value): string
    {
        return 'pong:' . $value;
    }
}

$runtime = new HyperScript();
$runtime->registerModel('demo', new DemoApp());

$result = $runtime->evaluate(<<<SCRIPT
    tell application "demo"
        ping "ok"
    end tell
SCRIPT)->result();

Tell and Query Example

tell application "mail"
    set unread to every message of inbox whose read status is false

    repeat with m in unread
        tell m
            set read status to true
        end tell
    end repeat
end tell

Error Handling Example

try
    error {message: "bad request", code: "APP_400"}
on error err
    return err.code
end try

Include Shared Script Files

use script "lib/common"
return greet("Ada")

use script resolves absolute paths or relative paths (for example via ExecutionOptions::withSourcePath()).

Advanced Handler Quick Start

<?php

use Hyperphp\Host\HyperApplication;
use Hyperphp\Host\ScriptHandlerHostInterface;
use Hyperphp\HyperScript;

final class Router extends HyperApplication implements ScriptHandlerHostInterface
{
    /** @var array<string, array<string, callable>> */
    private array $handlers = [];

    public function registerScriptHandler(string $verb, string $subject, callable $handler): void
    {
        $this->handlers[strtolower($verb)][strtolower($subject)] = $handler;
    }

    public function dispatchGet(string $path, array $eventContext = []): mixed
    {
        return ($this->handlers['get'][strtolower($path)] ?? static fn (array $_eventContext = []) => null)($eventContext);
    }
}

$router = new Router();
$runtime = new HyperScript();
$runtime->registerModel('router', $router);

$runtime->evaluate(<<<SCRIPT
    tell application "router"
        on get "/"
            return "ok"
        end get
    end tell
SCRIPT);

echo $router->dispatchGet('/'); // "ok"

See Advanced Handlers for more patterns (events, nested tell, cross-app workflows, and error modes).

Run the Test Suite

composer test