lib/Dancer2/ConfigReader.pm

filter: Statementstmt 91.5% 130 / 142 Branchbran 74.1% 43 / 58 Conditioncond 64.7% 11 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 86.9% 20 / 23 Podpod n/a totaltotal 83.6% 209 / 250 CC 41 SCAR 22.0
CC
41
Cov
85%
__ANON__
30.0
_warn_unknown_engine_config_keys
15.4
_strict_config_keys
9.0
CRAP 9.0
« blib/lib/Dancer2/Test.pm ↑ Summary lib/Dancer2/Core/Route.pm »
1 # ABSTRACT: Config reader for Dancer2 App
2 package Dancer2::ConfigReader;
3 189 $Dancer2::ConfigReader::VERSION = '2.3.0';
4 189 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 189 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 189 ▶use Hash::Merge::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 189 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 189 ▶use Module::Runtime qw{ use_module };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 189 ▶use Ref::Util qw/ is_arrayref is_hashref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 189 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 189 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 189 ▶use Dancer2::Core::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 189 ▶use Dancer2::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 189 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 189 ▶use Dancer2::ConfigUtils 'normalize_config_entry';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 189 ▶our $MAX_CONFIGS = $ENV{DANCER_MAX_CONFIGS} || 100;
Condition50%1/2
$ENV{'DANCER_MAX_CONFIGS'} || 100
l!l
0189
MC/DC0%0/1
$ENV{'DANCER_MAX_CONFIGS'} || 100
$ENV{'DANCER_MAX_CONFIGS'}
Truth table50%1/2
A
A= $ENV{'DANCER_MAX_CONFIGS'}
Aresult
11
00
20  
21 189 my %KNOWN_CORE_KEYS = map +( $_ => 1 ), qw(
22 additional_config_readers
23 appdir
24 apphandler
25 appname
26 auto_page
27 behind_proxy
28 charset
29 content_type
30 default_mime_type
31 engines
32 environment
33 error_template
34 host
35 layout
36 layout_dir
37 log
38 logger
39 no_default_middleware
40 no_server_tokens
41 plugins
42 port
43 public_dir
44 route_handlers
45 serializer
46 session
47 show_errors
48 show_stacktrace
49 startup_info
50 static_handler
51 template
52 timeout
53 traces
54 type_library
55 views
56 strict_config
57 strict_config_allow
58 );
59  
60 189 my %KNOWN_ENGINE_CONFIG = (
61 'logger' => {
62 'base_keys' => {
63 'app_name' => 1,
64 'auto_encoding_charset' => 1,
65 'log_format' => 1,
66 'log_level' => 1,
67 },
68 'engines' => {
69 'capture' => { keys => {} },
70 'console' => { keys => {} },
71 'diag' => { keys => {} },
72 'file' => {
73 'keys' => {
74 'file_name' => 1,
75 'log_dir' => 1,
76 },
77 },
78 'note' => { keys => {} },
79 'null' => { keys => {} },
80 },
81 },
82 'serializer' => {
83 'engines' => {
84 'dumper' => { keys => {} },
85 'json' => { allow_any => 1 },
86 'mutable' => {
87 keys => {
88 mapping => 1,
89 },
90 },
91 'yaml' => { keys => {} },
92 },
93 },
94 'session' => {
95 'base_keys' => {
96 'cookie_domain' => 1,
97 'cookie_duration' => 1,
98 'cookie_name' => 1,
99 'cookie_path' => 1,
100 'cookie_same_site' => 1,
101 'is_http_only' => 1,
102 'is_secure' => 1,
103 'session_duration' => 1,
104 },
105 'engines' => {
106 'simple' => { keys => {} },
107 'yaml' => {
108 'keys' => {
109 'session_dir' => 1,
110 },
111 },
112 },
113 },
114 'template' => {
115 'engines' => {
116 'templatetoolkit' => { allow_any => 1 },
117 'tiny' => { allow_any => 1 },
118 },
119 },
120 );
121  
122 189 has location => (
123 is => 'ro',
124 isa => Str,
125 required => 1,
126 );
127  
128 189 has default_config => (
129 is => 'ro',
130 isa => HashRef,
131 required => 1,
132 );
133  
134 has config_location => (
135 is => 'ro',
136 isa => ReadableFilePath,
137 lazy => 1,
138 212 ▶ default => sub { $_[0]->location },
Subroutine __ANON__: called CC=1 SCAR=0.0
139 189 );
140  
141 # The type for this attribute is Str because we don't require
142 # an existing directory with configuration files for the
143 # environments. An application without environments is still
144 # valid and works.
145 has environments_location => (
146 is => 'ro',
147 isa => Str,
148 lazy => 1,
149 0 ▶ default => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
150 # short circuit
151 defined $ENV{'DANCER_ENVDIR'}
152 0 ▶ and return $ENV{'DANCER_ENVDIR'};
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{"DANCER_ENVDIR"} and return $ENV{"DANCER_ENVDIR"} 0 0 0
153  
154 0 my $self = shift;
155  
156 0 foreach my $maybe_path ( $self->config_location, $self->location ) {
157 0 my $path = Path::Tiny::path($maybe_path, 'environments');
158 0 ▶ $path->exists && $path->is_dir
Branch: 1 branch
BranchTrueFalseTotal
$path->exists and $path->is_dir and return $path->stringify 0 0 0
Condition0%0/3
$path->exists and $path->is_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$path->exists and $path->is_dir
$path->exists $path->is_dir
Truth table0%0/3
A and B
A= $path->existsB= $path->is_dir
ABresult
0X0
100
111
159 and return $path->stringify;
160 }
161  
162 0 return '';
163 },
164 189 );
165  
166 189 has _config_location_path => (
167 is => 'ro',
168 lazy => 1,
169 builder => '_build_config_location_path',
170 init_arg => undef,
171 );
172  
173 189 has _environments_location_path => (
174 is => 'ro',
175 lazy => 1,
176 builder => '_build_environments_location_path',
177 init_arg => undef,
178 );
179  
180 0 ▶sub _build_config_location_path {
Subroutine _build_config_location_path: not called CC=1 SCAR=6.9
181 0 my $self = shift;
182 0 return Path::Tiny::path( $self->config_location );
183 }
184  
185 0 ▶sub _build_environments_location_path {
Subroutine _build_environments_location_path: not called CC=1 SCAR=6.9
186 0 my $self = shift;
187 0 return Path::Tiny::path( $self->environments_location );
188 }
189  
190 189 has config => (
191 is => 'ro',
192 isa => HashRef,
193 lazy => 1,
194 builder => '_build_config',
195 );
196  
197 189 has environment => (
198 is => 'ro',
199 isa => Str,
200 required => 1,
201 );
202  
203 189 has config_readers => (
204 is => 'ro',
205 lazy => 1,
206 isa => ArrayRef,
207 builder => '_build_config_readers',
208 );
209  
210 # The config builder
211 324 ▶sub _build_config {
Subroutine _build_config: called CC=7 SCAR=19.5
212 324 my ($self) = @_;
213  
214 324 my $config = $self->default_config;
215  
216 324 my $nbr_config = 0;
217  
218 324 my @readers = @{ $self->config_readers };
219  
220 210 ▶ my $config_to_object = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
221 210 my $thing = $_;
222  
223 210 ▶ return $thing if blessed $thing;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($thing) 0 210 210
224  
225 210 ▶ $thing = { $thing => {} } unless ref $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thing 209 1 210
226  
227 210 ▶ die "additional_config_readers entry must have exactly one key\n"
Branch: 1 branch
BranchTrueFalseTotal
if keys %$thing != 1 1 209 210
228 if keys %$thing != 1;
229  
230 209 my( $class, $args ) = %$thing;
231  
232 209 return use_module($class)->new(
233 location => $self->location,
234 environment => $self->environment,
235 %$args,
236 );
237 322 };
238  
239 322 ▶ while( my $r = shift @readers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = shift @readers) 533 316 849
240 533 ▶ die <<"END" if $nbr_config++ >= $MAX_CONFIGS;
Branch: 1 branch
BranchTrueFalseTotal
if $nbr_config++ >= $Dancer2::ConfigReader::MAX_CONFIGS 3 530 533
241 MAX_CONFIGS exceeded: read over $MAX_CONFIGS configurations
242  
243 Looks like you have an infinite recursion in your configuration system.
244 Re-run with DANCER_CONFIG_VERBOSE=1 to see what is going on.
245  
246 If your application really read that many configs (may \$dog have mercy
247 on your soul), you can increase the limit via the environment variable
248 DANCER_MAX_CONFIGS.
249  
250 END
251 0 ▶ warn "Reading config from @{[ $r->name() ]}\n" if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 530 530
252 530 my $local_config = $r->read_config;
253  
254 528 ▶ if( my $additionals = delete $local_config->{additional_config_readers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $additionals = delete $local_config->{"additional_config_readers"}) 209 319 528
255  
256 209 ▶ warn "Additional config readers found\n" if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 209 209
257  
258 209 ▶ unshift @readers, map { $config_to_object->($_) } is_arrayref($additionals) ? @$additionals : ($additionals);
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($additionals) ? : 1 208 209
259 }
260  
261 527 $config = Hash::Merge::Simple->merge(
262 $config, $local_config
263 );
264 }
265  
266 316 return $self->_normalize_config($config);
267 }
268  
269 317 ▶sub _normalize_config {
Subroutine _normalize_config: called CC=2 SCAR=6.9
270 317 my ( $self, $config ) = @_;
271  
272 317 foreach my $key ( keys %{$config} ) {
273 3845 my $value = $config->{$key};
274 3845 $config->{$key} = normalize_config_entry( $key, $value );
275 }
276  
277 316 $self->_strict_config_keys($config);
278  
279 316 return $config;
280 }
281  
282 321 ▶sub _build_config_readers {
Subroutine _build_config_readers: called CC=3 SCAR=11.0
283 321 my ($self) = @_;
284  
285 my @config_reader_names = $ENV{'DANCER_CONFIG_READERS'}
286 321 ▶ ? (split qr{,}msx, $ENV{'DANCER_CONFIG_READERS'})
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DANCER_CONFIG_READERS'} ? : 11 310 321
287 : ( q{Dancer2::ConfigReader::Config::Any} );
288  
289 321 ▶ warn "ConfigReaders to use: @config_reader_names\n" if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 321 321
290 return [
291 321 map use_module($_)->new(
292 location => $self->location,
293 environment => $self->environment,
294 ), @config_reader_names
295 ];
296 }
297  
298 316 ▶sub _strict_config_keys {
Subroutine _strict_config_keys: called CC=9 SCAR=22.0
299 316 my ( $self, $config ) = @_;
300  
301 return
302 if exists $config->{'strict_config'}
303 316 ▶ && !$config->{'strict_config'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"strict_config"} and not $config->{"strict_config"} 305 11 316
Condition100%3/3
exists $config->{"strict_config"} and not $config->{"strict_config"}
!ll&&!rl&&r
74305
MC/DC100%2/2
exists $config->{"strict_config"} and not $config->{"strict_config"}
exists $config->{"strict_config"} not $config->{"strict_config"}
Truth table100%3/3
A and not(B)
A= exists $config->{"strict_config"}B= not $config->{"strict_config"}
ABresult
0X0
100
111
304  
305 11 my %allowed_keys;
306 11 ▶ if ( exists $config->{'strict_config_allow'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"strict_config_allow"}) 3 8 11
307 3 my $allow = $config->{'strict_config_allow'};
308 3 ▶ if ( is_arrayref($allow) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($allow)) { } 3 0 3
309 3 %allowed_keys = map +( $_ => 1 ), @{$allow};
310 } else {
311 0 croak('strict_config_allow can only be arrayref');
312 }
313 }
314  
315 my @warnings = map +(
316 $KNOWN_CORE_KEYS{$_} || $allowed_keys{$_}
317 ? ()
318 : "Unknown configuration key '$_'"
319 11 ▶ ), sort keys %{$config};
Branch: 1 branch
BranchTrueFalseTotal
$KNOWN_CORE_KEYS{$_} || $allowed_keys{$_} ? : 54 10 64
Condition100%3/3
$KNOWN_CORE_KEYS{$_} or $allowed_keys{$_}
l!l&&r!l&&!r
51310
MC/DC100%2/2
$KNOWN_CORE_KEYS{$_} or $allowed_keys{$_}
$KNOWN_CORE_KEYS{$_} $allowed_keys{$_}
Truth table100%3/3
A or B
A= $KNOWN_CORE_KEYS{$_}B= $allowed_keys{$_}
ABresult
1X1
011
000
320  
321 11 ▶ if ( my $engines = $config->{'engines'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $engines = $config->{"engines"}) 9 2 11
322 9 push @warnings, $self->_warn_unknown_engine_config_keys($engines);
323 }
324  
325 11 ▶ @warnings or return;
Branch: 1 branch
BranchTrueFalseTotal
@warnings or return 9 2 11
326  
327 9 warn join(
328 "\n",
329 @warnings,
330 'Set strict_config => 0 to silence these warnings.'
331 ) . "\n";
332 }
333  
334 9 ▶sub _warn_unknown_engine_config_keys {
Subroutine _warn_unknown_engine_config_keys: called CC=15 SCAR=27.4
335 9 my ( $self, $engines ) = @_;
336 9 ▶ is_hashref($engines)
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($engines) or return 9 0 9
337 or return;
338  
339 9 my @warnings;
340  
341 9 for my $engine_type ( sort keys %{$engines} ) {
342 12 my $type_config = $engines->{$engine_type};
343 12 ▶ is_hashref($type_config)
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($type_config) or next 12 0 12
344 or next;
345  
346 12 ▶ my $known_type = $KNOWN_ENGINE_CONFIG{$engine_type}
Branch: 1 branch
BranchTrueFalseTotal
my $known_type = $KNOWN_ENGINE_CONFIG{$engine_type} or next 11 1 12
347 or next;
348  
349 11 ▶ my $base_keys = $known_type->{'base_keys'} || {};
Condition100%2/2
$known_type->{'base_keys'} || {}
l!l
74
MC/DC100%1/1
$known_type->{'base_keys'} || {}
$known_type->{'base_keys'}
Truth table100%2/2
A
A= $known_type->{'base_keys'}
Aresult
11
00
350 11 ▶ my $known_engines = $known_type->{'engines'} || {};
Condition50%1/2
$known_type->{'engines'} || {}
l!l
110
MC/DC0%0/1
$known_type->{'engines'} || {}
$known_type->{'engines'}
Truth table50%1/2
A
A= $known_type->{'engines'}
Aresult
11
00
351  
352 11 for my $engine_name ( sort keys %{$type_config} ) {
353 11 my $engine_config = $type_config->{$engine_name};
354 11 ▶ is_hashref($engine_config)
Branch: 1 branch
BranchTrueFalseTotal
is_hashref($engine_config) or next 11 0 11
355 or next;
356  
357 11 my $normalized = _normalize_engine_name($engine_name);
358 11 ▶ defined $normalized
Branch: 1 branch
BranchTrueFalseTotal
defined $normalized or next 11 0 11
359 or next;
360  
361 11 ▶ my $known_engine = $known_engines->{$normalized} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $known_engine = $known_engines->{$normalized} or next 10 1 11
362 10 ▶ next if $known_engine->{'allow_any'};
Branch: 1 branch
BranchTrueFalseTotal
if $known_engine->{"allow_any"} 4 6 10
363  
364 6 ▶ my %allowed = (%{$base_keys}, %{ $known_engine->{keys} || {} });
Condition50%1/2
$known_engine->{'keys'} || {}
l!l
60
MC/DC0%0/1
$known_engine->{'keys'} || {}
$known_engine->{'keys'}
Truth table50%1/2
A
A= $known_engine->{'keys'}
Aresult
11
00
365  
366 6 for my $key ( sort keys %{$engine_config} ) {
367 10 ▶ next if $allowed{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $allowed{$key} 5 5 10
368 5 push @warnings,
369 "Unknown configuration key '$key' for engine '$engine_type/$engine_name'";
370 }
371 }
372 }
373  
374 9 return @warnings;
375 }
376  
377 11 ▶sub _normalize_engine_name {
Subroutine _normalize_engine_name: called CC=3 SCAR=11.2
378 11 my ($name) = @_;
379  
380 11 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 11 11
381 11 ▶ return if $name =~ /::/xms;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/msx 0 11 11
382  
383 11 my $normalized = lc $name;
384 11 $normalized =~ s/_//xmsg;
385 11 return $normalized;
386 }
387  
388 189 1;
389  
390 __END__
« blib/lib/Dancer2/Test.pm ↑ Summary lib/Dancer2/Core/Route.pm »