PHPackages                             jalle19/haphproxy - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. jalle19/haphproxy

AbandonedArchivedLibrary[Parsing &amp; Serialization](/categories/parsing)

jalle19/haphproxy
=================

A HAProxy configuration parser for PHP

0.3.0(9y ago)69.4k↓25%2GPL-2.0+PHPPHP &gt;=5.6.0

Since Jul 29Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Jalle19/haphproxy)[ Packagist](https://packagist.org/packages/jalle19/haphproxy)[ RSS](/packages/jalle19-haphproxy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (6)Used By (0)

haphproxy
=========

[](#haphproxy)

[![Build Status](https://camo.githubusercontent.com/44fa0178735bb7a4d087eb0a4fbe61c5e8a58772f25f6b5a869a44372226ed53/68747470733a2f2f7472617669732d63692e6f72672f4a616c6c6531392f6861706870726f78792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Jalle19/haphproxy)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c503eeecd639c49cd107bad66c975eaaaedea7ec489deca6090c84435c698ee2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4a616c6c6531392f6861706870726f78792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Jalle19/haphproxy/?branch=master)

haphproxy is a PHP library which can parse and create HAproxy configuration files.

The library operates on the configuration at a low level. A configuration is made up of sections (e.g. `global` and `default`). Each section is made up of parameters (e.g. `mode` and `timeout`). Each parameter has a value associated with it, e.g. a parameter named `timeout` can have a value of `timeout 30s`.

Since the library doesn't actually understand how HAproxy works, it is only guaranteed to generate syntactically valid configurations. For proper validation, please use `haproxy -f  -c`.

Requirements
------------

[](#requirements)

- PHP 5.6 or newer

Installation
------------

[](#installation)

Install via Composer:

```
composer require jalle19/haphproxy

```

Usage
-----

[](#usage)

### Reading a configuration

[](#reading-a-configuration)

This example reads an existing configuration file, parses it and dumps it back as a string:

```
// Create a parser
try {
	$parser = new Parser('/etc/haproxy/haproxy.cfg');
} catch (FileNotFoundException $e) {
	die($e->getMessage());
}

// Parse and dump the configuration
$configuration = $parser->parse();
$writer = new Writer($configuration);

echo $writer->dump();
```

### Writing a configuration

[](#writing-a-configuration)

This example shows how you can dynamically create a configuration:

```
$configuration = new Configuration();

// Add a global section
$globalSection = new Section\GlobalSection();
$globalSection->addParameter(new Parameter('daemon'))
              ->addParameter(new Parameter('maxconns', 128));
$configuration->addSection($globalSection);

// Add a defaults section
$defaultsSection = new Section\DefaultsSection();
$defaultsSection->addParameter(new Parameter('mode', 'http'));
$configuration->addSection($defaultsSection);

// Dump the configuration
$writer = new Writer($configuration);

echo $writer->dump();
```

The above results in the following configuration being generated:

```
# Generated with Jalle19\haphproxy
global
    daemon
    maxconns 128

defaults
    mode http

```

#### Changing the output formatting

[](#changing-the-output-formatting)

You can change the indentation and preface of the generated configuration. Changing the preface may be useful when used in combination with other tools to indicate whether the configuration file has been generated dynamically or not.

```
$configuration = new Configuration();
$writer = new Writer($configuration);

// Remember to include the comment character in the preface
$writer->setPreface('# AUTOGENERATED, DO NOT EDIT MANUALLY');
$writer->setIndent('  ');

echo $writer->dump();
```

### Inspecting a configuration

[](#inspecting-a-configuration)

You can access the individual parameters of each section like this:

```
// Make a section with some parameters
$section = new Section\DefaultsSection();
$section->addParameter(new Parameter('mode', 'http'));
$section->addParameter(new Parameter('timeout', 'client 30s'));
$section->addParameter(new Parameter('timeout', 'connect 30s'));
$section->addParameter(new Parameter('timeout', 'server 30s'));

// Get the value of a single parameter
$modeParameter = $section->getParameter('mode');
$mode = $modeParameter->getValue();

// Loop through all the "timeout" parameters
foreach ($section->getParametersByName('timeout') as $timeoutParameter) {

}
```

You can also loop through specific sections of a configuration:

```
$configuration = new Configuration();
$configuration->addSection(new FrontendSection('frontend frontend-1'));
$configuration->addSection(new FrontendSection('frontend frontend-2'));
$configuration->addSection(new FrontendSection('frontend frontend-3'));

foreach ($configuration->getFrontendSections() as $frontendSection) {

}
```

### Magic comments

[](#magic-comments)

A magic comment is a comment that beings with `HAPHPROXY_COMMENT`. In contrast to normal comments, these are not omitted during parsing and writing. Magic comments only apply to sections and can be useful to store arbitrary data.

```
$configuration = new Configuration();

$section = new Section\DefaultsSection();
$section->addMagicComment('magic');
$section->addParameter(new Parameter('mode', 'http'));

$writer = new Writer($configuration);

echo $writer->dump();
```

The above results in the following configuration being generated:

```
# Generated with Jalle19\haphproxy
defaults
    # HAPHPROXY_COMMENT magic
    mode http

```

Testing
-------

[](#testing)

The test suite leaves all the validation of the generated configurations to HAproxy itself, so in order to run the test suite you'll need to have `haproxy` in your path. To run the test suite, run `php vendor/bin/phpunit`.

There is a `Vagrantfile` shipped with the library which automatically provisions itself with all the required software.

License
-------

[](#license)

This library is licensed under the GNU General Public License 2 or newer

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

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 ~1 days

Total

5

Last Release

3574d ago

### Community

Maintainers

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

---

Tags

haproxy

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jalle19-haphproxy/health.svg)

```
[![Health](https://phpackages.com/badges/jalle19-haphproxy/health.svg)](https://phpackages.com/packages/jalle19-haphproxy)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

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