### Install PeachPie .NET Templates
Source: https://docs.peachpie.io/get-started
Installs the latest PeachPie project templates for .NET. This command downloads the necessary templates to create new PeachPie projects.
```bash
dotnet new -i "Peachpie.Templates::*"
```
--------------------------------
### Create and Run PeachPie Console Application
Source: https://docs.peachpie.io/get-started
Creates a new PeachPie console application in the current directory and then runs it. The first execution may take longer due to dependency downloads and compilation.
```bash
dotnet new console -lang PHP
dotnet run
```
--------------------------------
### Create and Build PeachPie Class Library
Source: https://docs.peachpie.io/get-started
Creates a new PeachPie class library project and builds it into a .dll file. This library can be referenced by other C# or PHP projects.
```bash
dotnet new classlib -lang PHP
dotnet build
```
--------------------------------
### Create and Run PeachPie Web Application
Source: https://docs.peachpie.io/get-started
Creates a new PeachPie ASP.NET Core web application and runs it. This project type compiles PHP files into a website served by a built-in web server.
```bash
dotnet new web -lang PHP
dotnet run --project Server
```
--------------------------------
### Sample PHP File
Source: https://docs.peachpie.io/net/hosting/aspnetcore
A basic PHP file that outputs 'Hello World!'. This serves as a minimal example for a PHP project.
```php
```
--------------------------------
### Create PeachPie Console Application in VS Code
Source: https://docs.peachpie.io/get-started
Creates a new PeachPie console application within an empty folder using Visual Studio Code's terminal. This is the initial step for developing a PeachPie console app in VS Code.
```bash
dotnet new console -lang PHP
```
--------------------------------
### Get or Create Web Context (ASP.NET Core)
Source: https://docs.peachpie.io/api/ref/context
Retrieves or creates a web context instance from an existing `Microsoft.AspNetCore.Http.HttpContext`. This requires adding a reference to the `Peachpie.AspNetCore.Web` package.
```C#
using Peachpie.AspNetCore.Web;
// Assuming 'httpContext' is an instance of Microsoft.AspNetCore.Http.HttpContext
Peachpie.Runtime.Context context = httpContext.GetOrCreateContext(httpContext);
```
--------------------------------
### PHP Console App Output Example
Source: https://docs.peachpie.io/scenarios/beginner/vs-console-app
This snippet demonstrates the expected output when a PHP console application built with PeachPie runs successfully, including the 'Hello World!' message and process exit code.
```text
Hello World!\nConsoleApp2.exe (process 14116) exited with code 0.\nPress any key to close this window . . .\n
```
--------------------------------
### Configure Additional Web Static Assets in Startup.cs
Source: https://docs.peachpie.io/scenarios/blazor/inserting-php-scripts
This C# code snippet demonstrates how to integrate `UseAdditionalWebStaticAssets` into the ASP.NET Core `Startup` class to enable serving static files from custom locations. It assumes necessary configurations are present in `appsettings.json`.
```csharp
namespace BlazorApp.Server
{
public class Startup
{
public IConfiguration Configuration { get; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
...
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseAdditionalWebStaticAssets(Configuration);
app.UseRouting();
...
}
}
}
```
--------------------------------
### Sample MSBuild Project File for Peachpie
Source: https://docs.peachpie.io/php/msbuild
An example MSBuild project file (`.msbuildproj`) that configures a Peachpie project. It specifies the Peachpie SDK, output type as a library, targets .NET Standard 2.1, and includes all .php files for compilation.
```xml
librarynetstandard2.1
```
--------------------------------
### Add PeachPie NuGet Feed (CLI)
Source: https://docs.peachpie.io/download
This command adds the PeachPie NuGet feed to your local configuration. Replace '{{PASSWORD}}' with your actual Patreon password. Ensure you have the .NET SDK installed.
```bash
dotnet nuget add source https://feed.peachpie.io/v3/index.json -n "peachpie feed" -u PAT -p {{PASSWORD}}
```
--------------------------------
### Target Development Build of PeachPie in Project File
Source: https://docs.peachpie.io/scenarios/intermediate/debugging-peachpie
Example of a project file (`.msbuildproj`) configured to target a development build of PeachPie. It sets the Sdk to a versioned development build.
```xml
librarynetstandard2.0
```
--------------------------------
### PHP Main Function for Console App
Source: https://docs.peachpie.io/scenarios/beginner/vs-console-app
Defines the main entry point function for a PeachPie PHP console application. This function, named 'main', is intended to be called when the application starts.
```php
infoTag->writeWithTreeBuilder($builder, 0);
}
public function OnInitialized() : void
{
parent::OnInitialized();
$this->infoTag = new \Peachpie\Blazor\Tag("p");
$this->infoTag->content[] = new \Peachpie\Blazor\Text("Hello world");
}
}
```
--------------------------------
### Peachpie Blazor SDK Project Configuration (MSBuild)
Source: https://docs.peachpie.io/scenarios/blazor/inserting-php-scripts
This MSBuild project file configures a Peachpie project for Blazor integration. It specifies the output type as a library, targets .NET 5, and sets ProduceReferenceAssembly to false, which is typical for Peachpie Blazor SDK projects.
```xml
librarynet5.0false
```
--------------------------------
### Associate PHP Options with .NET Configuration Delegates in PeachPie
Source: https://docs.peachpie.io/api/Libraries-Configuration
This code shows how to register standard PHP options (like ini_get, ini_set) with PeachPie, associating them with a delegate that gets or sets values from a registered .NET configuration container.
```csharp
StandardPhpOptions.Register("option.name", IniFlags.HasDefaultValue, (IPhpConfigurationService configService, string value) =>
{
// Setter logic here, accessing configurations via configService
// Example: configService.GetConfiguration().SomeOption = value;
});
StandardPhpOptions.Register("option.name", IniFlags.HasDefaultValue, (IPhpConfigurationService configService) =>
{
// Getter logic here, accessing configurations via configService
// Example: return configService.GetConfiguration().SomeOption;
});
```
--------------------------------
### Using Exposed C# Classes/Interfaces in PHP
Source: https://docs.peachpie.io/api/Libraries-Architecture
Illustrates the usage of C# classes exposed via PeachPie in PHP code. This example shows how to instantiate a C# class, call its methods, access static fields, and reference class constants and context-specific static fields/constants as they would appear in PHP.
```php
(new ArrayIterator())->foo([]);
echo ArrayIterator::$StaticField;
echo ArrayIterator::$ContextStaticField;
echo ArrayIterator::A_CLASS_CONSTANT;
echo ArrayIterator::ContextConstant;
```
--------------------------------
### PHP Generics: Instantiation, Static and Instance Methods
Source: https://docs.peachpie.io/net/generics
Demonstrates the usage of generic types and methods in PHP with PeachPie. Includes examples for instantiating generic types, calling generic static methods, and invoking generic instance methods. Type arguments must be resolved at compile time and cannot be variables or expressions.
```php
;
// calling a generic static method
System\Activator::CreateInstance