![peachpie](https://i-blog.csdnimg.cn/blog_migrate/a3d993eacd1006774bac9907d1e587b7.png)
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 image](https://i-blog.csdnimg.cn/blog_migrate/96da82115a6be355ea72053b9a9fc426.png)
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 PHP to .NET](https://i-blog.csdnimg.cn/blog_migrate/40e21f40421838b27340feb6b567666a.png)
You can see my string here:
您可以在这里看到我的字符串:
![Hello .NET World inside a PHP app inside the CLR Hello .NET World inside a PHP app inside the CLR](https://i-blog.csdnimg.cn/blog_migrate/a95a66e3d6cc80f3da4bcd7d3e26be21.png)
But...why? Here's what they say, and much of it makes sense to me.
但为什么? 这就是他们说的话,其中大部分对我来说很有意义。
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性能分析器可用于解决瓶颈。
C# Extensibility: plugin functionality can be implemented in a separate C# project and/or PHP plugins may use .NET libraries.
C#可扩展性:插件功能可以在单独的C#项目中实现,并且/或者PHP插件可以使用.NET库。
Sourceless distribution: after the compilation, most of the source files are not needed.
无源分发:编译后,不需要大多数源文件。
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进行更新。
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 WordPress under .NET Core](https://i-blog.csdnimg.cn/blog_migrate/652441e43f9a9915023824b4048e3f24.png)
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
peachpie