jorisros/nginxparser

PHP 中解析 nginx 配置文件的类

1.1 2016-09-25 20:00 UTC

This package is auto-updated.

Last update: 2024-09-15 23:47:14 UTC


README

构建状态 NginxParser

在 PHP 中读取和创建 Nginx 配置文件的要求

  • PHP >= 7.4
  • Nginx 已安装(用于验证功能)

Composer

使用 Composer 将类添加到您的项目中

composer require jorisros/nginxparser

运行测试

在主目录中运行以下命令

./vendor/bin/phpunit tests

示例

使用类的示例

简单的配置文件

<?php

require __DIR__ . '/vendor/autoload.php';

use JorisRos\NginxParser\NginxParser;
use JorisRos\NginxParser\NginxElement;

$config = new NginxParser('server');

$location = new NginxParser('location','/');
$location->setRoot('/usr/share/nginx/html')
         ->setIndex(array('index.html', 'index.htm'));

$config ->setPort(80)
        ->setServerName(array('localhost','local','serveralias'))
        ->setAccessLog('/var/log/nginx/log/host.access.log')
        ->setLocation($location);

if($config->validate())
{
    $strFile = $config->build();
    file_put_contents('server.conf', $strFile);
}else{
    foreach ($config->getValidatorErrors() as $error) {
        # code...
    }
}

它将产生

server {
	port		80;
	server_name		localhost;
	server_alias		local serveralias;
	access_log		/var/log/nginx/log/host.access.log;

	location / {
		root		/usr/share/nginx/html;
		index		index.html index.htm;
	}

}

读取现有配置文件

<?php

require __DIR__ . '/vendor/autoload.php';

use JorisRos\NginxParser\NginxParser;
use JorisRos\NginxParser\NginxElement;

$d = new NginxParser();
$objects = $d->readFromFile('Resources/nginx-config/nginx.conf');

//var_dump($objects);

foreach($objects as $object)
{
    print($object->build());
}

Bitdeli Badge