Peachpie 0.3.0 and Visual Studio Code Extension

Although we still haven’t quite arrived at version 1.0.0, we recently introduced some interesting and useful features in Peachpie. This week, we released version 0.3.0 and there are some exciting additions to the compiler.

Recap

Peachpie is a compiler of PHP built on top of Microsoft Roslyn. The resulting binaries will be fully compatible with the traditional PHP 7 runtime, while providing interoperability features with the .NET Framework (as well as .NET Core and Mono). In addition, Peachpie provides increased performance and security, since a lot of potential issues may be handled during the compilation and in-depth analysis process.

New Version – 0.3.0

Peachpie is an open source project, and therefore it is crucial for its development that enthusiasts try it out. This is why we are releasing the updated NuGet packages and increasing the minor version number by one, because there have been many additions we would love for developers to try and give us feedback on.

"dependencies": {
    "Peachpie.App": "0.3.0-*"
  },
  "tools": {
    "Peachpie.Compiler.Tools": "0.3.0-*"
  },

The compiler is now another step closer to running something bigger than a simple “Hello World” app; let’s take a look at some of the changes.

Generating XML documentation from PHP sources

Since Peachpie compiles the PHP code to .NET assemblies that can be seamlessly referenced from within C# or another .NET language, it would be nice to see a documentation of PHP symbols (classes, functions, parameters etc.) in the C# editor. That is why Peachpie now compiles PHP PHPDoc comments and builds an XML file with annotations that Visual Studio and other IDEs automatically process and show you in the C# editor.

This is an equivalent of a PHP code in C# when we use ILSpy tool to de-compile compiled DLL with xmlDoc:

/// <summary>
/// WordPress Error class.
/// Container for checking for WordPress errors and error messages. Return
/// WP_Error and use is_wp_error() to check if this class is returned. Many
/// core WordPress functions pass this class in the event of an error and
/// if not handled properly will result in code errors.</summary>
public class WP_Error {
	/// <summary>
	/// Initialize the error.
	/// If `$code` is empty, the other parameters will be ignored.
	/// When `$code` is not empty, `$message` will be used even if
	/// it is empty. The `$data` parameter will be used only if it
	/// is not empty.
	///
	/// Though the class is constructed with a single error code and
	/// message, multiple codes can be added using the `add()` method.</summary>
	/// <param name="code">Error code</param>
	/// <param name="message">Error message</param>
	/// <param name="data">Optional. Error data.</param>
	public virtual PhpValue __construct(PhpValue code = "", string message = "", PhpValue data = "");

	public virtual PhpArray get_error_codes();

	public virtual PhpValue get_error_code();

	/// <summary>
	/// Retrieve all error messages or error messages matching code.</summary>
	/// <param name="code">Optional. Retrieve messages matching code, if exists.</param>
	/// <returns>Error strings on success, or empty array on failure (if using code parameter).</returns>
	public virtual PhpArray get_error_messages(PhpValue code = "");

You can clearly see documented and undocumented constructs as well as how .NET tools like ILSpy treats compiled PHP code the same as C#.

Starting with Peachpie 0.3.0, aside from a compiled PHP library, an XML file can be built, containing the source documentation used by the IDE and other tools, like HTML documentation generators. To enable generating xmlDoc, either pass /doc argument to peach.exe compiler (https://github.com/iolevel/peachpie/wiki/peach.exe) or set `”xmlDoc”:true` in project.json when targeting .NET Core (https://docs.microsoft.com/en-us/dotnet/articles/core/tools/project-json). When using such a compiled PHP library in C#, IntelliSense shows all the information contained in the corresponding PHP PHPDoc comments.

This greatly improves interoperability between PHP and .NET since developers now have the development documentation on hand.

Plenty of new functions

With this release, we have implemented a lot of standard PHP functions like “header”, “base64_encode” or “function_exists” into Peachpie. This task is important for the full compatibility with conventional PHP code so that existing PHP apps may be compiled with only little or no changes at all.

Extending Peachpie with new functions and classes is straightforward; nevertheless, it is crucial for the project’s applicability. Refer to https://github.com/iolevel/peachpie/wiki/Libraries-Architecture to see how extensions are made.

ASP.NET Core Request Delegate

It wouldn’t be PHP if we couldn’t run it on a web server. We improved the integration with ASP.NET and ASP.NET Core, so that compiled PHP can take advantage of the great ASP.NET pipeline and performance. In addition, the Peachpie API provides enough extension points to enable an integration with any other (.NET capable) web server.

To setup your ASP.NET Core pipeline with support for PHP, add dependency to

"Peachpie.NETCore.Web": "0.3.0-*",

and adjust your startup configure method with

app.UsePhp();

For the full example see github.com/iolevel/peachpie-samples/web-application.

Compilation Enhancements

There are still some PHP constructs we cannot compile. Currently traits, closures, eval, generators and some operators will intentionally cause the compiler to crash. However, with 0.3.0 we have implemented a lot of standard operators and fixed some of the old ones, moving the project another step closer to our goals.

Peachpie for Visual Studio Code Extension

To make developing with Peachpie even easier, download our extension for VSCode, which makes it simpler to create a project and set your first .NET breakpoint in PHP code. Then just hit F5 to compile and run the code. The extension is available on the Visual Studio Marketplace, or you can simply install it right in VSCode using the command ext install peachpie-vscode.

Watch our tutorial on how to install and use the Peachpie for Visual Studio Code extension:

Share this article:Share on FacebookShare on VKPin on PinterestShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit
Loading Facebook Comments ...

One thought on “Peachpie 0.3.0 and Visual Studio Code Extension

Leave a Reply