Skip to content

Commit bccf2d2

Browse files
committed
Update User Guide
1 parent cb51d22 commit bccf2d2

File tree

1 file changed

+133
-32
lines changed

1 file changed

+133
-32
lines changed

guide/index.rst

Lines changed: 133 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ The installation of this library can be done with Composer:
2525
Config Manipulation
2626
--------------------
2727

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+
2833
.. code-block:: php
2934
3035
<?php
@@ -37,47 +42,66 @@ Config Manipulation
3742
The structure of a service instance configuration
3843
#################################################
3944

45+
All configurations are stored in arrays, in which there are keys with the name
46+
of the service instances, such as ``default``:
47+
4048
.. code-block:: php
4149
4250
[
4351
'default' => [],
4452
]
4553
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:
4857

4958
.. code-block:: php
5059
51-
use Framework\Language\Language;
60+
<?php
5261
5362
return [
5463
'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',
6467
],
6568
];
6669
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+
6778
.. code-block:: php
6879
6980
[
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+
],
7391
]
7492
7593
Set and Get
7694
###########
7795

96+
In the Config instance we can set and get configurations with the ``set`` and
97+
``get`` methods.
98+
7899
Set Service Configs
79100
^^^^^^^^^^^^^^^^^^^
80101

102+
Let's see how to set the **database** service configs with host and username
103+
information:
104+
81105
.. code-block:: php
82106
83107
$serviceName = 'database';
@@ -90,10 +114,15 @@ Set Service Configs
90114
Get Service Configs
91115
^^^^^^^^^^^^^^^^^^^
92116

