Skip to content

Commit 41622b8

Browse files
committed
adapt to codeigniter CI2 and CI3 using constructor init event more that one variables
* rename class and put a path structure * update README with instructions * change __construct to fith CI libraries policy * the __construct can autodetects if were init with only the file * support of array configuration
1 parent a533172 commit 41622b8

File tree

2 files changed

+72
-126
lines changed

2 files changed

+72
-126
lines changed

README.md

Lines changed: 44 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,75 @@
1-
[![Build Status](https://travis-ci.org/hollax/ArrayConfigWriter.svg?branch=develop)](https://travis-ci.org/hollax/ArrayConfigWriter)
2-
[![Coverage Status](https://coveralls.io/repos/github/hollax/ArrayConfigWriter/badge.svg?branch=develop)](https://coveralls.io/github/hollax/ArrayConfigWriter?branch=master)
31
[![php: >=5.3.0](https://img.shields.io/badge/php->=5.3-8892BF.svg)](https://php.net/)
2+
[![Linux ready](https://img.shields.io/badge/linux-ready-success)](https://debian.org)
43

5-
# Array Config Writer
4+
# Codeigniter Array ConfigWriter
65

7-
This php library can be used to update array values in a php file.
8-
The library can be used by applications that use php array to store configuration values. It makes updating config array possible programatically.
6+
This php library can be used to update array config valuies dinamically of codeigniter.
7+
The library can be used by applications that use php array to store configuration values.
8+
It makes updating config array possible programatically.
99

10-
## Installation
10+
## Requirements
1111

12-
* Download the library and extract it to a folder in your application. The folder choice depends on your application structure.
12+
* Codeigniter 2.x or 3.x
13+
* PHP 5.3; GD module need for image loading and preview
1314

14-
## Usage
15-
* `include` the class library in your script
15+
## Installation
1616

17-
```php
18-
require_once 'class-array-config-writer.php';```
17+
The folder structure should match the CodeIgniter directory structure.
18+
Download the library and extract it to `libraries` in your application.
1919

20-
**The class supports autoload via composer**
20+
### Quick setup
2121

22-
* Create an instance of `Array_Config_Writer` class for the file that needs to be updated:
22+
1. Copy the `application/libraries/ConfigWriter.php` file to `application/libraries/`
23+
2. Initialise the ConfigWriter library and include it in your controller or whatever. Example below:
2324

24-
```php
25-
$config_writer = new Array_Config_Writer($config_file, $variable_name , $auto_save );
26-
```
25+
public function myform()
26+
{
27+
$this->load->library('ConfigWriter', 'config.php', 'configwriter');
28+
$this->configwriter->write('index_page' , 'index2.php' );
29+
}
2730

28-
Where :
31+
If you checked your `applications/config/config.php` will see how the line `$config['index_page'] = 'index.php';`
32+
changed to `$config['index_page'] = 'index2.php';` magically!
2933

30-
* **$config_file** (string) : The absolute path to the file where the array is declared.
31-
* **$variable_name** (string) : The variable name of the array to update.
32-
* **$auto_save** (boolean) : Whether the library should automatically save the changes.
3334

34-
We can start updating values:
35+
## Configuration options
3536

36-
```
37-
$config_writer->write('key' , value );
38-
```
37+
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);`
3939

40-
**Notes:**
41-
* You can set value to any php variable type.
42-
* The library treats numeric index "as is". Meaning '21' is different from 21
40+
* **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.
41+
* **variable_name** (string) : The variable name of the array to update. It will default to `$config`.as in file of codeignoter in `config.php`.
42+
* **auto_save** (boolean) : Whether the library should automatically save the changes. It will default to `TRUE`.
4343

44-
Supported variable Styles:
45-
46-
1. Single index
47-
```php
48-
$config[ 'key'] = 'value' ;
49-
```
50-
51-
2. Multi dimensional
52-
53-
```php
54-
$config['key1']['key2'] = 'value';
55-
```
44+
## USAGE
5645

5746
**note** You can not use the library to update the following format:
5847

5948
```php
6049
$config = array( 'key' => 'value' );
6150
```
62-
63-
**Notes:**
64-
* The library expect the variable to be indexed.
65-
* The file can have other variables aside our target variable.
66-
67-
## Examples
68-
69-
**PHP File** config.php
70-
71-
```php
72-
/*
73-
|--------------------------------------------------------------------------
74-
| Site Name
75-
|--------------------------------------------------------------------------
76-
|
77-
|
78-
|
79-
*/
80-
$config[ 'site_name'] = 'Example Site';
81-
82-
83-
/*
84-
|--------------------------------------------------------------------------
85-
| Enable caching
86-
|-------------------==-------------------------------------------------------
87-
|
88-
|
89-
*/
90-
$config[ 'enable_caching'] = true;
91-
92-
/*
93-
|--------------------------------------------------------------------------
94-
| Custom Array
95-
|-------------------==-------------------------------------------------------
96-
|
97-
|
98-
*/
99-
$config[ 'message'] = array(
100-
'title' => 'Welcome' ,
101-
'body' => 'Thanks for your interest in the library'
102-
);
103-
104-
105-
/*
106-
|--------------------------------------------------------------------------
107-
| Another Config Variable for the database
108-
|-------------------==-------------------------------------------------------
109-
|
110-
|
111-
*/
112-
$db[ 'database'] = '';
113-
$db[ 'username'] = '';
114-
```
115-
116-
Create an instance of the library:
51+
A complex code sample to change dinamically the database settings:
11752

11853
```php
119-
$config_writer = new Array_Config_Writer( APP_PATH.'config/config.php', 'config' );
54+
$init = array();
55+
$init['file'] = 'database.php';
56+
$init['variable_name'] = 'db';
57+
$this->load->library('ConfigWriter', $initialization, 'configwriter');
58+
$this->configwriter->write( array('default' , 'hostname') , 'my_hostname' );
59+
$this->configwriter->write( array('default' , 'username') , 'my_username' );
60+
$this->configwriter->write( array('default' , 'pconnet') , FALSE );
12061
```
12162

122-
Update a value by index. The *site_name* for instance:
123-
124-
```php
125-
$config_writer->write('site_name' , "New Site Name' );
126-
```
127-
The file *config.php* should be updated
63+
**Notes:**
64+
* You can set value to any php variable type.
65+
* The library treats numeric index "as is". Meaning '21' is different from 21
12866

12967
## Method chaining
13068

13169
```php
132-
$config_writer->write('site_name' , "New Site Name' )
133-
->write('enable_caching' , false );
70+
$config_writer->write('site_name' , "New Site Name' )->write('enable_caching' , false );
13471
```
13572

136-
13773
To update the `'message'` index which has array has value
13874

13975
* First get the current value
@@ -159,9 +95,9 @@ $message = $config['message'];
15995

16096
`$config_writer->write('message' , $message );`
16197

162-
## Testing
163-
You need phpunit to run the test cases
98+
## Credits
99+
100+
This is specific AD-HOC folk for Codeigniter-powered, see original project for.
164101

165-
`$ phpunit`
102+
- Copyright © Wakeel Ogunsanya - https://github.com/hollax/ArrayConfigWriter
166103

167-
[Read More](http://hollax.github.io/ArrayConfigWriter)
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22

33
/*
4-
Copyright 2016 Wakeel Ogunsanya
54
Licensed under GPLv2 or above
65
76
Version 1.2.2
87
*/
98

10-
class Array_Config_Writer {
9+
class ConfigWriter {
1110

1211
/**
1312
* Skip updating the index if the index acctually exist
@@ -106,12 +105,28 @@ class Array_Config_Writer {
106105
* @param string $config_file Asolute path to config file
107106
* @param string $variable_name the name of the config varible to update
108107
*/
109-
public function __construct($config_file , $variable_name = '\$config' , $auto_save = true )
108+
public function __construct($config_file = NULL)
110109
{
111-
$this->_file = $config_file ;
112-
$this->_destinationFile = $config_file ;
113-
$this->_autoSave = $auto_save ;
114-
$this->setVariableName($variable_name );
110+
$this->_file = APPPATH .'config/config.php';
111+
$this->_autoSave = true ;
112+
$this->setVariableName('config');
113+
if( is_array($config_file) )
114+
{
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'] );
121+
}
122+
else
123+
{
124+
if( isset($config_file) )
125+
$this->_file = APPPATH .'config/config.php';
126+
else
127+
$this->_file = APPPATH .'config/'.$config_file;
128+
}
129+
$this->_destinationFile = $this->_file ;
115130

116131
if ( ! file_exists($this->_file))
117132
{
@@ -120,12 +135,7 @@ public function __construct($config_file , $variable_name = '\$config' , $auto_s
120135

121136
return ;
122137
}
123-
if ( ! $variable_name)
124-
{
125-
$this->_lastError = 'You must set the set parameter of the library construct has varible to update' ;
126-
return ;
127-
}
128-
138+
129139
$this->_fileContent = file_get_contents( $this->_file ) ;
130140

131141
}
@@ -286,7 +296,7 @@ public function write( $index = null, $replacement = null , $write_method = sel
286296
* @param string $name
287297
* @deprecated since 1.3.0 use setDestinationFile() method
288298
*
289-
* @return Array_Config_Writer for method chaining
299+
* @return ConfigWriter for method chaining
290300
*/
291301
public function setFilename($name = null)
292302
{
@@ -300,7 +310,7 @@ public function setFilename($name = null)
300310
*
301311
* @param string $name
302312
*
303-
* @return Array_Config_Writer for method chaining
313+
* @return ConfigWriter for method chaining
304314
*/
305315
public function setDestinationFile($path)
306316
{
@@ -326,7 +336,7 @@ public function getDestinationFile()
326336
*
327337
* @param string $name
328338
* @since 1.3.0
329-
* @return Array_Config_Writer
339+
* @return ConfigWriter
330340
*/
331341
public function setVariableName($name = null)
332342
{
@@ -377,10 +387,10 @@ public function __destruct()
377387
/**
378388
* Save the new content to file
379389
*
380-
* You can call Array_Config_Writer::write() as many times as required
390+
* You can call ConfigWriter::write() as many times as required
381391
* before calling this method
382392
*
383-
* @return \Array_Config_Writer
393+
* @return \ConfigWriter
384394
*/
385395
public function save()
386396
{

0 commit comments

Comments
 (0)