File Coverage

blib/lib/Dancer2/Core/Role/ConfigReader.pm
Criterion Covered Total %
statement 27 31 87.1
branch n/a
condition n/a
subroutine 9 11 81.8
pod n/a
total 36 42 85.7


line stmt bran cond sub pod time code
1             # ABSTRACT: Config reader role for Dancer2 core objects
2             package Dancer2::Core::Role::ConfigReader;
3             $Dancer2::Core::Role::ConfigReader::VERSION = '2.1.0';
4 152     152   624287 use Moo::Role;
  152         14513  
  152         1390  
5              
6 152     152   89951 use Carp 'croak';
  152         389  
  152         9884  
7 152     152   1818 use Path::Tiny ();
  152         12870  
  152         4058  
8 152     152   1288 use Config::Any;
  152         9988  
  152         8381  
9 152     152   1256 use Hash::Merge::Simple;
  152         771  
  152         100145  
10 152     152   1014 use Module::Runtime 'require_module';
  152         399  
  152         2843  
11              
12 152     152   10404 use Dancer2::Core::Factory;
  152         395  
  152         5279  
13 152     152   867 use Dancer2::Core;
  152         354  
  152         4331  
14 152     152   1213 use Dancer2::Core::Types;
  152         368  
  152         1491  
15              
16             has config_location => (
17             is => 'ro',
18             isa => ReadableFilePath,
19             lazy => 1,
20             default => sub { $ENV{DANCER_CONFDIR} || $_[0]->location },
21             );
22              
23             # The type for this attribute is Str because we don't require
24             # an existing directory with configuration files for the
25             # environments. An application without environments is still
26             # valid and works.
27             has environments_location => (
28             is => 'ro',
29             isa => Str,
30             lazy => 1,
31             default => sub {
32             # short circuit
33             defined $ENV{'DANCER_ENVDIR'}
34             and return $ENV{'DANCER_ENVDIR'};
35              
36             my $self = shift;
37              
38             my $last;
39             foreach my $maybe_path ( $self->config_location, $self->location ) {
40             my $path = Path::Tiny::path($maybe_path, 'environments');
41             $last = $path;
42             $path->exists && $path->is_dir
43             and return $path->stringify;
44             }
45              
46             # This is to assure any call on path($environments_location) won't crash
47             # But the calling code will eventually realize the path doesn't exist
48             return $last->stringify;
49             },
50             );
51              
52             has _config_location_path => (
53             is => 'ro',
54             lazy => 1,
55             builder => '_build_config_location_path',
56             init_arg => undef,
57             );
58              
59             has _environments_location_path => (
60             is => 'ro',
61             lazy => 1,
62             builder => '_build_environments_location_path',
63             init_arg => undef,
64             );
65              
66             sub _build_config_location_path {
67 0     0     my $self = shift;
68 0           return Path::Tiny::path( $self->config_location );
69             }
70              
71             sub _build_environments_location_path {
72 0     0     my $self = shift;
73 0           return Path::Tiny::path( $self->environments_location );
74             }
75              
76             # It is required to get environment from the caller.
77             # Environment should be passed down from Dancer2::Core::App.
78             has environment => (
79             is => 'ro',
80             isa => Str,
81             required => 1,
82             );
83              
84             # It is required to get location from the caller.
85             has location => (
86             is => 'ro',
87             isa => ReadableFilePath,
88             required => 1,
89             );
90              
91             1;
92              
93             __END__