-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConfig.php
More file actions
107 lines (92 loc) · 2.47 KB
/
Config.php
File metadata and controls
107 lines (92 loc) · 2.47 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
*
* @version 1.0
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @copyright Digiflex Development
* @package Config
* @link wwww.ppiframework.com
*/
class PPI_Config {
/**
* The config object doing the parsing
*
* @var null|PPI_Config_Ini
*/
protected $_oConfig = null;
/**
* The configuration options
*
* @var array
*/
protected $_options = array();
/**
* Initialise the config object
*
* Will check the file extension of your config filename and load up a specific parser
* @param array $options The options
*
*/
function __construct(array $options = array()) {
$this->_options = $options;
}
/**
* Get the current set config object
*
* @return object
*/
function getConfig() {
if($this->_oConfig === null) {
if(isset($this->_options['cacheConfig']) && $this->_options['cacheConfig']) {
$this->_oConfig = $this->cacheConfig();
} else {
$this->_oConfig = $this->parseConfig();
}
}
return $this->_oConfig;
}
/**
* Get a cached version of the framework, if no cached version exists it parses the config and caches it for you.
*
* @throws PPI_Exception
* @return void
*/
function cacheConfig() {
if(!isset($this->_options['configCachePath'])) {
throw new PPI_Exception('Missing path to the config cache path');
}
$path = sprintf('%s%s.%s.cache',
$this->_options['configCachePath'],
$this->_options['configFile'],
$this->_options['configBlock']);
if(file_exists($path)) {
return unserialize(file_get_contents($path));
}
$config = $this->parseConfig();
file_put_contents($path, serialize($config));
return $config;
}
/**
* Parse the config file
*
* @return object
*/
function parseConfig() {
// Make sure our config file exists
if(!file_exists(CONFIGPATH . $this->_options['configFile'])) {
die('Unable to find <b>'. CONFIGPATH . $this->_options['configFile'] .'</b> file, please check your application configuration');
}
// Switch the file extension
switch(PPI_Helper::getFileExtension($this->_options['configFile'])) {
case 'ini':
return new PPI_Config_Ini(parse_ini_file(CONFIGPATH . $this->_options['configFile'], true, INI_SCANNER_RAW), $this->_options['configBlock']);
case 'xml':
die('Trying to load a xml config file but no parser yet created.');
break;
case 'php':
die('Trying to load a php config file but no parser yet created.');
break;
}
}
}