Skip to content

Commit b38ae72

Browse files
committed
as in any lib from CI framework, provide initialize(array())
* update README documentation usage * provide initialize( $config = array() ) as any CI library * use initialize for constructor defaults
1 parent 41622b8 commit b38ae72

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,18 @@ Download the library and extract it to `libraries` in your application.
2121

2222
1. Copy the `application/libraries/ConfigWriter.php` file to `application/libraries/`
2323
2. Initialise the ConfigWriter library and include it in your controller or whatever. Example below:
24-
25-
public function myform()
26-
{
24+
```
2725
$this->load->library('ConfigWriter', 'config.php', 'configwriter');
2826
$this->configwriter->write('index_page' , 'index2.php' );
29-
}
30-
27+
```
3128
If you checked your `applications/config/config.php` will see how the line `$config['index_page'] = 'index.php';`
3229
changed to `$config['index_page'] = 'index2.php';` magically!
3330

3431

3532
## Configuration options
3633

3734
There are some configuration options which you can pass to the library in an associative array when you
38-
performed in code `$this->configwriter->init($config)` or by constructor like `$this->load->library('ConfigWriter', $config);`
35+
performed in code `$this->configwriter->initialize($config)` or by constructor like `$this->load->library('ConfigWriter', $config);`
3936

4037
* **file**: (string) This should be set to the file that you want to alter config array. It will default to `config.php`, each name will assume `applications/config/` as search path.
4138
* **variable_name** (string) : The variable name of the array to update. It will default to `$config`.as in file of codeignoter in `config.php`.

application/libraries/ConfigWriter.php

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,46 +100,84 @@ class ConfigWriter {
100100
protected $_autoSave = true ;
101101

102102

103+
// --------------------------------------------------------------------
104+
103105
/**
104-
*
105-
* @param string $config_file Asolute path to config file
106-
* @param string $variable_name the name of the config varible to update
106+
* CI Singleton
107+
*
108+
* @var object
109+
*/
110+
protected $CI;
111+
112+
// --------------------------------------------------------------------
113+
114+
/**
115+
* Class constructor
116+
*
117+
* Loads the calendar language file and sets the default time reference.
118+
*
119+
* @uses CI_Lang::$is_loaded
120+
*
121+
* @param array $config Calendar options
122+
* @return void
123+
*/
124+
public function __construct($config = NULL)
125+
{
126+
127+
$this->CI =& get_instance();
128+
129+
empty($config) OR $this->initialize($config);
130+
131+
log_message('info', 'ConfigWriter Class Initialized');
132+
133+
}
134+
135+
// --------------------------------------------------------------------
136+
137+
/**
138+
* Initialize the user preferences
139+
*
140+
* Accepts an associative array as input, containing display preferences
141+
*
142+
* @param array config preferences
143+
* @return CI_Calendar
107144
*/
108-
public function __construct($config_file = NULL)
145+
public function initialize($config = array())
109146
{
110147
$this->_file = APPPATH .'config/config.php';
111148
$this->_autoSave = true ;
112149
$this->setVariableName('config');
113-
if( is_array($config_file) )
150+
151+
if( is_array($config) )
114152
{
115-
if (array_key_exists('file', $config_file))
116-
$this->_file = APPPATH .'config/'.$config_file['file'];
117-
if (array_key_exists('auto_save', $config_file))
118-
$this->_autoSave = $config_file['auto_save'];
119-
if (array_key_exists('variable_name', $config_file))
120-
$this->setVariableName($config_file['variable_name'] );
153+
if (array_key_exists('file', $config))
154+
$this->_file = APPPATH .'config/'.$config['file'];
155+
if (array_key_exists('auto_save', $config))
156+
$this->_autoSave = $config['auto_save'];
157+
if (array_key_exists('variable_name', $config))
158+
$this->setVariableName($config['variable_name'] );
121159
}
122160
else
123161
{
124-
if( isset($config_file) )
162+
if( isset($config) )
125163
$this->_file = APPPATH .'config/config.php';
126164
else
127-
$this->_file = APPPATH .'config/'.$config_file;
165+
$this->_file = APPPATH .'config/'.$config;
128166
}
129167
$this->_destinationFile = $this->_file ;
130168

131169
if ( ! file_exists($this->_file))
132170
{
133171
//throw new Exception('Config Write Error: Config file doesnt exists ' . $this->_file);
134172
$this->_lastError = 'Config Write Error: Config file doesnt exists ' . $this->_file ;
135-
136-
return ;
173+
log_message('warning', 'ConfigWriter error: config file not exist, using default' . $this->_file);
174+
$this->_file = APPPATH .'config/config.php';
175+
return $this;
137176
}
138-
139177
$this->_fileContent = file_get_contents( $this->_file ) ;
140-
178+
return $this;
141179
}
142-
180+
143181

144182
/**
145183
* Wite or update an item of the config array

0 commit comments

Comments
 (0)