aleksey.nemiro/apacheconf.php

此包已被废弃且不再维护。未建议替代包。

这是一组用于操作Apache2网络服务器配置文件的类。

1.0 2016-02-21 14:40 UTC

This package is not auto-updated.

Last update: 2020-08-24 18:09:02 UTC


README

ApacheConf.PHP 最新稳定版本 总下载量 许可

这是一组用于操作Apache2网络服务器配置文件的类。

代码根据Apache License Version 2.0授权。

要求

  • PHP5 >= 5.5, PHP7;
  • Apache2 >= 2.4.

注意:未对早期版本进行测试,但可能一切正常。

如何使用?

包含Conf.php文件并导入类Nemiro\Apache\Conf

# include the class file (use own path of the file location)
require_once 'Apache/Conf.php';

# import class
use Nemiro\Apache\Conf as ApacheConf;

从文件加载配置

# create instance and load config from file
$conf = new ApacheConf('/etc/apache2/sites-available/example.org.conf');
# or
# $conf = ApacheConf::CreateFromFile('/etc/apache2/sites-available/example.org.conf');

# get values
var_dump($conf['VirtualHost']);
var_dump($conf['VirtualHost']->ParametersAsString());
var_dump($conf['VirtualHost']['DocumentRoot']->ParametersAsString());
var_dump($conf['VirtualHost']['ServerName']->ParametersAsString());
var_dump($conf['VirtualHost']['Alias']);

从字符串加载配置

# config data
$str = '<VirtualHost 127.0.0.1:80>
	DocumentRoot /home/example.org/www
  ServerName example.org

  <Location />
	  AuthType Basic
		AuthUserFile users.pwd
		Require valid-user
	</Location>
</VirtualHost>';

# parse string
$conf = ApacheConf::CreateFromString($str);

# get values
var_dump($conf['VirtualHost']);
var_dump($conf['VirtualHost']->ParametersAsString());
var_dump($conf['VirtualHost']['ServerName']->ParametersAsString());

# get location
$location = $conf['VirtualHost']['Location'][0];
var_dump($location);

保存到文件

# load from file
$conf = ApacheConf::CreateFromFile('/etc/apache2/sites-available/example.org.conf');

# set values
$conf['VirtualHost']['ServerName']->Parameters = array('example.org', 'www.example.org');
$conf['VirtualHost']['DocumentRoot']->Parameters = array('/home/example.org/www');

# create a new directive
$new_directory = ApacheConf::CreateDirective('Directory');
$new_directory->AddDirective('AllowOverride', 'All');
$new_directory->AddDirective('Allow', array('from', 'all'));
$new_directory->AddDirective('Require', array('all', 'granted'));

# add the new Directory section to the VirtualHost section
$new_directory->AddTo($conf['VirtualHost']);

# save
$conf->Save();

# or save as...
# $conf->Save('newFileName.conf');

从当前实例获取字符串

# load from file
$conf = new ApacheConf::CreateFromFile('/etc/apache2/sites-available/example.org.conf');

# set values
$conf['VirtualHost']['ServerName']->Parameters = array('example.org', 'www.example.org');
$conf['VirtualHost']['DocumentRoot']->Parameters = array('/home/example.org/www');

# create a new directive
$new_directory = ApacheConf::CreateDirective('Directory');
$new_directory->AddDirective('AllowOverride', 'All');
$new_directory->AddDirective('Allow', array('from', 'all'));
$new_directory->AddDirective('Require', array('all', 'granted'));

# add the new Directory section to the VirtualHost section
$new_directory->AddTo($conf['VirtualHost']);

# get as string
$string = $conf->GetAsString();

# show string
var_dump($string);

创建新的配置

# create an instance
$conf = new ApacheConf();

# create VirtualHost
$virtualHost = ApacheConf::CreateDirective('VirtualHost', '192.168.100.39:8080');
$virtualHost->AddDirective('DocumentRoot', '/home/example.org/www');
$virtualHost->AddDirective('ServerName', 'example.org');

# add to conf
$conf->Add($virtualHost);

# create directory
$directory = ApacheConf::CreateDirective('Directory');
$directory->AddDirective('AllowOverride', 'All');
$directory->AddDirective('Allow', array('from', 'all'));
$directory->AddDirective('Require', array('all', 'granted'));

# add the new Directory section to the VirtualHost section
$directory->AddTo($virtualHost);

# get as string
$string = $conf->GetAsString();

# show string
var_dump($string);

# or save
# $conf->Save('newFileName.conf');