PHP code example of sashabo / nginx-conf-parser

1. Go to this page and download the library: Download sashabo/nginx-conf-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sashabo / nginx-conf-parser example snippets


class Row
{
    public readonly string $name;
    /** @var array<string> */
    public readonly array $values;
    /** @var array<self> */
    public readonly array $rows;
    public readonly ?string $file;
    /** @var non-negative-int */
    public readonly int $position;
    /** @var non-negative-int */
    public readonly int $line;
    /** @var non-negative-int */
    public readonly int $linePosition;
    /** @var positive-int */
    public readonly int $length;
}

class Finder
{
    /**
     * @param array<Row> $rows
     * @return array<Row>
     */
    public static function findByName(array $rows, string $name): array

    /**
     * @param array<Row> $rows
     * @return ?Row
     */
    public static function findOneByName(array $rows, string $name): ?Row

    /**
     * @param array<Row>    $rows
     * @param array<string> $path
     * @return array<Row>
     */
    public static function findByPath(array $rows, array $path): array

    /**
     * @param array<Row>    $rows
     * @param array<string> $path
     * @return ?Row
     */
    public static function findOneByPath(array $rows, array $path): ?Row
}

class VariableReplacer
{
    public const MISSED_SET_EMPTY = 'set-empty';
    public const MISSED_IGNORE = 'ignore';
    public const MISSED_THROW_EXCEPTION = 'exception';

    /**
     * @param array<Row>            $rows
     * @param array<string, string> $variables
     * @return array<Row>
     */
    public static function replace(array $rows, array $variables, string $missedMode = self::MISSED_IGNORE): array

    public static function replaceRow(Row $row, array $variables, string $missedMode = self::MISSED_IGNORE): Row
}

$rows = VariableReplacer::replace(
    Parser::parseFile('/etc/nginx/nginx.conf'),
    [
        'server_port' => 80,
        'server_root' => '/app/public'
    ]
);


server {
    listen $server_port;
    root $server_root;

    location / {
        try_files $uri @php;
    }
    location @php {