介紹
Boson 是一個(gè)創(chuàng)新的跨平臺(tái)桌面應(yīng)用程序開(kāi)發(fā)平臺(tái),它釋放了 Web 技術(shù)(PHP、JavaScript、HTML、CSS)的強(qiáng)大功能以及本機(jī)匯編的優(yōu)勢(shì)。它的主要功能是將基于 Chromium 的 WebView 引擎和 PHP 解釋器直接集成到可執(zhí)行文件應(yīng)用程序中。
該解決方案允許開(kāi)發(fā)人員:
- 使用熟悉的堆棧技術(shù) — 通過(guò) HTML/CSS 創(chuàng)建接口并在 PHP(使用 JavaScript 元素)中實(shí)現(xiàn)邏輯,而無(wú)需學(xué)習(xí)特定于平臺(tái)的語(yǔ)言。
- 通過(guò) Chromium 引擎實(shí)現(xiàn)穩(wěn)定渲染,在 Windows、macOS 和 Linux 上提供可靠的用戶(hù)體驗(yàn) 。
- 減少單個(gè)代碼庫(kù)計(jì)數(shù)器的開(kāi)發(fā)時(shí)間 — 更改在所有平臺(tái)之間自動(dòng)同步。
- 簡(jiǎn)化分發(fā) — 自包含的二進(jìn)制文件不需要安裝額外的運(yùn)行時(shí)。
Boson 的設(shè)計(jì)功能使其成為想要超越瀏覽器應(yīng)用程序的 Web 開(kāi)發(fā)人員的正確選擇。該庫(kù)完全允許使用本機(jī) API,而不是通常的工作流程,即自動(dòng)將 Web 組件轉(zhuǎn)換為桌面界面。現(xiàn)成的應(yīng)用程序保留了本機(jī)程序的所有優(yōu)勢(shì),包括對(duì)文件系統(tǒng)和系統(tǒng)資源的訪(fǎng)問(wèn),同時(shí)保持跨平臺(tái)“開(kāi)箱即用”。
Boson 不是什么?
Boson 不是一個(gè) GUI 框架。 我們不是在這里決定您的應(yīng)用程序的外觀(guān)或感覺(jué)。使用最適合您的工作流程的任何前端堆棧 — React、Angular、Vue、Svelte、jQuery,或者只是經(jīng)典的 HTML 和 CSS。更喜歡 Bootstrap、Bulma 還是 Tailwind?去做吧。您的 UI,您的規(guī)則。
Boson 不啟動(dòng) HTTP 服務(wù)器 (與 NativePHP 不同)也不依賴(lài) Node.js。沒(méi)有不穩(wěn)定的解決方法,沒(méi)有額外的層,也沒(méi)有不必要的數(shù)據(jù)轉(zhuǎn)換。只需直接、簡(jiǎn)化地訪(fǎng)問(wèn)渲染器 - 所有這些都在一個(gè)統(tǒng)一的過(guò)程中完成。
Boson 不依賴(lài)繁重的依賴(lài)項(xiàng) ,也不是 Electron 或 NativePHP 的分支。它利用了作系統(tǒng)上已有的工具,使您的應(yīng)用程序保持輕量級(jí)。Boson 不像典型的 Electron 或 NativePHP 應(yīng)用程序那樣消耗數(shù)百兆字節(jié),而是將其占用空間保持在幾千字節(jié)——通過(guò)設(shè)計(jì)高效。
Boson 也沒(méi)有像 JPHP (Devel Studio/Devel Next) 那樣重新發(fā)明 PHP。它使用您已經(jīng)熟悉和喜愛(ài)的現(xiàn)代 PHP——沒(méi)有分叉,沒(méi)有驚喜。
忘記復(fù)雜的設(shè)置或自定義 PHP 擴(kuò)展。只需運(yùn)行一個(gè)命令即可開(kāi)始: composer require boson-php/runtime
— 然后您就可以開(kāi)始工作了。
安裝
運(yùn)行時(shí) — 充當(dāng) PHP 代碼和底層作系統(tǒng)之間的橋梁。這是您可以在運(yùn)行時(shí)和開(kāi)發(fā)期間使用的主要 API編譯器 – 允許您將工作結(jié)果構(gòu)建到完成的項(xiàng)目中,以便分發(fā)。
運(yùn)行時(shí)
Boson 運(yùn)行時(shí)提供了庫(kù)的核心,并允許您運(yùn)行出色的應(yīng)用程序。
Library 作為 Composer 存儲(chǔ)庫(kù)提供,可以使用以下命令安裝在項(xiàng)目的根目錄中:
composer require boson-php/runtime
不要忘記在您的應(yīng)用程序中包含自動(dòng)加載文件。
<?php
require __DIR__ . '/vendor/autoload.php';
$app = new Boson\Application();
編譯器
Boson 編譯器使您能夠?qū)⒐ぷ鹘Y(jié)果組裝成最終產(chǎn)品。也就是說(shuō),導(dǎo)入到目標(biāo)平臺(tái)的可執(zhí)行文件中。
Library 作為 Composer 存儲(chǔ)庫(kù)提供,可以使用以下命令安裝在項(xiàng)目的根目錄中:
composer require boson-php/compiler --dev
編譯器只是開(kāi)發(fā)時(shí)需要的,不是代碼執(zhí)行時(shí)需要的,因此建議將其作為 --dev
包包含在內(nèi)。
環(huán)境要求:PHP 8.4+
開(kāi)啟 ext-ffi
擴(kuò)展
案例
您可以在 Web Components 中使用 Twig。為此,您需要遵循幾個(gè)簡(jiǎn)單的步驟。
1. 在您的項(xiàng)目中安裝 Twig 組件:
composer require twig/twig
2. 創(chuàng)建 Twig 組件
之后,您應(yīng)該創(chuàng)建一個(gè)支持 twig 渲染的組件。
use Boson\WebView\Api\WebComponents\ReactiveContext;
useBoson\WebView\Api\WebComponents\WebComponent;
useBoson\WebView\WebView;
useTwig\Environment;
useTwig\TemplateWrapper;
abstractclass TwigComponent extends WebComponent
{
/**
* In this case, the template will be initialized
* once during the first render.
*/
private TemplateWrapper $template {
get => $this->template ??= $this->twig->createTemplate(
template: $this->renderTwig(),
);
}
publicfunction __construct(
protected readonly Environment $twig,
ReactiveContext $ctx,
WebView $webview,
) {
parent::__construct($ctx, $webview);
}
abstractprotectedfunction renderTwig(): string;
/**
* Override the default render behavior by
* redirecting it to a Twig template
*/
#[\Override]
finalpublicfunction render(): string
{
return$this->template->render(\get_object_vars($this));
}
}
3. 創(chuàng)建 Instantiator
現(xiàn)在我們需要定義這些組件的確切創(chuàng)建方式,為此,我們應(yīng)該創(chuàng)建自己的實(shí)例化器,它將按需返回新組件。
use Boson\WebView\Api\WebComponents\Instantiator\WebComponentInstantiatorInterface;
useBoson\WebView\Api\WebComponents\ReactiveContext;
useBoson\WebView\WebView;
useTwig\Environment;
useTwig\Loader\ArrayLoader;
final readonly class TwigComponentInstantiator implements
WebComponentInstantiatorInterface
{
private Environment $twig;
publicfunction __construct()
{
$this->twig = new Environment(new ArrayLoader());
}
privatefunction isTwigComponent(string $component): bool
{
return \is_subclass_of($component, TwigComponent::class);
}
publicfunction create(WebView $webview, ReactiveContext $context): object
{
$component = $context->component;
// Pass twig as a first argument in case of passed
// component extends from TwigComponent class
if ($this->isTwigComponent($component)) {
returnnew $component($this->twig, $context, $webview);
}
returnnew $component($context, $webview);
}
}
4. 注冊(cè)實(shí)例化器
要確定應(yīng)使用不同的實(shí)例化器,可以在 webview 配置中指定它。
$webComponentsConfig = new WebComponentsCreateInfo(
instantiator: new TwigComponentInstantiator(),
);
$applicationConfig = new ApplicationCreateInfo(
window: new WindowCreateInfo(
webview: new WebViewCreateInfo(
webComponents: $webComponentsConfig,
),
),
);
$app = new Boson\Application($applicationConfig);
5. Twig 組件
現(xiàn)在我們可以創(chuàng)建自定義 twig 組件了!
class MyTwigComponent extends TwigComponent
{
protected array $items = [1, 2, 3];
protected function renderTwig(): string
{
return <<<'twig'
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
twig;
}
}
要注冊(cè)和檢查,只需寫(xiě)幾行
$app->webview->defineComponent('my-list', MyTwigComponent::class);
$app->webview->html = '<my-list />';
?