Skip to content

Commit 1c6876e

Browse files
author
=
committed
First commit
1 parent b5a333f commit 1c6876e

File tree

6 files changed

+646
-20
lines changed

6 files changed

+646
-20
lines changed

README.md

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,209 @@
1-
# Config
1+
# InitPHP Config
2+
3+
Advanced configuration manager library.
4+
5+
[![Latest Stable Version](http://poser.pugx.org/initphp/config/v)](https://packagist.org/packages/initphp/config) [![Total Downloads](http://poser.pugx.org/initphp/config/downloads)](https://packagist.org/packages/initphp/config) [![Latest Unstable Version](http://poser.pugx.org/initphp/config/v/unstable)](https://packagist.org/packages/initphp/config) [![License](http://poser.pugx.org/initphp/config/license)](https://packagist.org/packages/initphp/config) [![PHP Version Require](http://poser.pugx.org/initphp/config/require/php)](https://packagist.org/packages/initphp/config)
6+
7+
## Requirements
8+
9+
- PHP 7.4 or higher
10+
- [ParameterBag Library](https://github.com/initphp/parameterbag)
11+
12+
## Installation
13+
14+
```php
15+
composer require initphp/config
16+
```
17+
18+
## Usage
19+
20+
### Config Classes
21+
22+
```php
23+
class MyAppConfig extends \InitPHP\Config\Classes
24+
{
25+
public $url = 'http://lvh.me';
26+
27+
public $name = 'LocalHost';
28+
29+
public $db = [
30+
'host' => 'localhost',
31+
'user' => 'root'
32+
];
33+
34+
// ...
35+
}
36+
```
37+
38+
```php
39+
$config = new MyAppConfig();
40+
41+
echo $config->get('url');
42+
// Output : "http://lvh.me"
43+
44+
echo $config->get('details', 'Not Found');
45+
// Output : "Not Found"
46+
47+
echo $config->get('db.host');
48+
// Output : "localhost"
49+
50+
if($config->has('name')){
51+
echo $config->get('name');
52+
// Output : "LocalHost"
53+
}
54+
```
55+
56+
### Config Library
57+
58+
#### `Config::setClass()`
59+
60+
Lets you define properties of an object or class as a configuration.
61+
62+
```php
63+
public function setClass(string|object $classOrObject): self;
64+
```
65+
66+
**_Example :_**
67+
68+
```php
69+
namespace App\Config;
70+
71+
class AppConfig
72+
{
73+
public $url = 'http://lvh.me';
74+
}
75+
76+
class Database
77+
{
78+
public $host = 'localhost';
79+
}
80+
```
81+
82+
```php
83+
use \InitPHP\Config\Config;
84+
85+
// Class
86+
Config::setClass(\App\Config\AppConfig::class);
87+
88+
// or Object
89+
Config::setClass(new \App\Config\Database());
90+
91+
Config::get('appconfig.url');
92+
93+
Config::get('database.host');
94+
```
95+
96+
#### `Config::setArray()`
97+
98+
Imports an array.
99+
100+
```php
101+
public function setArray(?string $name, array $assoc = []): self;
102+
```
103+
104+
**_Example :_**
105+
106+
```php
107+
require_once "vendor/autoload.php";
108+
use \InitPHP\Config\Config;
109+
110+
$configs = [
111+
'url' => 'http://lvh.me',
112+
'db' => [
113+
'host' => 'localhost',
114+
'user' => 'db_user',
115+
'pass' => '',
116+
'name' => 'database'
117+
],
118+
];
119+
Config::setArray('site', $configs);
120+
121+
122+
Config::get('site.url');
123+
Config::get('site.db.host', '127.0.0.1');
124+
Config::get('site.db.user', 'root');
125+
```
126+
127+
#### `Config::setFile()`
128+
129+
Loads the configurations in the PHP file, which returns an associative array.
130+
131+
```php
132+
public function setFile(?string $name, string $path): self;
133+
```
134+
135+
**_Example :_**
136+
137+
`public_html/db_config.php` :
138+
139+
```php
140+
<?php
141+
return [
142+
'HOST' => 'localhost',
143+
'USER' => 'root',
144+
'PASS' => '',
145+
'NAME' => 'database'
146+
];
147+
```
148+
149+
```php
150+
require_once "vendor/autoload.php";
151+
use \InitPHP\Config\Config;
152+
153+
Config::setFile('DB', __DIR__ . '/public_html/db_config.php');
154+
155+
// Usage :
156+
Config::get('db.host');
157+
```
158+
159+
#### `Config::setDir()`
160+
161+
Loads PHP files in a directory as configuration files.
162+
163+
```php
164+
public function setDir(?string $name, string $path, array $exclude = []): self;
165+
```
166+
167+
**_Example :_**
168+
169+
`public_html/config/db.php` :
170+
171+
```php
172+
<?php
173+
return [
174+
'HOST' => 'localhost',
175+
'USER' => 'root',
176+
'PASS' => '',
177+
'NAME' => 'database'
178+
];
179+
```
180+
181+
`public_html/config/site.php` :
182+
183+
```php
184+
<?php
185+
return [
186+
'URL' => 'http://lvh.me',
187+
// ...
188+
];
189+
```
190+
191+
192+
```php
193+
require_once "vendor/autoload.php";
194+
use \PHPConfig\Config;
195+
196+
Config::setDir('app', __DIR__ . '/public_html/config/');
197+
198+
// Usage :
199+
Config::get('app.site.url');
200+
Config::get('app.db.host');
201+
```
202+
203+
## Credit
204+
205+
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>>
206+
207+
## License
208+
209+
Copyright &copy; 2022 [MIT License](./LICENSE)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"minimum-stability": "stable",
2020
"require": {
21-
"php": ">=7.4"
21+
"php": ">=7.4",
22+
"initphp/parameterbag": "^1.0"
2223
}
2324
}

src/Classes.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
/**
33
* Classes.php
44
*
5-
* This file is part of Config.
5+
* This file is part of InitPHP.
66
*
77
* @author Muhammet ŞAFAK <[email protected]>
8-
* @copyright Copyright © 2022 Config
9-
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPL 3.0
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
1010
* @version 1.0
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
@@ -15,7 +15,71 @@
1515

1616
namespace InitPHP\Config;
1717

18-
class Classes
18+
use \InitPHP\Config\Interfaces\ConfigInterface;
19+
use \InitPHP\ParameterBag\ParameterBag;
20+
21+
abstract class Classes implements ConfigInterface
1922
{
2023

21-
}
24+
protected ParameterBag $_ParameterBag;
25+
26+
public function __construct()
27+
{
28+
$data = get_class_vars(get_called_class());
29+
$this->_ParameterBag = new ParameterBag($data, [
30+
'isMulti' => true,
31+
'separator' => '.'
32+
]);
33+
}
34+
35+
public function __destruct()
36+
{
37+
if(isset($this->_ParameterBag)){
38+
$this->_ParameterBag->close();
39+
unset($this->_ParameterBag);
40+
}
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function set(string $key, $value): self
47+
{
48+
$this->_ParameterBag->set($key, $value);
49+
return $this;
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function get(string $key, $default = null)
56+
{
57+
return $this->_ParameterBag->get($key, $default);
58+
}
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function has(string $key): bool
64+
{
65+
return $this->_ParameterBag->has($key);
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function remove(string $key): self
72+
{
73+
$this->_ParameterBag->remove($key);
74+
return $this;
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
public function all(): array
81+
{
82+
return $this->_ParameterBag->all();
83+
}
84+
85+
}

src/Config.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
/**
33
* Config.php
44
*
5-
* This file is part of Config.
5+
* This file is part of InitPHP.
66
*
77
* @author Muhammet ŞAFAK <[email protected]>
8-
* @copyright Copyright © 2022 Config
9-
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPL 3.0
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
1010
* @version 1.0
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
@@ -15,7 +15,65 @@
1515

1616
namespace InitPHP\Config;
1717

18+
/**
19+
*
20+
* Intermediate class to use the methods of the \PHPConfig\Library class as static.
21+
*
22+
* @see \InitPHP\Config\Library
23+
*
24+
* @method static string version()
25+
* @method static mixed get(string $key = null, mixed $default = null)
26+
* @method static Library set(string $key, mixed $value)
27+
* @method static bool has(string $key)
28+
* @method static array all()
29+
* @method static Library setArray(null|string $name = null, array $assoc = [])
30+
* @method static Library setDir(string|null $name, string $path, array $exclude = [])
31+
* @method static Library setFile(string|null $name, string $path)
32+
* @method static Library setClass(string|object $classOrObject)
33+
* @method static void close()
34+
*
35+
*/
1836
class Config
1937
{
2038

21-
}
39+
private static Library $Instance;
40+
41+
public function __construct()
42+
{
43+
self::getInstance();
44+
}
45+
46+
public function __destruct()
47+
{
48+
self::getInstance()->close();
49+
}
50+
51+
public function __debugInfo()
52+
{
53+
return self::getInstance()->__debugInfo();
54+
}
55+
56+
public function __get($name)
57+
{
58+
return self::getInstance()->__get($name);
59+
}
60+
61+
public function __call($name, $arguments)
62+
{
63+
return self::getInstance()->{$name}(...$arguments);
64+
}
65+
66+
public static function __callStatic($name, $arguments)
67+
{
68+
return self::getInstance()->{$name}(...$arguments);
69+
}
70+
71+
private static function getInstance(): Library
72+
{
73+
if(!isset(self::$Instance)){
74+
self::$Instance = new Library();
75+
}
76+
return self::$Instance;
77+
}
78+
79+
}

0 commit comments

Comments
 (0)