| 1 | # ABSTRACT: Config reader role for Dancer2 core objects | ||||||||||||||||||||||||||||
| 2 | package Dancer2::Core::Role::ConfigReader; | ||||||||||||||||||||||||||||
| 3 | 172 | $Dancer2::Core::Role::ConfigReader::VERSION = '2.3.0'; | |||||||||||||||||||||||||||
| 4 | 172 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 172 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 172 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 172 | ▶ | use Config::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 172 | ▶ | use Hash::Merge::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 172 | ▶ | use Module::Runtime 'require_module'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 172 | ▶ | use Dancer2::Core::Factory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 172 | ▶ | use Dancer2::Core; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 172 | ▶ | use Dancer2::Core::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has config_location => ( | ||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||
| 18 | isa => ReadableFilePath, | ||||||||||||||||||||||||||||
| 19 | lazy => 1, | ||||||||||||||||||||||||||||
| 20 | 319 | ▶ | default => sub { $ENV{DANCER_CONFDIR} || $_[0]->location }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 172 | ); | |||||||||||||||||||||||||||
| 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 | 318 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 32 | # short circuit | ||||||||||||||||||||||||||||
| 33 | defined $ENV{'DANCER_ENVDIR'} | ||||||||||||||||||||||||||||
| 34 | 318 | ▶ | and return $ENV{'DANCER_ENVDIR'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 318 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 318 | my $last; | |||||||||||||||||||||||||||
| 39 | 318 | foreach my $maybe_path ( $self->config_location, $self->location ) { | |||||||||||||||||||||||||||
| 40 | 617 | my $path = Path::Tiny::path($maybe_path, 'environments'); | |||||||||||||||||||||||||||
| 41 | 617 | $last = $path; | |||||||||||||||||||||||||||
| 42 | 617 | ▶ | $path->exists && $path->is_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path->exists and $path->is_dir
MC/DC50%1/2
$path->exists and $path->is_dir
$path->exists
$path->is_dir
Truth table66%2/3
A and B
A= $path->existsB= $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 | 299 | return $last->stringify; | |||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | 172 | ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 172 | has _config_location_path => ( | |||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||
| 54 | lazy => 1, | ||||||||||||||||||||||||||||
| 55 | builder => '_build_config_location_path', | ||||||||||||||||||||||||||||
| 56 | init_arg => undef, | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 172 | has _environments_location_path => ( | |||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||
| 61 | lazy => 1, | ||||||||||||||||||||||||||||
| 62 | builder => '_build_environments_location_path', | ||||||||||||||||||||||||||||
| 63 | init_arg => undef, | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub _build_config_location_path { | ||||||||||||||||||||||||||
|
Subroutine
_build_config_location_path: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 0 | return Path::Tiny::path( $self->config_location ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _build_environments_location_path { | ||||||||||||||||||||||||||
|
Subroutine
_build_environments_location_path: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 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 | 172 | has environment => ( | |||||||||||||||||||||||||||
| 79 | is => 'ro', | ||||||||||||||||||||||||||||
| 80 | isa => Str, | ||||||||||||||||||||||||||||
| 81 | required => 1, | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # It is required to get location from the caller. | ||||||||||||||||||||||||||||
| 85 | 172 | has location => ( | |||||||||||||||||||||||||||
| 86 | is => 'ro', | ||||||||||||||||||||||||||||
| 87 | isa => ReadableFilePath, | ||||||||||||||||||||||||||||
| 88 | required => 1, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 172 | 1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||