OOP Config class
Om config aan te roepen
Code (php)
\lib\Config\Config.php
Code (php)
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
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
<?php
class Config_Config {
/**
* @acces protected.
* @param $config Array.
*/
protected $config = array();
/**
* @acces public.
*/
public function __destruct() {
foreach ($this->config AS $key => $value) {
$this->unsetConfig($key);
}
}
/**
* @acces public.
* @param $key Mixed.
* @param $value Mixed.
*/
public function setConfig($key = null, $value = null) {
if ($this->existConfig($key)) {
logger::warning(sprintf('Config Class error: config key \'%s\' allready exists.', $key));
}else{
$this->config[$key] = $value;
}
}
/**
* @acces public
* @param $key Mixed
*/
public function unsetConfig($key = null) {
if ($this->existConfig($key)) {
unset($this->config[$key]);
}
}
/**
* @acces public.
* @param $key Mixed.
* @return Mixed.
*/
public function getConfig($key = null) {
if ($this->existConfig($key)) {
$key = $this->config[$key];
}else{
logger::warning(sprintf('Config Class error: Connfig key \'%s\' doesn\'t exists.', $resource));
}
return $key;
}
/**
* @acces public.
*/
public function existConfig($key = null) {
return (isset($this->config[$key])) ? true : false;
}
}
?>
class Config_Config {
/**
* @acces protected.
* @param $config Array.
*/
protected $config = array();
/**
* @acces public.
*/
public function __destruct() {
foreach ($this->config AS $key => $value) {
$this->unsetConfig($key);
}
}
/**
* @acces public.
* @param $key Mixed.
* @param $value Mixed.
*/
public function setConfig($key = null, $value = null) {
if ($this->existConfig($key)) {
logger::warning(sprintf('Config Class error: config key \'%s\' allready exists.', $key));
}else{
$this->config[$key] = $value;
}
}
/**
* @acces public
* @param $key Mixed
*/
public function unsetConfig($key = null) {
if ($this->existConfig($key)) {
unset($this->config[$key]);
}
}
/**
* @acces public.
* @param $key Mixed.
* @return Mixed.
*/
public function getConfig($key = null) {
if ($this->existConfig($key)) {
$key = $this->config[$key];
}else{
logger::warning(sprintf('Config Class error: Connfig key \'%s\' doesn\'t exists.', $resource));
}
return $key;
}
/**
* @acces public.
*/
public function existConfig($key = null) {
return (isset($this->config[$key])) ? true : false;
}
}
?>
\lib\Config\ConfigMapper.php
Code (php)
\lib\Config\ConfigMapperINI.php
Code (php)
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
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
<?php
class Config_ConfigMapperINI implements Config_ConfigMapper {
/**
* @acces protected.
* @param String.
*/
protected $config;
/**
* @acces public.
* @param $config String.
* @return Object.
*/
public function __construct($config = null) {
if ($config == null || !is_string($config)) {
throw new coreException('Failed to initialize the ConfigMapper Class, setConfig parameter #1 isn\'t set correctly, a string is needed.');
}elseif (!file_exists($config)) {
throw new coreException(sprintf('Failed to initialize the ConfigMapper Class, setConfig config \'%s\' doesn\'t exist.', $config));
}elseif (strtolower(strrchr($config, '.')) != '.ini') {
throw new coreException('Failed to initialize the ConfigMapper Class, setConfig config \'%s\' is a wrong file type, a .ini is needed.');
}else{
$this->config = (string) $config;
}
}
/**
* @acces private.
* @return Object.
*/
public function handleConfig() {
$ini = parse_ini_file($this->config);
$return = new Config_Config();
foreach ($ini AS $key => $value) {
$return->setConfig($key, $value);
}
return (object) $return;
}
}
?>
class Config_ConfigMapperINI implements Config_ConfigMapper {
/**
* @acces protected.
* @param String.
*/
protected $config;
/**
* @acces public.
* @param $config String.
* @return Object.
*/
public function __construct($config = null) {
if ($config == null || !is_string($config)) {
throw new coreException('Failed to initialize the ConfigMapper Class, setConfig parameter #1 isn\'t set correctly, a string is needed.');
}elseif (!file_exists($config)) {
throw new coreException(sprintf('Failed to initialize the ConfigMapper Class, setConfig config \'%s\' doesn\'t exist.', $config));
}elseif (strtolower(strrchr($config, '.')) != '.ini') {
throw new coreException('Failed to initialize the ConfigMapper Class, setConfig config \'%s\' is a wrong file type, a .ini is needed.');
}else{
$this->config = (string) $config;
}
}
/**
* @acces private.
* @return Object.
*/
public function handleConfig() {
$ini = parse_ini_file($this->config);
$return = new Config_Config();
foreach ($ini AS $key => $value) {
$return->setConfig($key, $value);
}
return (object) $return;
}
}
?>
Gewijzigd op 18/07/2012 20:24:08 door Joakim Broden
En daarnaast is werken met een Service Container hierbij echt veel mooier.
Ik zal ff googlen naar Service Container