介绍
Boson 是一个创新的跨平台桌面应用程序开发平台,它释放了 Web 技术(PHP、JavaScript、HTML、CSS)的强大功能以及本机汇编的优势。它的主要功能是将基于 Chromium 的 WebView 引擎和 PHP 解释器直接集成到可执行文件应用程序中。
该解决方案允许开发人员:
- 使用熟悉的堆栈技术 — 通过 HTML/CSS 创建接口并在 PHP(使用 JavaScript 元素)中实现逻辑,而无需学习特定于平台的语言。
- 通过 Chromium 引擎实现稳定渲染,在 Windows、macOS 和 Linux 上提供可靠的用户体验 。
- 减少单个代码库计数器的开发时间 — 更改在所有平台之间自动同步。
- 简化分发 — 自包含的二进制文件不需要安装额外的运行时。
Boson 的设计功能使其成为想要超越浏览器应用程序的 Web 开发人员的正确选择。该库完全允许使用本机 API,而不是通常的工作流程,即自动将 Web 组件转换为桌面界面。现成的应用程序保留了本机程序的所有优势,包括对文件系统和系统资源的访问,同时保持跨平台“开箱即用”。
Boson 不是什么?
Boson 不是一个 GUI 框架。 我们不是在这里决定您的应用程序的外观或感觉。使用最适合您的工作流程的任何前端堆栈 — React、Angular、Vue、Svelte、jQuery,或者只是经典的 HTML 和 CSS。更喜欢 Bootstrap、Bulma 还是 Tailwind?去做吧。您的 UI,您的规则。
Boson 不启动 HTTP 服务器 (与 NativePHP 不同)也不依赖 Node.js。没有不稳定的解决方法,没有额外的层,也没有不必要的数据转换。只需直接、简化地访问渲染器 - 所有这些都在一个统一的过程中完成。
Boson 不依赖繁重的依赖项 ,也不是 Electron 或 NativePHP 的分支。它利用了作系统上已有的工具,使您的应用程序保持轻量级。Boson 不像典型的 Electron 或 NativePHP 应用程序那样消耗数百兆字节,而是将其占用空间保持在几千字节——通过设计高效。
Boson 也没有像 JPHP (Devel Studio/Devel Next) 那样重新发明 PHP。它使用您已经熟悉和喜爱的现代 PHP——没有分叉,没有惊喜。
忘记复杂的设置或自定义 PHP 扩展。只需运行一个命令即可开始: composer require boson-php/runtime
— 然后您就可以开始工作了。
安装
运行时 — 充当 PHP 代码和底层作系统之间的桥梁。这是您可以在运行时和开发期间使用的主要 API编译器 – 允许您将工作结果构建到完成的项目中,以便分发。
运行时
Boson 运行时提供了库的核心,并允许您运行出色的应用程序。
Library 作为 Composer 存储库提供,可以使用以下命令安装在项目的根目录中:
composer require boson-php/runtime
不要忘记在您的应用程序中包含自动加载文件。
<?php
require __DIR__ . '/vendor/autoload.php';
$app = new Boson\Application();
编译器
Boson 编译器使您能够将工作结果组装成最终产品。也就是说,导入到目标平台的可执行文件中。
Library 作为 Composer 存储库提供,可以使用以下命令安装在项目的根目录中:
composer require boson-php/compiler --dev
编译器只是开发时需要的,不是代码执行时需要的,因此建议将其作为 --dev
包包含在内。
环境要求:PHP 8.4+
开启 ext-ffi
扩展
案例
您可以在 Web Components 中使用 Twig。为此,您需要遵循几个简单的步骤。
1. 在您的项目中安装 Twig 组件:
composer require twig/twig
2. 创建 Twig 组件
之后,您应该创建一个支持 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. 创建 Instantiator
现在我们需要定义这些组件的确切创建方式,为此,我们应该创建自己的实例化器,它将按需返回新组件。
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. 注册实例化器
要确定应使用不同的实例化器,可以在 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 组件
现在我们可以创建自定义 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;
}
}
要注册和检查,只需写几行
$app->webview->defineComponent('my-list', MyTwigComponent::class);
$app->webview->html = '<my-list />';