Peachpie Compiling PHP to .NET Core with Visual Studio Code

This tutorial will show you how to set up Peachpie compiler in Visual Studio Code and compile PHP to .NET Core as simply as ever.

Up until recently, it was rather difficult to play around with Peachpie, as everything had to be done on the command line. However, this has now changed. This article will show you how to set up Peachpie compiler very easily, so that you can start experimenting with it.In this tutorial, we make use of the VS Code editor, which is easy to set up. VS Code is quite a fast editor with a built-in .NET debugger and provides a simple, comfortable PHP editing experience.

Objectives

Our intentions with this article are to ensure an easy installation of Peachpie, to integrate PHP into a comfortable development environment and to demonstrate the multi-platform nature of .NET Core and Peachpie:

    • Make PHP a fully qualified member of the dotnet languages family

The objective is for the developer to be able to compile and run the project at the push of the F5 button and to enable him to make use of the .NET debugging features.

    • PHP as a dependency to a C# project

Peachpie compiler aims to allow the PHP language as a dependency to a C# project and vice versa.

    • NuGet Distribution & Updates

New as of this week is that Peachpie as a platform is now available as a NuGet package. This implies that it is as simple as ever to download and install Peachpie, that the compiled programs are NuGet packable and that the NuGet packages can be used as dependencies for PHP projects.

    • Make it all multi-platform

Since VS Code and .NET Core are both multi-platform, let us demonstrate this feature by showing this tutorial on a Mac. Needless to say it will also work on Linux; operating systems simple aren’t a hurdle anymore. Of course, not only VS Code and .NET will run on any OS, but also Peachpie itself and any programs that were compiled by Peachpie.

Outcome

The final outcome of this tutorial is to allow for PHP to be compiled and launched in Visual Studio Code upon pressing F5, to use the .NET debugging features and to enable the insertion of breakpoints into the PHP code:

breakpoint

Setting up the project

  • Install Visual Studio Code
  • Install .NET Core (by downloading and installing the .NET Core SDK or making use of the Docker image microsoft/dotnet)
  • Create an empty project
    • Create an empty folder called ‘MyProject’
    • Open the command prompt/terminal and navigate to the folder
    • Run the command dotnet new, which creates the default Program.cs and project.json files

*Note: On a Mac, we ran into the issue of dotnet quitting unexpectedly. This can be caused by an outdated OpenSSL library, which needs to be updated. You can fix this by running the commands brew update and brew install openssl. If you still have trouble getting dotnet to run, please see the solution here.
Default Dotnet Program

Building PHP in VS Code

Open the folder that contains your project in VS Code. When you first try to run the program, VSCode will ask you to select an environment. Make sure you install the C# for Visual Studio Code extension in order to enable the ‘.NET Core’ environment:

C# extensionThe .NET Core environment is very important for our case, since we can teach it to compile the project using Peachpie instead of the default C# compiler. Proceed by selecting “.NET Core”.

Choose environment

Now delete Program.cs and create a new PHP file called index.php instead, containing

new phpproject.json and its ‘buildOptions” section. Add the following in order to compile PHP files using “a php compiler” (i.e. something that compiles PHP, which we haven’t specified yet, but we will later):

"buildOptions" : {
"compilerName": "php",
"compile": "**/*.php"
}

If you try to run the code now, VS Code will initiate the command dotnet run, which will result in an error message such as ”Command ‘dotnet-compile-php’ couldn’t be found”. In order to enable such a command, we have created – yes, indeed – a NuGet package. Add the following to project.json:

"tools": {
"Peachpie.Compiler.Tools" : "0.5.0-*"
},

*The version is 0.5.0 – it is a pre-release and will be  updated continuously
*The package contains the command above and the complete Peachpie compiler.

However, this isn’t everything just yet. PHP programs require an additional runtime, which is distributed in a NuGet package: “Peachpie.NETStandard.App”. Add the following into “frameworks” in your project.json:

{ "netcoreapp1.0" : { "dependencies" : { "Peachpie.App" : "0.5.0-*" } } }

new json>

Now we can proceed by pressing F5:

errors

In the output window, you will most likely see two errors. VS Code informs you that you should restore the NuGet packages first. It will either show you a “Restore” button at the top of the window, or you can enter the following in the terminal/command prompt: dotnet restore.

Secondly, we need to configure the task runner. Click “Configure task runner”, when prompted, select .NET Core and keep all the settings as they are by default:

task runner

Pressing F5 or typing the command dotnet run will now properly initiate Peachpie compiler, which produces the corresponding DLL file. However, you will initially see a notification telling you to configure launch.json first:

configurejson

In order to let VS Code know what it should run after a successful compilation, edit the “program” configuration in the newly created launch.json file and change its value to

${workspaceRoot}/bin/Debug/netcoreapp1.0/MyProject.dll

Of course, this depends on the name of your project. Beware that file names on Macs are case sensitive! When you change the path, make sure you have the right profile selected:

profiles

Now press F5 again, or enter dotnet build in the command prompt/terminal. To run a compiled PHP project in VS Code, type dotnet run in the command line or to run an already compiled program, type dotnet bin/Debug/netcoreapp1.0/myproject.dll.

Enabling breakpoints in PHP code

One more aspect we would like to highlight is the possibility of inserting a breakpoint into your PHP code. By default, VS Code does not allow this, and an extension must first specify that it is safe to insert breakpoints into PHP files.

You can either write this extension yourself, or make use of the previously downloaded C# for Visual Studio Code extension. Open its definition from “C:Usersusername.vscodeextensionsms-vscode.csharp-1.4.1package.json” in Windows (/Users/username/.vscode/extensions/ms-vscode.csharp-1.4.1/package.json on a Mac), search for “enableBreakpointsFor” and add “php” to the list in “languageIds”:

languageids

Now save the file and restart VS Code. Behold, you are now able to insert breakpoints into PHP files (either by pressing F9 or clicking to the left of your code):

breakpoint

Pressing F5 will run your project and stop on a breakpoint. Enjoy the view of the local variables and the strange callstack.

If you have questions about Peachpie compiler, join our community chat on Gitter and make sure you follow our progress on GitHub, Twitter and Facebook.

EDIT: We updated the version 0.1.1 to 0.5.0 and names of NuGet packages. Please keep in mind that this will keep changing continuously.

Share this article:Share on FacebookShare on VKPin on PinterestShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit
Posted on September 6, 2016, in category Announcement, Information, Tutorial, tags: , , , , , ,