117+
So, we can get the information through the ``get`` method. Let's see:
118+
93119
.. code-block:: php
94120
121+
$serviceName = 'database';
95122
$configs = $config->get($serviceName);
96123
124+
And, in the ``$configs`` variable, the database information will be defined:
125+
97126
.. code-block:: php
98127
99128
[
@@ -104,30 +133,53 @@ Get Service Configs
104133
Custom Service Instance Names
105134
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106135

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+
107143
.. code-block:: php
108144
109-
$serviceInstanceName = 'custom';
145+
$serviceInstanceName = 'replica';
110146
$configs = $config->set($serviceName, $serviceConfigs, $serviceInstanceName);
111147
148+
And to get information, we use the second parameter of the ``get`` method.
149+
112150
.. code-block:: php
113151
114-
$serviceInstanceName = 'custom';
152+
$serviceInstanceName = 'replica';
115153
$configs = $config->get($serviceName, $serviceInstanceName);
116154
117155
Add
118156
###
119157

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+
120164
.. code-block:: php
121165
122166
$config->add($serviceName, $serviceConfigs);
123167
168+
And, in the third parameter, you can define in which instance the configs will
169+
be added:
170+
124171
.. code-block:: php
125172
126-
$config->add($serviceName, $serviceConfigs, 'custom');
173+
$config->add($serviceName, $serviceConfigs, 'default');
127174
128175
Set Many
129176
########
130177

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+
131183
.. code-block:: php
132184
133185
$config->setMany([
@@ -152,20 +204,34 @@ Set Many
152204
Get All
153205
#######
154206

207+
To get all the configurations use the ``getAll`` method:
208+
155209
.. code-block:: php
156210
157211
$allConfigs = $config->getAll();
158212
159213
Configuration Files
160214
-------------------
161215

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+
162226
.. code-block:: php
163227
164228
$directoryPath = __DIR__ . '/configs';
165229
$config = new Config($directoryPath);
166230
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**:
169235

170236
.. code-block:: php
171237
@@ -174,12 +240,19 @@ key set:
174240
'custom' => [],
175241
];
176242
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+
177249
.. code-block:: php
178250
179251
$databaseDefaultConfigs = $config->get('database');
180252
$databaseCustomConfigs = $config->get('database', 'custom');
181253
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.
183256

184257
Persistence
185258
-----------
@@ -203,9 +276,12 @@ and ``setMany`` methods:
203276
Parsers
204277
-------
205278

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.
207282

208-
Example setting many:
283+
Let's see an example parsing a file of type **env** and setting various
284+
configurations:
209285

210286
.. code-block:: php
211287
@@ -218,7 +294,7 @@ Example setting many:
218294
$config = new Config();
219295
$config->setMany($configs);
220296
221-
Example setting persistence:
297+
The same can be done to set persistent configurations:
222298

223299
.. code-block:: php
224300
@@ -242,7 +318,7 @@ The Config Library provides the following parsers:
242318
INI Parser
243319
##########
244320

245-
INI syntax
321+
Files of type **INI** can be parsed as shown below:
246322

247323
.. code-block:: php
248324
@@ -251,6 +327,8 @@ INI syntax
251327
$filename = __DIR__ . '/../config.ini';
252328
$configs = IniParser::parse($filename);
253329
330+
The syntax of **INI** files is as follows:
331+
254332
.. code-block:: ini
255333
256334
# Service 1
@@ -266,7 +344,7 @@ INI syntax
266344
YAML Parser
267345
###########
268346

269-
YAML syntax
347+
Files of type **YAML** can be parsed as follows:
270348

271349
.. code-block:: php
272350
@@ -275,6 +353,8 @@ YAML syntax
275353
$filename = __DIR__ . '/../config.yaml';
276354
$configs = YamlParser::parse($filename);
277355
356+
And below is an example of the syntax of a **YAML** file:
357+
278358
.. code-block:: yaml
279359
280360
# Service 1
@@ -293,9 +373,11 @@ YAML syntax
293373
Database Parser
294374
###############
295375

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/>`_.
297378

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:
299381

300382
.. code-block:: php
301383
@@ -309,6 +391,8 @@ Database table
309391
];
310392
$configs = DatabaseParser::parse($databaseConfigs);
311393
394+
The configuration table in the database can be created as shown below:
395+
312396
.. code-block:: sql
313397
314398
USE `app`;
@@ -318,6 +402,14 @@ Database table
318402
`value` varchar(255) NOT NULL
319403
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
320404
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+
321413
INSERT INTO `Configs`
322414
(`key`, `value`)
323415
VALUES
@@ -326,6 +418,9 @@ Database table
326418
('service2.default.0', 'True'),
327419
('service2.custom.0', '"False"');
328420
421+
Below is an example file to create the Configs table and insert sample data
422+
using the Database Library:
423+
329424
.. code-block:: php
330425
331426
use Framework\Database\Database;
@@ -345,7 +440,7 @@ Database table
345440
})->run();
346441
347442
$database->insert($table)
348-
->columns('key', 'value')
443+
->columns('key', 'value')
349444
->values([
350445
['service1.default.value1', 'foo'],
351446
['service1.default.value2', 23],
@@ -356,7 +451,9 @@ Database table
356451
JSON Parser
357452
###########
358453

359-
JSON syntax
454+
Configurations can also be stored in **JSON** files.
455+
456+
To get the configs, just use JsonParser:
360457

361458
.. code-block:: php
362459
@@ -365,6 +462,8 @@ JSON syntax
365462
$filename = __DIR__ . '/../config.json';
366463
$configs = JsonParser::parse($filename);
367464
465+
Below is an example with the **JSON** syntax:
466+
368467
.. code-block:: json
369468
370469
{
@@ -391,7 +490,7 @@ JSON syntax
391490
XML Parser
392491
##########
393492

394-
XML syntax
493+
Configurations can also be stored in **XML**.
395494

396495
.. code-block:: php
397496
@@ -400,6 +499,8 @@ XML syntax
400499
$filename = __DIR__ . '/../config.xml';
401500
$configs = XmlParser::parse($filename);
402501
502+
Example **XML** file with configs:
503+
403504
.. code-block:: xml
404505
405506
<?xml version="1.0" encoding="UTF-8" ?>
@@ -426,7 +527,7 @@ XML syntax
426527
Env Parser
427528
##########
428529

429-
Dotenv syntax
530+
Also, you can use files with the **ENV** syntax:
430531

431532
.. code-block:: php
432533

0 commit comments

Comments
 (0)