概述
Perry 是一个跨平台 UI 代码生成框架——你可以用 PHP 定义 UI,为 11 个平台生成原生代码。
用 PHP 写一次 UI,然后生成多平台原生代码,支持的目标平台:Apple(SwiftUI)、Android(Jetpack Compose)、Web(HTML/CSS/JS),以及 Windows、Linux 等。
它不是运行时框架,而是一个编译/生成器:你在 PHP 中声明界面和逻辑,库会输出对应平台的代码。
工作原理
PHP 微件树 (DSL) │ ▼代码生成后端 (例如 SwiftUIBackend、HtmlBackend) │ ▼原生源代码 (Swift、HTML、Kotlin、C# 等) │ ▼平台工具链 (swiftc、dotnet、gradle 等) │ ▼原生应用 (.app、.exe、.apk 等)
安装
composer require perry/perry
第一个应用
创建一个简单的计数器
<?phpuse Perry\App;use Perry\UI\Binding;use Perry\UI\Styling\Style;use Perry\UI\Widget\Button;use Perry\UI\Widget\HStack;use Perry\UI\Widget\Text;use Perry\UI\Widget\VStack;$count = new Binding('count', 0);$app = new App();$app->setRoot(new VStack( (new Text($count))->style(Style::make()->fontSize(48)),new HStack( (new Button('-', function()use($count){ $count -= 1; }))->style(Style::make()->fontSize(24)->padding(16)), (new Button('+', function()use($count){ $count += 1; }))->style(Style::make()->fontSize(24)->padding(16)), ), ));// 生成 Web 代码echo $app->generateCode('html');
生成代码
# 生成 SwiftUI 代码(macOS/iOS)php your-app.php > App.swift# 生成 HTMLphp your-app.php > index.html# 生成 Jetpack Composephp your-app.php > MainActivity.kt# 生成 WPF/XAML( Windows)php your-app.php > MainWindow.xaml# 生成 GTK4 XML(Linux)php your-app.php > app.ui# 生成 ArkTS(HarmonyOS)php your-app.php > pages/index.ets# 生成 Flutter Dartphp your-app.php > main.dart
使用 CLI
./bin/perry info # 查看平台信息./bin/perry demo --target=macos # 生成演示代码./bin/perry codegen --target=web # 为指定后端生成代码./bin/perry compile --target=macos # 编译为可执行文件./bin/perry targets # 列出所有 15 个目标平台./bin/perry backends # 列出所有代码生成后端
仓库文档:https://yangweijie.github.io/perry-php/zh