@@ -25,6 +25,11 @@ The installation of this library can be done with Composer:
25
25
Config Manipulation
26
26
--------------------
27
27
28
+ The Config Library allows you to manipulate configurations to be used by
29
+ services, storing them in a single place.
30
+
31
+ To instantiate the Config class, we can do as follows:
32
+
28
33
.. code-block :: php
29
34
30
35
<?php
@@ -37,47 +42,66 @@ Config Manipulation
37
42
The structure of a service instance configuration
38
43
#################################################
39
44
45
+ All configurations are stored in arrays, in which there are keys with the name
46
+ of the service instances, such as ``default ``:
47
+
40
48
.. code-block :: php
41
49
42
50
[
43
51
'default' => [],
44
52
]
45
53
46
- Used for the `Language Library <https://docs.aplus-framework.com/guides/libraries/language/ >`_
47
- configuration file in the `App Project <https://docs.aplus-framework.com/guides/projects/app/ >`_.
54
+ And in these keys are inserted the configs of each service instance.
55
+
56
+ Let's look at a configuration file used to instantiate database services:
48
57
49
58
.. code-block :: php
50
59
51
- use Framework\Language\Language;
60
+ <?php
52
61
53
62
return [
54
63
'default' => [
55
- 'default' => 'en',
56
- 'supported' => [
57
- 'en',
58
- 'es',
59
- 'pt-br',
60
- ],
61
- 'fallback_level' => Language::FALLBACK_NONE,
62
- 'directories' => null,
63
- 'negotiate' => false,
64
+ 'host' => 'localhost',
65
+ 'username' => 'root',
66
+ 'password' => 'password',
64
67
],
65
68
];
66
69
70
+ Note that the file returns an array with the ``default `` key.
71
+
72
+ It is possible to define more configurations, adding new keys, which are the
73
+ name of the service instances.
74
+
75
+ Let's see how to define the configurations for the ``default `` and ``replica ``
76
+ instances:
77
+
67
78
.. code-block :: php
68
79
69
80
[
70
- 'default' => [],
71
- 'custom_instance' => [],
72
- 'other_custom_instance' => [],
81
+ 'default' => [
82
+ 'host' => 'localhost',
83
+ 'username' => 'root',
84
+ 'password' => 'password',
85
+ ],
86
+ 'replica' => [
87
+ 'host' => '192.168.0.100',
88
+ 'username' => 'root',
89
+ 'password' => 'foo',
90
+ ],
73
91
]
74
92
75
93
Set and Get
76
94
###########
77
95
96
+ In the Config instance we can set and get configurations with the ``set `` and
97
+ ``get `` methods.
98
+
78
99
Set Service Configs
79
100
^^^^^^^^^^^^^^^^^^^
80
101
102
+ Let's see how to set the **database ** service configs with host and username
103
+ information:
104
+
81
105
.. code-block :: php
82
106
83
107
$serviceName = 'database';
@@ -90,10 +114,15 @@ Set Service Configs
90
114
Get Service Configs
91
115
^^^^^^^^^^^^^^^^^^^
92
116
117
+ So, we can get the information through the ``get `` method. Let's see:
118
+
93
119
.. code-block :: php
94
120
121
+ $serviceName = 'database';
95
122
$configs = $config->get($serviceName);
96
123
124
+ And, in the ``$configs `` variable, the database information will be defined:
125
+
97
126
.. code-block :: php
98
127
99
128
[
@@ -104,30 +133,53 @@ Get Service Configs
104
133
Custom Service Instance Names
105
134
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106
135
136
+ The default instance is the ``default ``. However, you can manipulate information
137
+ from other instances.
138
+
139
+ To set a non-default instance, use the third parameter of the ``set `` method.
140
+
141
+ Let's see how to add information to the ``replica `` instance:
142
+
107
143
.. code-block :: php
108
144
109
- $serviceInstanceName = 'custom ';
145
+ $serviceInstanceName = 'replica ';
110
146
$configs = $config->set($serviceName, $serviceConfigs, $serviceInstanceName);
111
147
148
+ And to get information, we use the second parameter of the ``get `` method.
149
+
112
150
.. code-block :: php
113
151
114
- $serviceInstanceName = 'custom ';
152
+ $serviceInstanceName = 'replica ';
115
153
$configs = $config->get($serviceName, $serviceInstanceName);
116
154
117
155
Add
118
156
###
119
157
158
+ Above, we saw how to set configurations that overwrite existing instances.
159
+
160
+ But, it is possible to add only new configs, which will be merged.
161
+
162
+ For this, we use the ``add `` method:
163
+
120
164
.. code-block :: php
121
165
122
166
$config->add($serviceName, $serviceConfigs);
123
167
168
+ And, in the third parameter, you can define in which instance the configs will
169
+ be added:
170
+
124
171
.. code-block :: php
125
172
126
- $config->add($serviceName, $serviceConfigs, 'custom ');
173
+ $config->add($serviceName, $serviceConfigs, 'default ');
127
174
128
175
Set Many
129
176
########
130
177
178
+ It is possible to set several configurations at once through the ``setMany `` method.
179
+
180
+ Let's see how to set two instances of database configurations (default and
181
+ replica) and one instance for the cache service (default):
182
+
131
183
.. code-block :: php
132
184
133
185
$config->setMany([
@@ -152,20 +204,34 @@ Set Many
152
204
Get All
153
205
#######
154
206
207
+ To get all the configurations use the ``getAll `` method:
208
+
155
209
.. code-block :: php
156
210
157
211
$allConfigs = $config->getAll();
158
212
159
213
Configuration Files
160
214
-------------------
161
215
216
+ Above, we saw how to set configurations individually by instances and also
217
+ several at once.
218
+
219
+ In addition to being able to modify the configurations by methods, it is also
220
+ possible to define configurations in files that contain the name of the services
221
+ and return an array with the instances.
222
+
223
+ To do this, use Config passing the directory where the configuration files will
224
+ be in the first argument:
225
+
162
226
.. code-block :: php
163
227
164
228
$directoryPath = __DIR__ . '/configs';
165
229
$config = new Config($directoryPath);
166
230
167
- A basic config file must return an *array * that should have the ``default ``
168
- key set:
231
+ It is desirable that all configuration files have the ``default `` instance.
232
+
233
+ In the file below we have two instances, ``default `` and ``custom `` and the file
234
+ name must be the name of the service, for example, **database.php **:
169
235
170
236
.. code-block :: php
171
237
@@ -174,12 +240,19 @@ key set:
174
240
'custom' => [],
175
241
];
176
242
243
+ When there is a directory defined, the configuration files will be loaded
244
+ automatically and the service settings will be filled in.
245
+
246
+ In the example below, let's get the database service information with the
247
+ ``default `` instance and then with the ``custom `` instance:
248
+
177
249
.. code-block :: php
178
250
179
251
$databaseDefaultConfigs = $config->get('database');
180
252
$databaseCustomConfigs = $config->get('database', 'custom');
181
253
182
- `Config Manipulation `_
254
+ If you try to get configs from a service that hasn't been set up yet and the
255
+ service file doesn't exist, an exception will be thrown.
183
256
184
257
Persistence
185
258
-----------
@@ -203,9 +276,12 @@ and ``setMany`` methods:
203
276
Parsers
204
277
-------
205
278
206
- Config `Persistence `_ or with the `Set Many `_ method.
279
+ The library has several parses for different types of files. With which it is
280
+ possible to set `Persistence `_ or several settings at once using the
281
+ `Set Many `_ method.
207
282
208
- Example setting many:
283
+ Let's see an example parsing a file of type **env ** and setting various
284
+ configurations:
209
285
210
286
.. code-block :: php
211
287
@@ -218,7 +294,7 @@ Example setting many:
218
294
$config = new Config();
219
295
$config->setMany($configs);
220
296
221
- Example setting persistence :
297
+ The same can be done to set persistent configurations :
222
298
223
299
.. code-block :: php
224
300
@@ -242,7 +318,7 @@ The Config Library provides the following parsers:
242
318
INI Parser
243
319
##########
244
320
245
- INI syntax
321
+ Files of type ** INI ** can be parsed as shown below:
246
322
247
323
.. code-block :: php
248
324
@@ -251,6 +327,8 @@ INI syntax
251
327
$filename = __DIR__ . '/../config.ini';
252
328
$configs = IniParser::parse($filename);
253
329
330
+ The syntax of **INI ** files is as follows:
331
+
254
332
.. code-block :: ini
255
333
256
334
# Service 1
@@ -266,7 +344,7 @@ INI syntax
266
344
YAML Parser
267
345
###########
268
346
269
- YAML syntax
347
+ Files of type ** YAML ** can be parsed as follows:
270
348
271
349
.. code-block :: php
272
350
@@ -275,6 +353,8 @@ YAML syntax
275
353
$filename = __DIR__ . '/../config.yaml';
276
354
$configs = YamlParser::parse($filename);
277
355
356
+ And below is an example of the syntax of a **YAML ** file:
357
+
278
358
.. code-block :: yaml
279
359
280
360
# Service 1
@@ -293,9 +373,11 @@ YAML syntax
293
373
Database Parser
294
374
###############
295
375
296
- Database table
376
+ In addition to files, configurations of a **database ** table can also be
377
+ obtained using the `Database Library <https://docs.aplus-framework.com/guides/libraries/database/ >`_.
297
378
298
- `Database <https://docs.aplus-framework.com/guides/libraries/database/ >`_
379
+ Instead of passing the file path to the ``parse `` method, you pass the
380
+ database connection information:
299
381
300
382
.. code-block :: php
301
383
@@ -309,6 +391,8 @@ Database table
309
391
];
310
392
$configs = DatabaseParser::parse($databaseConfigs);
311
393
394
+ The configuration table in the database can be created as shown below:
395
+
312
396
.. code-block :: sql
313
397
314
398
USE `app`;
@@ -318,6 +402,14 @@ Database table
318
402
`value` varchar(255) NOT NULL
319
403
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
320
404
405
+ And the values of the services must have the service name as a prefix, followed
406
+ by a period and the name of the instance and after another period the name
407
+ of the configuration key.
408
+
409
+ Let's see how to enter example configurations:
410
+
411
+ .. code-block :: sql
412
+
321
413
INSERT INTO `Configs`
322
414
(`key`, `value`)
323
415
VALUES
@@ -326,6 +418,9 @@ Database table
326
418
('service2.default.0', 'True'),
327
419
('service2.custom.0', '"False"');
328
420
421
+ Below is an example file to create the Configs table and insert sample data
422
+ using the Database Library:
423
+
329
424
.. code-block :: php
330
425
331
426
use Framework\Database\Database;
@@ -345,7 +440,7 @@ Database table
345
440
})->run();
346
441
347
442
$database->insert($table)
348
- ->columns('key', 'value')
443
+ ->columns('key', 'value')
349
444
->values([
350
445
['service1.default.value1', 'foo'],
351
446
['service1.default.value2', 23],
@@ -356,7 +451,9 @@ Database table
356
451
JSON Parser
357
452
###########
358
453
359
- JSON syntax
454
+ Configurations can also be stored in **JSON ** files.
455
+
456
+ To get the configs, just use JsonParser:
360
457
361
458
.. code-block :: php
362
459
@@ -365,6 +462,8 @@ JSON syntax
365
462
$filename = __DIR__ . '/../config.json';
366
463
$configs = JsonParser::parse($filename);
367
464
465
+ Below is an example with the **JSON ** syntax:
466
+
368
467
.. code-block :: json
369
468
370
469
{
@@ -391,7 +490,7 @@ JSON syntax
391
490
XML Parser
392
491
##########
393
492
394
- XML syntax
493
+ Configurations can also be stored in ** XML **.
395
494
396
495
.. code-block :: php
397
496
@@ -400,6 +499,8 @@ XML syntax
400
499
$filename = __DIR__ . '/../config.xml';
401
500
$configs = XmlParser::parse($filename);
402
501
502
+ Example **XML ** file with configs:
503
+
403
504
.. code-block :: xml
404
505
405
506
<?xml version =" 1.0" encoding =" UTF-8" ?>
@@ -426,7 +527,7 @@ XML syntax
426
527
Env Parser
427
528
##########
428
529
429
- Dotenv syntax
530
+ Also, you can use files with the ** ENV ** syntax:
430
531
431
532
.. code-block :: php
432
533
0 commit comments