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↓77.4%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 2d 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 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

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

3621d 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

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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