Skip to content

Commit 2b31796

Browse files
committed
Support automatic configuration import from yaml files | adding doc to README
1 parent 6046694 commit 2b31796

File tree

4 files changed

+175
-2
lines changed

4 files changed

+175
-2
lines changed

CHANGELOG_V1-x.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Changelog for V1.x
2+
3+
#### V1.1.0
4+
* Support automatic configuration import from yaml files

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,121 @@
11
# perl-module-configurationManager
22
The configuration manager perl module is used to dynamicaly load YAML configuration files with leazy load
3+
4+
## Define the configuration repository:
5+
6+
To define a repository, several ways exists:
7+
8+
#### With use statement :
9+
10+
The repositories defined by the use statement are shared between each configuration manager.
11+
12+
```perl
13+
use FindBin;
14+
use configurationManager ["repo=".$FindBin::Bin."/conf", "repo=/an/other/config"];
15+
```
16+
17+
#### With the 'new' subroutine
18+
19+
By instanciate a new configurationManager with repositories, you assign to this manager a self repository that is not shared with the others.
20+
21+
```perl
22+
use configurationManager;
23+
24+
my $altConfig = configurationManager->new({
25+
repositories => [$FindBin::Bin."/altConf"],
26+
});
27+
```
28+
29+
## Get configuration
30+
31+
The configurationManager offer access to the configuration behind the 'get' subroutine. The parameter to give as argument is the configuration filename without extension. If the file is into a recursive repository, give the relative path to it.
32+
33+
### Example:
34+
```
35+
/path/to/the/repository:
36+
-> myConfig.yaml
37+
-> mySecondConfig.yaml
38+
-> aDirectory:
39+
-> anotherConfig.yaml
40+
```
41+
##### content of /path/to/the/repository/myConfig.yaml :
42+
```yaml
43+
hello: "world"
44+
my: "name"
45+
```
46+
47+
##### content of /path/to/the/repository/aDirectory/anotherConfig.yaml :
48+
```yaml
49+
hello: "state"
50+
my: "purpose"
51+
```
52+
53+
##### code :
54+
```perl
55+
use configurationManager;
56+
use Data::Dumper;
57+
58+
my $manager = configurationManager->new({
59+
repositories => ["/path/to/the/repository"],
60+
});
61+
62+
print Dumper $manager->get("myConfig");
63+
# $VAR1 = {
64+
# 'hello' => 'world',
65+
# 'my' => 'name'
66+
# };
67+
68+
print Dumper $manager->get("aDirectory/anotherConfig");
69+
# $VAR2 = {
70+
# 'hello' => 'state',
71+
# 'my' => 'purpose'
72+
# };
73+
```
74+
75+
## Reserved keys :
76+
77+
name | purpose | content
78+
---- | ------- | -------
79+
import | Import another configuration | scalar OR array
80+
81+
### Import
82+
83+
The import key is used to automatically load another configuration into the current one. Note, the importation is done by first level key, and the imported configuration never override the current.
84+
85+
##### example:
86+
```
87+
/path/to/the/repository:
88+
-> myConfig.yaml
89+
-> mySecondConfig.yaml
90+
```
91+
##### content of /path/to/the/repository/myConfig.yaml :
92+
```yaml
93+
hello: "world"
94+
my: "name"
95+
import: "mySecondConfig"
96+
```
97+
##### content of /path/to/the/repository/mySecondConfig.yaml :
98+
```yaml
99+
is: "bergamote"
100+
and: 'I'
101+
eat: "strudle"
102+
```
103+
104+
##### code :
105+
```perl
106+
use configurationManager;
107+
use Data::Dumper;
108+
109+
my $manager = configurationManager->new({
110+
repositories => ["/path/to/the/repository"],
111+
});
112+
113+
print Dumper $manager->get("myConfig");
114+
# $VAR1 = {
115+
# 'hello' => 'world',
116+
# 'my' => 'name',
117+
# 'is' => 'bergamote',
118+
# 'and' => 'I',
119+
# 'eat' => 'strudle'
120+
# };
121+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ConfigurationImporter;
2+
use strict;
3+
use warnings;
4+
require Exporter;
5+
6+
sub import
7+
{
8+
no strict 'refs';
9+
no warnings 'once';
10+
11+
my ($args) = @_;
12+
return if(!exists $args->{"content"});
13+
return if(!defined $args->{"content"});
14+
15+
my $content = ${$args->{"content"}};
16+
my $loader = $args->{"loader"};
17+
18+
foreach my $contentKey (keys($content)) {
19+
next if ($contentKey ne "import");
20+
21+
my @importedFiles;;
22+
if (ref(\$content->{$contentKey}) eq "SCALAR") {
23+
push(@importedFiles, $content->{$contentKey});
24+
} elsif (ref($content->{$contentKey}) eq "ARRAY") {
25+
push(@importedFiles, $_) foreach (@{$content->{$contentKey}});
26+
}
27+
28+
delete $content->{$contentKey};
29+
foreach my $importedFile (@importedFiles) {
30+
my $importedContent = $loader->load($importedFile);
31+
32+
foreach my $importedContentKey (keys($importedContent)) {
33+
if (!exists $content->{$importedContentKey}) {
34+
$content->{$importedContentKey} = $importedContent->{$importedContentKey};
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
1;
42+
__END__

configurationManagerLib/Loader/ConfigurationLoader.pm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package ConfigurationLoader;
22
use strict;
33
use warnings;
44
use ConfigException;
5-
use YAML qw/LoadFile/;
5+
use ConfigurationImporter qw/import/;
6+
use YAML qw/LoadFile Load/;
67

78
sub new
89
{
@@ -38,7 +39,14 @@ sub load
3839
if ($config->{"loaded"}) {
3940
return $config->{"content"};
4041
} else {
41-
$config->{"content"} = LoadFile($config->{"location"});
42+
my $content = LoadFile($config->{"location"});
43+
44+
ConfigurationImporter::import({
45+
"content" => \$content,
46+
"loader" => $self}
47+
);
48+
49+
$config->{"content"} = $content;
4250
$config->{"loaded"} = 1;
4351

4452
return $config->{"content"};

0 commit comments

Comments
 (0)