serafim/boson

Native PHP WebView bridge

dev-master / 1.0.x-dev 2025-03-16 15:01 UTC

This package is auto-updated.

Last update: 2025-03-16 15:01:09 UTC


README

PHP 8.4+ Latest Stable Version Latest Unstable Version License MIT MetaStorm

Why Boson? Because it's not an Electron! And much easier than that =)

Also, this repository contains included high level PHP bindings for webview v0.12.0.

Simple Example

use Serafim\Boson\Application;

$app = new Application();

$app->webview->html = <<<'HTML'
    <button onclick="foo('HELLO');">Hello</button>
    HTML;

$app->webview->bind('foo', function (string $message): void {
    var_dump($message);
});

$app->run();

Requirements

  • PHP ^8.4
    • ext-ffi
Platform Technologies
Windows Windows API, WebView2
Linux GTK, WebKitGTK
macOS Cocoa, WebKit

Windows

Requires Windows 10 or higher.

End-users must have the WebView2 runtime installed on their system for any version of Windows before Windows 11.

Note: A pre-builded version of the bridge already comes with the portability package in bin/WebView2Loader.dll. No any additional installation required.

Linux and BSD

The GTK and WebKitGTK libraries are required. You need to check your package repositories regarding which packages to install.

Debian

WebKitGTK 6.0, GTK 4

apt install libgtk-4-1 libwebkitgtk-6.0-4

WebKitGTK 4.1, GTK 3, libsoup 3

apt install libgtk-3-0 libwebkit2gtk-4.1-0

WebKitGTK 4.0, GTK 3, libsoup 2

apt install libgtk-3-0 libwebkit2gtk-4.0-37

Fedora

WebKitGTK 6.0, GTK 4

dnf install gtk4 webkitgtk6.0

WebKitGTK 4.1, GTK 3, libsoup 3

dnf install gtk3 webkit2gtk4.1

WebKitGTK 4.0, GTK 3, libsoup 2

dnf install gtk3 webkit2gtk4.0

FreeBSD

GTK 4

pkg install webkit2-gtk4

GTK 3

pkg install webkit2-gtk3

MacOS

It appears that no additional dependencies are required.

Usage

Window Title

To get or update the title, you should change the $title property

$app = new Serafim\Boson\Application();

$app->webview->title = 'New Title';

echo 'Current Title: ' . $app->webview->title;

$app->run();

Window Resizing

To change the size, use the resize() method.

$app = new Serafim\Boson\Application();

$app->webview->resize(640, 480);

$app->run();

Window Max Size

To change the max size, use the resize() method.

$app = new Serafim\Boson\Application();

$app->webview->resize(640, 480, \Serafim\Boson\Core\WebViewSizeHint::MaxBounds);

$app->run();

Window Min Size

To change the min size, use the resize() method.

$app = new Serafim\Boson\Application();

$app->webview->resize(640, 480, \Serafim\Boson\Core\WebViewSizeHint::MinBounds);

$app->run();

Window Fixed Size

To set the fixed size, use the resize() method.

$app = new Serafim\Boson\Application();

$app->webview->resize(640, 480, \Serafim\Boson\Core\WebViewSizeHint::Fixed);

$app->run();

Window HTML Content

To set the content, you should use the $html property

$app = new Serafim\Boson\Application();

$app->webview->html = '<button>Do Not Click Me!</button>';

$app->run();

Please note that reading this property is NOT possible. If you need to read the contents, use the data retrieval method.

$app = new Serafim\Boson\Application();

$app->webview->request('document.body.innerHTML')
    ->then(function (string $html) {
        var_dump($html);
    });

$app->run();

Global Styles

You can register a CSS style that will be applied to any page

$app = new Serafim\Boson\Application();

$app->webview->styleBeforeLoad(<<<'CSS'
    body {
        background: #900;
    }
    CSS);

$app->run();

Global Scripts

You can register a JavaScript code that will be applied to any page

$app = new Serafim\Boson\Application();

$app->webview->evalBeforeLoad(<<<'JS'
    alert('hello');
    JS);

$app->run();

Creating Functions

You can create a function that can be called directly from WebView

$app = new Serafim\Boson\Application();

$app->webview->bind('foo', function () { 
    var_dump('Executed!');
});

$app->run();

Code Evaluation

You can execute arbitrary code directly on current WebView

$app = new Serafim\Boson\Application();

$app->webview->eval('document.write("Hello World!")');

$app->run();

Code Requests

You can directly get data from WebView context

$app = new Serafim\Boson\Application();

$app->webview->request('document.location')
    ->then(function (array $data) {
        var_dump($data);
    });

$app->run();

Quit

To exit the application, you should call the quit() method

$app = new Serafim\Boson\Application();

$app->webview->html = '<button onclick="quit()">exit</button>';

$app->webview->bind('quit', function () use ($app) {
    $app->quit();
});

$app->run();