Warning: count(): Parameter must be an array or an object that implements Countable in /home/luiscoms/www/blog/wp-includes/post-template.php on line 293

Warning: count(): Parameter must be an array or an object that implements Countable in /home/luiscoms/www/blog/wp-includes/post-template.php on line 293

Warning: count(): Parameter must be an array or an object that implements Countable in /home/luiscoms/www/blog/wp-includes/post-template.php on line 293

Warning: count(): Parameter must be an array or an object that implements Countable in /home/luiscoms/www/blog/wp-includes/post-template.php on line 293

Warning: count(): Parameter must be an array or an object that implements Countable in /home/luiscoms/www/blog/wp-includes/post-template.php on line 293
jan
24
2012

Utilizando arquivo de configuração ini

Arquivo de Configuração

Arquivo de Configuração

Muitas vezes em uma aplicação se faz necessário um arquivo de configuração.

Já trabalhei com os tradicionais arquivos .php com as variáveis ou constantes que seria usadas no restante do código.

Hoje trabalho mais com arquivos de configuração do tipo .ini, para isso utilizo uma classe que faz uso da função parse_ini_file do php.

Exemplo de arquivo .ini:

1
2
3
4
5
6
;Arquivo de configuração de teste
;Comentários começam com ';'
[database] ;Neste caso a seção é database
username="root" ;aqui o username é a chave na seção databese
pass=""
server="localhost"

Utilização da classe de configuração:

1
2
3
4
5
6
7
8
require_once 'config.php';
$config = new config('path/to/config.ini');
//pegar algum valor
//$config->getConfig('sessao', 'chave'))
echo $config->getConfig('database', 'username'); // root
//setar algum valor
//$config->setConfig('sessao', 'chave', 'valor')
$config->setConfig('database', 'username', 'luiscoms');

Código da classe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
 
class config {
    // Declaracao de Atributos
    private $file;
 
    private $configArray = array();
 
    // Construtor
    function Config ($configFile = '') {
	$this->file = $configFile;
	$this->parseConfig();
    }
 
   	// Realiza o parse da configuracao
	function parseConfig($processaSessoes = true) {
		$this->configArray = parse_ini_file($this->file, $processaSessoes);
 
		if (isset($this->configArray['import'])) {
			 foreach ($this->configArray['import'] as $file) {
				  $this->tempArray = parse_ini_file($file, true);
				  foreach($this->tempArray as $section => $keys){
					 $this->configArray[$section] = array_merge(isset($this->configArray[$section])?$this->configArray[$section]:array(),$keys);
				  }
			 }
		}
 
		// Executa comandos de configuração do PHP
		if (isset($this->keys['php'])){
		   foreach($this->keys['php'] as $key=> $value){
				ini_set($key, $value);
		   }
		}
 
	}
 
	// Seta uma configuracao
	function setConfig($sessao, $chave, $valor) {
		if (!isset($this->configArray[$sessao])) {
			$this->configArray[$sessao] = array();
		}
		$this->configArray[$sessao][$chave] = $valor;
	}
 
	// Busca um valor do arquivo de configuracao
	function getConfigSession($sessao){
		return isset($this->configArray[$sessao]) ? $this->configArray[$sessao] : array();
	}
 
	// Busca um valor do arquivo de configuracao
	function getConfig($sessao, $chave){
		return isset($this->configArray[$sessao][$chave]) ? $this->configArray[$sessao][$chave] : '';
	}
 
	// Realiza a checagem da configuracao
	function checkConfig($arrayChecagem){
		foreach ($arrayChecagem as $sessao => $chavesSessao){
			foreach ($chavesSessao as $chave){
				if (!$this->getConfig($sessao, $chave)){
					return false;
				}
			}
		}
		return true;
	}
}
 
?>

Gostou? Deixe um comentário.

Espero ter ajudado.

Written by Luis com S in: PHP | Tags:, ,

Deixe um Comentário

Loading Disqus Comments ...
Loading Facebook Comments ...

Nenhum comentário »

RSS feed for comments on this post. TrackBack URL


Leave a Reply


Time limit is exhausted. Please reload CAPTCHA.

Design: TheBuckmaker.com Web Templates