| 148 |
lars |
1 |
# league/config
|
|
|
2 |
|
|
|
3 |
[](https://packagist.org/packages/league/config)
|
|
|
4 |
[](https://packagist.org/packages/league/config)
|
|
|
5 |
[](LICENSE)
|
|
|
6 |
[](https://github.com/thephpleague/config/actions?query=workflow%3ATests+branch%3Amain)
|
|
|
7 |
[](https://scrutinizer-ci.com/g/thephpleague/config/code-structure)
|
|
|
8 |
[](https://scrutinizer-ci.com/g/thephpleague/config)
|
|
|
9 |
[](https://www.colinodell.com/sponsor)
|
|
|
10 |
|
|
|
11 |
**league/config** helps you define nested configuration arrays with strict schemas and access configuration values with dot notation. It was created by [Colin O'Dell][@colinodell].
|
|
|
12 |
|
|
|
13 |
## 📦 Installation
|
|
|
14 |
|
|
|
15 |
This project requires PHP 7.4 or higher. To install it via [Composer] simply run:
|
|
|
16 |
|
|
|
17 |
```bash
|
|
|
18 |
composer require league/config
|
|
|
19 |
```
|
|
|
20 |
|
|
|
21 |
## ðŸ§°ï¸ Basic Usage
|
|
|
22 |
|
|
|
23 |
The `Configuration` class provides everything you need to define the configuration structure and fetch values:
|
|
|
24 |
|
|
|
25 |
```php
|
|
|
26 |
use League\Config\Configuration;
|
|
|
27 |
use Nette\Schema\Expect;
|
|
|
28 |
|
|
|
29 |
// Define your configuration schema
|
|
|
30 |
$config = new Configuration([
|
|
|
31 |
'database' => Expect::structure([
|
|
|
32 |
'driver' => Expect::anyOf('mysql', 'postgresql', 'sqlite')->required(),
|
|
|
33 |
'host' => Expect::string()->default('localhost'),
|
|
|
34 |
'port' => Expect::int()->min(1)->max(65535),
|
|
|
35 |
'ssl' => Expect::bool(),
|
|
|
36 |
'database' => Expect::string()->required(),
|
|
|
37 |
'username' => Expect::string()->required(),
|
|
|
38 |
'password' => Expect::string()->nullable(),
|
|
|
39 |
]),
|
|
|
40 |
'logging' => Expect::structure([
|
|
|
41 |
'enabled' => Expect::bool()->default($_ENV['DEBUG'] == true),
|
|
|
42 |
'file' => Expect::string()->deprecated("use logging.path instead"),
|
|
|
43 |
'path' => Expect::string()->assert(function ($path) { return \is_writeable($path); })->required(),
|
|
|
44 |
]),
|
|
|
45 |
]);
|
|
|
46 |
|
|
|
47 |
// Set the values, either all at once with `merge()`:
|
|
|
48 |
$config->merge([
|
|
|
49 |
'database' => [
|
|
|
50 |
'driver' => 'mysql',
|
|
|
51 |
'port' => 3306,
|
|
|
52 |
'database' => 'mydb',
|
|
|
53 |
'username' => 'user',
|
|
|
54 |
'password' => 'secret',
|
|
|
55 |
],
|
|
|
56 |
]);
|
|
|
57 |
|
|
|
58 |
// Or one-at-a-time with `set()`:
|
|
|
59 |
$config->set('logging.path', '/var/log/myapp.log');
|
|
|
60 |
|
|
|
61 |
// You can now retrieve those values with `get()`.
|
|
|
62 |
// Validation and defaults will be applied for you automatically
|
|
|
63 |
$config->get('database'); // Fetches the entire "database" section as an array
|
|
|
64 |
$config->get('database.driver'); // Fetch a specific nested value with dot notation
|
|
|
65 |
$config->get('database/driver'); // Fetch a specific nested value with slash notation
|
|
|
66 |
$config->get('database.host'); // Returns the default value "localhost"
|
|
|
67 |
$config->get('logging.path'); // Guaranteed to be writeable thanks to the assertion in the schema
|
|
|
68 |
|
|
|
69 |
// If validation fails an `InvalidConfigurationException` will be thrown:
|
|
|
70 |
$config->set('database.driver', 'mongodb');
|
|
|
71 |
$config->get('database.driver'); // InvalidConfigurationException
|
|
|
72 |
|
|
|
73 |
// Attempting to fetch a non-existent key will result in an `InvalidConfigurationException`
|
|
|
74 |
$config->get('foo.bar');
|
|
|
75 |
|
|
|
76 |
// You could avoid this by checking whether that item exists:
|
|
|
77 |
$config->exists('foo.bar'); // Returns `false`
|
|
|
78 |
```
|
|
|
79 |
|
|
|
80 |
## 📓 Documentation
|
|
|
81 |
|
|
|
82 |
Full documentation can be found at [config.thephpleague.com][docs].
|
|
|
83 |
|
|
|
84 |
## 💭 Philosophy
|
|
|
85 |
|
|
|
86 |
This library aims to provide a **simple yet opinionated** approach to configuration with the following goals:
|
|
|
87 |
|
|
|
88 |
- The configuration should operate on **arrays with nested values** which are easily accessible
|
|
|
89 |
- The configuration structure should be **defined with strict schemas** defining the overall structure, allowed types, and allowed values
|
|
|
90 |
- Schemas should be defined using a **simple, fluent interface**
|
|
|
91 |
- You should be able to **add and combine schemas but never modify existing ones**
|
|
|
92 |
- Both the configuration values and the schema should be **defined and managed with PHP code**
|
|
|
93 |
- Schemas should be **immutable**; they should never change once they are set
|
|
|
94 |
- Configuration values should never define or influence the schemas
|
|
|
95 |
|
|
|
96 |
As a result, this library will likely **never** support features like:
|
|
|
97 |
|
|
|
98 |
- Loading and/or exporting configuration values or schemas using YAML, XML, or other files
|
|
|
99 |
- Parsing configuration values from a command line or other user interface
|
|
|
100 |
- Dynamically changing the schema, allowed values, or default values based on other configuration values
|
|
|
101 |
|
|
|
102 |
If you need that functionality you should check out other libraries like:
|
|
|
103 |
|
|
|
104 |
- [symfony/config]
|
|
|
105 |
- [symfony/options-resolver]
|
|
|
106 |
- [hassankhan/config]
|
|
|
107 |
- [consolidation/config]
|
|
|
108 |
- [laminas/laminas-config]
|
|
|
109 |
|
|
|
110 |
## 🏷️ Versioning
|
|
|
111 |
|
|
|
112 |
[SemVer](http://semver.org/) is followed closely. Minor and patch releases should not introduce breaking changes to the codebase.
|
|
|
113 |
|
|
|
114 |
Any classes or methods marked `@internal` are not intended for use outside this library and are subject to breaking changes at any time, so please avoid using them.
|
|
|
115 |
|
|
|
116 |
## 🛠️ Maintenance & Support
|
|
|
117 |
|
|
|
118 |
When a new **minor** version (e.g. `1.0` -> `1.1`) is released, the previous one (`1.0`) will continue to receive security and critical bug fixes for *at least* 3 months.
|
|
|
119 |
|
|
|
120 |
When a new **major** version is released (e.g. `1.1` -> `2.0`), the previous one (`1.1`) will receive critical bug fixes for *at least* 3 months and security updates for 6 months after that new release comes out.
|
|
|
121 |
|
|
|
122 |
(This policy may change in the future and exceptions may be made on a case-by-case basis.)
|
|
|
123 |
|
|
|
124 |
## 👷â€ï¸ Contributing
|
|
|
125 |
|
|
|
126 |
Contributions to this library are **welcome**! We only ask that you adhere to our [contributor guidelines] and avoid making changes that conflict with our Philosophy above.
|
|
|
127 |
|
|
|
128 |
## 🧪 Testing
|
|
|
129 |
|
|
|
130 |
```bash
|
|
|
131 |
composer test
|
|
|
132 |
```
|
|
|
133 |
|
|
|
134 |
## 📄 License
|
|
|
135 |
|
|
|
136 |
**league/config** is licensed under the BSD-3 license. See the [`LICENSE.md`][license] file for more details.
|
|
|
137 |
|
|
|
138 |
## ðŸ—ºï¸ Who Uses It?
|
|
|
139 |
|
|
|
140 |
This project is used by [league/commonmark][league-commonmark].
|
|
|
141 |
|
|
|
142 |
[docs]: https://config.thephpleague.com/
|
|
|
143 |
[@colinodell]: https://www.twitter.com/colinodell
|
|
|
144 |
[Composer]: https://getcomposer.org/
|
|
|
145 |
[PHP League]: https://thephpleague.com
|
|
|
146 |
[symfony/config]: https://symfony.com/doc/current/components/config.html
|
|
|
147 |
[symfony/options-resolver]: https://symfony.com/doc/current/components/options_resolver.html
|
|
|
148 |
[hassankhan/config]: https://github.com/hassankhan/config
|
|
|
149 |
[consolidation/config]: https://github.com/consolidation/config
|
|
|
150 |
[laminas/laminas-config]: https://docs.laminas.dev/laminas-config/
|
|
|
151 |
[contributor guidelines]: https://github.com/thephpleague/config/blob/main/.github/CONTRIBUTING.md
|
|
|
152 |
[license]: https://github.com/thephpleague/config/blob/main/LICENSE.md
|
|
|
153 |
[league-commonmark]: https://commonmark.thephpleague.com
|