jalle19 / haphproxy
此软件包已废弃,不再维护。未建议替代软件包。
PHP的HAProxy配置解析器
0.3.0
2016-08-04 10:49 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ^5.4
- symfony/process: ^3.1
README
haphproxy是一个PHP库,可以解析和创建HAProxy配置文件。
库在底层操作配置。配置由部分(例如 global
和 default
)组成。每个部分由参数(例如 mode
和 timeout
)组成。每个参数都有一个与之关联的值,例如名为 timeout
的参数可以具有 timeout 30s
的值。
由于库实际上并不理解HAProxy是如何工作的,因此只能保证生成语法上有效的配置。请使用 haproxy -f <configurationFile> -c
来进行适当的验证。
要求
- PHP 5.6 或更高版本
安装
通过Composer安装
composer require jalle19/haphproxy
使用
读取配置
此示例读取现有的配置文件,解析它并将它作为字符串回显
// 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();
写入配置
此示例展示了如何动态创建配置
$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();
上述操作将生成以下配置
# Generated with Jalle19\haphproxy
global
daemon
maxconns 128
defaults
mode http
更改输出格式
您可以通过更改缩进和前缀来更改生成的配置。当与其他工具一起使用时,更改前缀可能有助于指示配置文件是否是动态生成的。
$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();
检查配置
您可以通过以下方式访问每个部分的单独参数
// 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) { }
您还可以遍历配置的特定部分
$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) { }
魔法注释
魔法注释是以 HAPHPROXY_COMMENT
开头的注释。与普通注释不同,在解析和写入时不会被省略。魔法注释仅适用于部分,并且可以用于存储任意数据。
$configuration = new Configuration(); $section = new Section\DefaultsSection(); $section->addMagicComment('magic'); $section->addParameter(new Parameter('mode', 'http')); $writer = new Writer($configuration); echo $writer->dump();
上述操作将生成以下配置
# Generated with Jalle19\haphproxy
defaults
# HAPHPROXY_COMMENT magic
mode http
测试
测试套件将所有生成的配置的验证都留给HAproxy本身,因此要运行测试套件,您需要在您的路径中安装 haproxy
。要运行测试套件,请运行 php vendor/bin/phpunit
。
该库附带了一个 Vagrantfile
,它可以自动安装所有所需的软件。
许可证
此库根据GNU通用公共许可证2或更新版许可