peachpie_Peachpie-ASP.NET Core下针对.NET和WordPress的开源PHP编译器

peachpie

peachpie

The Peachpie PHP compiler project joined the .NET Foundation this week and I'm trying to get my head around it. PHP in .NET? PHP on .NET? Under .NET? What compiles to what? Why would I want this? How does it work? Does it feel awesome or does it feel gross?

Peachpie PHP编译器项目本周加入了.NET Foundation ,我正在努力解决这个问题。 .NET中PHP? .NET上PHP? 在.NET下? 什么编译成什么? 我为什么要这个? 它是如何工作的? 感觉很棒还是感觉很粗糙?

image

Just drink this in.

只是喝这个。

C:\Users\scott\Desktop\peachcon> type program.php
<?php

function main()
{
echo "Hello .NET World!";
}

main();

C:\Users\scott\Desktop\peachcon> dotnet run
Hello .NET World!

Just like that. Starting from a .NET SDK (They say 1.1, although I used a 2.0 preview) you just add their templates

就这样从.NET SDK(他们说1.1,尽管我使用2.0预览版)开始,您只需添加其模板

dotnet new -i Peachpie.Templates::*

Then dotnet new now shows a bunch of php options.

然后dotnet new现在显示了一堆php选项。

C:\Users\scott\Desktop\peachcon> dotnet new | find /i "php"
Peachpie console application peachpie-console PHP Console
Peachpie Class library peachpie-classlibrary PHP Library
Peachpie web application peachpie-web PHP Web/Empty

dotnet new peachpie-console for example, then dotnet restore and dotnet run. Boom.

例如,dotnet新的peachpie-console,然后还原dotnet并运行dotnet。 繁荣。

NOTE: I did have to comment out his one line "<Import Project="$(CSharpDesignTimeTargetsPath)" />" in their project file that doesn't work at the command line. It's some hack they did to make things work in Visual Studio but I'm using VS Code. I'm sure it's an alpha-point-in-time thing.

注意:我确实必须注释掉他的项目文件中的一行“ <Import Project =“ $(CSharpDesignTimeTargetsPath)” />“在命令行中不起作用。 他们确实做了一些技巧,以使它们在Visual Studio中正常工作,但是我正在使用VS Code。 我确定这是一个即时点。

It's really compiling PHP into .NET Intermediate Language!

它实际上是将PHP编译为.NET中间语言!

PHP to .NET

You can see my string here:

您可以在这里看到我的字符串:

Hello .NET World inside a PHP app inside the CLR

But...why? Here's what they say, and much of it makes sense to me.

但为什么? 这就是他们说的话,其中大部分对我来说很有意义。

  1. Performance: compiled code is fast and also optimized by the .NET Just-in-Time Compiler for your actual system. Additionally, the .NET performance profiler may be used to resolve bottlenecks.

    性能:编译后的代码速度很快,而且.NET即时编译器还为您的实际系统进行了优化。 此外,.NET性能分析器可用于解决瓶颈。

  2. C# Extensibility: plugin functionality can be implemented in a separate C# project and/or PHP plugins may use .NET libraries.

    C#可扩展性:插件功能可以在单独的C#项目中实现,并且/或者PHP插件可以使用.NET库。

  3. Sourceless distribution: after the compilation, most of the source files are not needed.

    无源分发:编译后,不需要大多数源文件。

  4. Power of .NET: Peachpie allows the compiled WordPress clone to run in a .NET JIT'ted, secure and manageable environment, updated through windows update.

    .NET的功能: Peachpie允许编译的WordPress克隆在.NET JIT设置,安全且可管理的环境中运行,并通过Windows Update进行更新。

  5. No need to install PHP: Peachpie is a modern compiler platform and runtime distributed as a dependency to your .NET project. It is downloaded automatically on demand as a NuGet package or it can be even deployed standalone together with the compiled application as its library dependency.

    无需安装PHP: Peachpie是一个现代的编译器平台,其运行时作为对.NET项目的依赖项进行分发。 它可以根据需要作为NuGet软件包自动下载,或者甚至可以与已编译的应用程序一起作为库依赖项独立部署。

PHP does have other VMs/Runtimes that are used (beyond just PHP.exe) but the idea that I could reuse code between PHP and C# is attractive, not to mention the "PHP as dependency" part. Imagine if I have an existing .NET shop or project and now I want to integrate something like WordPress?

PHP确实还有其他使用的VM /运行时(除了PHP.exe),但是我可以在PHP和C#之间重用代码的想法很有吸引力,更不用说“ PHP作为依赖项”部分了。 想象一下,如果我有一个现有的.NET商店或项目,现在想集成WordPress之类的东西?

ASP.NET Core下PHP (PHP under ASP.NET Core)

Their Web Sample is even MORE interesting, as they've implemented PHP as ASP.NET Middleware. Check this out. See where they pass in the PHP app as an assembly they compiled?

他们的Web示例甚至更有趣,因为他们已经将PHP实现为ASP.NET中间件。 看一下这个。 看到它们作为编译的程序集在PHP应用程序中传递到哪里?

using Peachpie.Web;

namespace peachweb.Server
{
class Program
{
static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5004/")
.UseStartup<Startup>()
.Build();

host.Run();
}
}

class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Adds a default in-memory implementation of IDistributedCache.
services.AddDistributedMemoryCache();

services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.CookieHttpOnly = true;
});
}

public void Configure(IApplicationBuilder app)
{
app.UseSession();

app.UsePhp(new PhpRequestOptions(scriptAssemblyName: "peachweb"));
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
}

Interesting, but it's still Hello World. Let's run WordPress under PeachPie (and hence, under .NET). I'll run MySQL in a local Docker container for simplicity:

有趣,但仍然是Hello World。 让我们在PeachPie下(因此在.NET下)运行WordPress。 为了简单起见,我将在本地Docker容器中运行MySQL:

docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -p 3306:3306 -d mysql

I downloaded WordPress from here (note they have the "app" bootstrapper" that hosts .NET and then runs WordPress) restore and run.

从此处下载了WordPress (请注意,它们具有承载.NET的“应用”引导程序”,然后运行WordPress )还原并运行。

WordPress under .NET Core

It's early and it's alpha - so set your expectations appropriately - but it's surprisingly useful and appears to be under active development.

它很早,而且是alpha版本-因此要适当设置您的期望-但它出奇的有用,并且似乎正在积极开发中。

What do you think?

你怎么看?

Be sure to explore their resources at http://www.peachpie.io/resources and watch their video of WordPress running on .NET. It's all Open Source, in the .NET Foundation, and the code is up at https://github.com/iolevel/ and you can get started here: http://www.peachpie.io/getstarted

确保在http://www.peachpie.io/resources上浏览他们的资源,并观看他们在.NET上运行的WordPress视频。 它全部是.NET Foundation中的开源代码,代码位于https://github.com/iolevel/ ,您可以在此处开始: http : //www.peachpie.io/getstarted

翻译自: https://www.hanselman.com/blog/peachpie-open-source-php-compiler-to-net-and-wordpress-under-aspnet-core

peachpie

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值