Skip to content

Commit fa7a051

Browse files
committed
improve content about config
1 parent ba40e5e commit fa7a051

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

configuration/index.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ Fano Framework provides `IAppConfiguration` interface for that purpose.
1818

1919
- `getString()` which accepts name and returns string value
2020
- `getInt()` which accept name returns integer value
21+
- `getBool()` which accept name returns boolean value
2122

2223
## Built-in IAppConfiguration implementation
2324

24-
Fano Framework provides `TJsonFileConfig` class which loads configuration data from
25-
JSON file.
25+
Fano Framework provides `TJsonFileConfig` and `TIniFileConfig` class which loads configuration data from JSON and INI file respectively.
26+
27+
Load config from JSON,
2628

2729
```
2830
var config : IAppConfiguration;
@@ -32,8 +34,21 @@ config := TJsonFileConfig.create(
3234
);
3335
```
3436

35-
To be able to use `TJsonFileConfig` class with [dependency container](/dependency-container), Fano Framework provides `TJsonFileConfigFactory` class which enables you to register `TJsonFileConfig` class in container.
37+
Load config from INI,
3638

39+
```
40+
var config : IAppConfiguration;
41+
...
42+
config := TIniFileConfig.create(
43+
getCurrentDir() + '/config/config.ini',
44+
'fano'
45+
);
46+
```
47+
Last parameter is name of default section to use. Read *INI file configuration* section in this document for more information.
48+
49+
To be able to use `TJsonFileConfig` and `TIniFileConfig` class with [dependency container](/dependency-container), Fano Framework provides `TJsonFileConfigFactory` and `TIniFileConfigFactory` class which enables you to register above classes in container.
50+
51+
Register JSON config
3752
```
3853
container.add(
3954
'config',
@@ -43,6 +58,17 @@ container.add(
4358
);
4459
```
4560

61+
Register INI config
62+
```
63+
container.add(
64+
'config',
65+
TIniFileConfigFactory.create(
66+
getCurrentDir() + '/config/config.ini'
67+
)
68+
);
69+
```
70+
71+
4672
To get configuration instance
4773

4874
```
@@ -97,6 +123,38 @@ var cookieMaxAge : integer;
97123
cookieMaxAge := config.getInt('cookie.maxAge');
98124
```
99125

126+
## INI file configuration
127+
128+
`TIniFileConfig` is thin wrapper of Free Pascal `TIniFile`. `TIniFile` cannot read data from INI file that has no section. Your INI file must contain at least one section which serve as default section. The last parameter of `TIniFileConfig`'s constructor expect name of default section. If you use `TIniFileConfigFactory`, by default it uses `fano` as default section if not specified. You can specify default section by calling `setDefaultSection()` method as shown in following code.
129+
130+
```
131+
container.add(
132+
'config',
133+
TIniFileConfigFactory.create(
134+
getCurrentDir() + '/config/config.ini'
135+
).setDefaultSection('hello')
136+
);
137+
```
138+
139+
Consider reading `cookie.maxAge` configuration in code example above. It will read `maxAge` from `cookie` section.
140+
141+
```
142+
[cookie]
143+
maxAge=3600
144+
```
145+
146+
However, because nested section are not allowed in INI file, you can only read on section. For example,
147+
148+
```
149+
nestedData := config.getInt('fano.data.nested');
150+
```
151+
will read data from
152+
153+
```
154+
[fano]
155+
data.nested=test
156+
```
157+
100158
## Explore more
101159

102160
- [Dependency Container](/dependency-container)

0 commit comments

Comments
 (0)