blib/lib/CallBackery/Model/ConfigJsonSchema.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 168 CC 8 SCAR 35.5
CC
8
Cov
0%
postProcessCfg
72.0
validator
2.0
BEGIN
2.0
CRAP 34.9
1 package CallBackery::Model::ConfigJsonSchema;
2  
3 =head1 NAME
4  
5 CallBackery::Model::ConfigJsonSchema - get parse configuration file for CallBackery
6  
7 =head1 SYNOPSIS
8  
9 use CallBackery::Model::ConfigYAML;
10 my $cfg = CallBackery::Config->new(file=>$file);
11 my $hash_ref = $cfg->cfgHash();
12 my $pod = $cfg->pod();
13  
14 =head1 DESCRIPTION
15  
16 CallBackery gets much of its configuration from this config file.
17  
18 =cut
19  
20 use Mojo::Base 'CallBackery::Config', -signatures;
21 use JSON::Validator;
22 use Mojo::Exception;
23 use Mojo::Util qw(dumper);
24 use Mojo::Loader qw(data_section);
25 use YAML::XS qw(LoadFile Load);
26 use Mojo::JSON qw(true false);
27 use CallBackery::Translate qw(trm);
28  
29 $YAML::XS::Boolean = 'JSON::PP';
30  
31 =head2 cfgHash
32  
33 a hash containing the data from the config file
34  
35 =cut
36  
37 my $walker;
38 $walker = sub ($data, $cb, $path='' ) {
39 if (ref $data eq 'HASH') {
40 for my $key (keys %$data) {
41 $data->{$key} = $walker->($data->{$key},$cb,$path.'/'.$key);
42 }
43 }
44 elsif (ref $data eq 'ARRAY') {
45 my $i = 0;
46 for my $item (@$data) {
47 $item = $walker->($item, $cb, $path.'/'.$i);
48 $i++;
49 }
50 }
51 else {
52 return $cb->($path,$data);
53 }
54 return $data;
55 };
56  
57 =head2 cfgHash
58  
59 a hash containing the data from the config file. If the environment
60 variable CM_CB_OVERRIDE_... is set, the value from the config file is
61 overridden with the value from the environment. All config key characters
62 outside of [0-9a-zA-Z] are replaced with _. Note that you can only
63 override settings that exist in the config file, you can not create new settings.
64  
65 Example config file:
66  
67 BACKEND:
68 cfg-db: 'dbi:SQLite:dbname=/opt/running/cb.db'
69 LIST:
70 - hello
71 - world
72
73 Example environment override:
74
75 export CB_CFG_OVERRIDE_BACKEND_cfg_db='dbi:SQLite:dbname=/tmp/cb.db'
76 export CB_CFG_OVERRIDE_LIST_0='goodbye'
77  
78 =cut
79  
80 has cfgHash => sub ($self) {
81 my $cfgRaw = eval { LoadFile($self->file) };
82 if ($@) {
83 Mojo::Exception->throw("Loading ".$self->file.": $@");
84 }
85 my $cfg = $walker->($cfgRaw, sub ($path,$data) {
86 my $env = 'CB_CFG_OVERRIDE'.$path;
87 $env =~ s/[^0-9a-zA-Z]/_/g;
88 if (exists $ENV{$env} and my $override = $ENV{$env}) {
89 $self->log->debug("overriding cfg $path ($data) with ENV \$$env ($override)");
90 return $override;
91 }
92 return $data;
93 });
94 if (my @errors = $self->validator->validate($cfg)){
95 Mojo::Exception->throw("Validating ".$self->file.":\n".join "\n",@errors);
96 }
97 return $cfg;
98 };
99  
100 =head2 pod
101  
102 returns a pod documenting the config file
103  
104 =cut
105  
106 has pod => sub ($self) {
107 return "pod output for YAML is not supported";
108 };
109  
110  
111 has grammar => sub ($self) {
112 die "The grammar is now described in the 'schema' attribute. Please upgrade!";
113 };
114  
115 has schema => sub ($self) {
116 my $yaml = data_section __PACKAGE__, 'config-schema.yaml';
117 my $schema = eval {
118 Load($yaml);
119 };
120 if ($@) {
121 Mojo::Exception->throw("$@");
122 }
123 return $schema;
124 };
125  
126 sub validator ($self) {
127 my $validator = JSON::Validator->new();
128 $validator->schema($self->schema);
129 return $validator;
130 }
131  
132  
133 =head2 postProcessCfg
134  
135 Post process the configuration data into a format that is easily used
136 by the application.
137  
138 =cut
139  
140 sub postProcessCfg ($self) {
141 my $cfg = $self->cfgHash;
142 my $schema = $self->schema;
143 my @items;
144 my %pluginMap;
145 my @fullPluginList;
146 my @pluginList;
147 for my $prop (keys %{$schema->{properties}{BACKEND}{properties}}) {
148 my $default = $schema->{properties}{BACKEND}{properties}{$prop}{default};
149 $cfg->{BACKEND}{$prop} //= $default if $default;
150 }
151 for my $prop (keys %{$schema->{properties}{FRONTEND}{properties}}) {
152 my $default = $schema->{properties}{FRONTEND}{properties}{$prop}{default};
153 $cfg->{FRONTEND}{$prop} //= $default if $default;
154 }
155 for my $item (@{$cfg->{PLUGIN}}) {
156 my ($name) = keys %$item;
157 push @fullPluginList, $name;
158 push @pluginList, $name
159 if not $item->{$name}{unlisted};
160 Mojo::Exception->throw("Duplicated plugin instance $name")
161 if exists $pluginMap{$name};
162 my $obj = eval {
163 $self->loadAndNewPlugin($item->{$name}{module});
164 };
165 if ($@){
166 warn "Failed to load Plugin $name: $@";
167 next;
168 }
169 $obj->config($item->{$name});
170 $obj->name($name);
171 $obj->app($self->app);
172 $pluginMap{$name} = $obj;
173 push @items, {
174 type => 'object',
175 properties => {
176 $name => $obj->schema,
177 },
178 required => [ $name ],
179 additionalProperties => false,
180 };
181 }
182  
183 $schema->{properties}{PLUGIN}{items} = {
184 anyOf => \@items
185 };
186  
187 $schema->{properties}{FRONTEND}{properties}{$_.'_popup'}
188 {properties}{plugin}{enum} = \@fullPluginList
189 for qw(registration passwordreset);
190  
191 # warn YAML::XS::Dump $schema;
192 # second pass with data structures from plugins integrated
193 if (my @errors = $self->validator->validate($cfg)){
194 Mojo::Exception->throw(join "\n",@errors);
195 }
196  
197 $cfg->{PLUGIN} = {
198 prototype => \%pluginMap,
199 list => \@pluginList
200 };
201  
202 for (qw(registration passwordreset)) {
203 if (my $popup = $cfg->{FRONTEND}{$_.'_popup'}){
204 my $plugin = delete $popup->{plugin};
205 my $obj = $pluginMap{$plugin};
206 $cfg->{FRONTEND}{$_.'_popup'} = {
207 popupTitle => $obj->config->{'tab-name'},
208 name => $plugin,
209 %$popup,
210 };
211 }
212 }
213 #warn YAML::XS::Dump($cfg->{FRONTEND})
214 for my $name (@fullPluginList) {
215 $pluginMap{$name}->massageConfig($cfg);
216 }
217  
218 $cfg->{FRONTEND}{TRANSLATIONS} = $self->getTranslations();
219 return $cfg;
220 }
221  
222 1;
223  
224 __DATA__