PHPackages                             axy/nginx-config-syntax - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. axy/nginx-config-syntax

ActiveLibrary

axy/nginx-config-syntax
=======================

Builds config file with nginx syntax

0.2.0(3y ago)127[1 issues](https://github.com/axypro/nginx-config-syntax/issues)1MITPHPPHP &gt;=8.1

Since Jun 11Pushed 3y ago2 watchersCompare

[ Source](https://github.com/axypro/nginx-config-syntax)[ Packagist](https://packagist.org/packages/axy/nginx-config-syntax)[ Docs](https://github.com/axypro/nginx-config-syntax)[ RSS](/packages/axy-nginx-config-syntax/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (5)Used By (1)

axy\\nginx\\config\\syntax
==========================

[](#axynginxconfigsyntax)

[![Latest Stable Version](https://camo.githubusercontent.com/da5698f9f354e37a952d1b7e0697bf7131f57c2ddf09dd14bce6611b8cbdd342/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6178792f6e67696e782d636f6e6669672d73796e7461782e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/axy/nginx-config-syntax)[![Minimum PHP Version](https://camo.githubusercontent.com/2d18ce514c7016022dad012ac9e39a8b6f47cc411b2daff6627cbf208f8cea63/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e312d3838393242462e7376673f7374796c653d666c61742d737175617265)](https://php.net/)[![Tests](https://github.com/axypro/nginx-config-syntax/actions/workflows/test.yml/badge.svg)](https://github.com/axypro/nginx-config-syntax/actions/workflows/test.yml)[![Coverage Status](https://camo.githubusercontent.com/b80efd819e9a5fd1c199d5a525cad1a31ab4940991ed0c7f06d53da588f222e9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f61787970726f2f6e67696e782d636f6e6669672d73796e7461782f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/axypro/nginx-config-syntax?branch=master)[![License](https://camo.githubusercontent.com/4d9d5563db6a9737c017a026d44e9db360b15cd9b5259c986f2849ce89aa40fa/68747470733a2f2f706f7365722e707567782e6f72672f6178792f6e67696e782d636f6e6669672d73796e7461782f6c6963656e7365)](LICENSE)

Builds a file with syntax similar to nginx config. Nginx-specific directives are implemented in a separate package.

Structure
---------

[](#structure)

- There is a "context" on the top level
- Context contains list of items
- An item can be "directive" or "comment"
- There are two type of directives: single and block
- A directive of any type has "name" and list of parameters (can be empty, a parameter is a string)
- A block directive has nested context
- Nested context contains list of items, etc. The number of levels is not limited

Syntax
------

[](#syntax)

```
# The main context
# Comment line starts with "#"
# Indents doesn't matter
# Empty strings doesn't matter

single_directive;
single_directive_with_parameters one two three;

block_directive param1 param2 {
    # Nested context
    nested_single_directive;
    nested_block_directive {
        nested_nested_single_directive param;
    }
}

```

Example
-------

[](#example)

```
use axy\nginx\config\syntax\Context;

$main = new Context();
$main->comment->set([
    'The main context',
    'Comment line starts with "#"', 'Indents doesn\'t matter',
    'Empty strings doesn\'t matter',
]);

$main->append('');
$main->single('single_directive');
$directive = $main->single('single_directive_with_parameters', ['one', 'two']);
$directive->params[] = 'three';
$main->append('');

$block = $main->block('block_directive', ['param1', 'param2']);
$block->context->single('nested_single_directive');
$nested = $block->context->block('nested_block_directive');
$nested->context->single('nested_nested_single_directive', 'param');
$block->context->comment->set('Nested context');

echo $main->render();
```

Result is the text from "syntax" section.

Class hierarchy
---------------

[](#class-hierarchy)

- `BaseItem`
    - `BaseContext`
        - `Context`
            - `DirectiveContext`
    - `BaseDirective`
        - `SingleDirective`
            - `CustomSingleDirective`
        - `BlockDirective`
            - `CustomBlockDirective`
- `Comment`

### Context

[](#context)

A `context` has the public array `$items`. When a context is rendering it renders all items in order.

An item can be any value (object or scalar) that can be cast to a string.

```
$context->append(new CustomeSingleDirective('name')); // append BaseItem
$context->append('other_directive;'); // or just a string
```

An item is rendered on a separate line (or several lines) with indentation of the current context. If a scalar item converts to empty line it will take one empty line in the output. If an object item converts to empty line it will take no lines.

Methods and properties:

- Can work with the `$items` array directly
- `append($item): void` - appends an item to the array
- `single($name [, $params]): CustomSingleDirective` - helper for easy creation of a single directive (see below). Creates, appends and returns.
- `block($name [, $params]): CustomBlockDirective` - helper for block directive creation

### Directives

[](#directives)

`CustomSingleDirective` and `CustomBlockDirective` used for easy creation directives. They take as arguments in the constructor:

- `$name` (string) - the directive name
- `$params` - list of parameters
    - available as public property and can be changed afterwards
    - NULL - no params
    - string - displayed as is (without escaping)
    - string\[\] - list of parameters, escaped and imploded
        - escaped spaces and quoted
        - *not sure if this is correct*

```
$directive = new CustomSingleDirective('my_directive', 'param');
echo $directive; // my_directive param;

```

Base classes `SingleDirective` and `BlockDirective` are used as parent for specific directive classes.

### Block directive context

[](#block-directive-context)

`BlockDirective` has the public property `$context`.

```
$block = new CustomBlockDirective('block');
$block->context->append('one');
$block->append('two'); // alias for $block->context->append()

```

Result:

```
block {
    one
    two
}

```

There are `$topContext` and `$mainContext` (protected) also. They are appended to `$context` automatically. Usually it is enough to use `$context` only.

But a specific directive class should write its content to the `$mainContext`. If the user want to add custom directives they can use `$context->appned()` (append to the bottom) or `$topContext->append()`.

### Disable directive

[](#disable-directive)

All directive objects is enabled by default. But can be disabled and will not be displayed.

- `isEnabled(): bool`
- `enable(): void`
- `disable(): void`

### Comment

[](#comment)

All directive and context objects have the public property `comment`.

```
$directive = new CustomSingleDirective('name');
$directive->comment->set('It is directive');
```

Result:

```
# It is directive
name;

```

`set()` can take:

- string - the comment content, can be multiline
- string\[\] - will be imploded
- NULL - no comment (by default)
- `delete()` is alias for `set(null)`

Can set comment for a block directive and for its context:

```
$block = new CustomBlockDirective('block');
$block->context->single('single');
$block->comment->set('It is directive');
$block->context->comment->set('It is context');
```

Result:

```
# It is directive
block {
    # It is context
    single;
}

```

Render
------

[](#render)

All `BaseItem` implement `__toString()`, but there are nuances:

- There may be trailing spaces
- Tabs are used for indentation (one tab per nested level)
- "\\n" for line breaks

For formatting on the top level can be used `render()` method. Arguments:

- `$indent` (string, 4 spaces by default) - replaces tabs
- `$clear` (bool, FALSE by default) - deletes comment lines and empty lines
- `$rtrim` (bool, TRUE by default) - deletes tailing spaces, empty lines at the end and ends the file by one blank line
- `$nl` (string, "\\n" by default) - line break symbol

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~214 days

Total

4

Last Release

1159d ago

PHP version history (2 changes)0.1.0PHP &gt;=8.0

0.2.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/e5b3a2db8d5614167d4510019e3373dbba63c6aade58d38d23f3e7d2e63c3c4a?d=identicon)[axy](/maintainers/axy)

---

Top Contributors

[![vasa-c](https://avatars.githubusercontent.com/u/557081?v=4)](https://github.com/vasa-c "vasa-c (12 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/axy-nginx-config-syntax/health.svg)

```
[![Health](https://phpackages.com/badges/axy-nginx-config-syntax/health.svg)](https://phpackages.com/packages/axy-nginx-config-syntax)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
