| 1 | package Prancer::Config; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '1.05'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Config::Any; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # even though this *should* work automatically, it was not | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | our @CARP_NOT = qw(Prancer Try::Tiny); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 18 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 18 | my ($class, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 18 | my $self = bless({}, $class); | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # find config files, load them | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 18 | my $files = $self->_build_file_list($path); | |||||||||||||||||||||||||||||||||||||||||
| 24 | 18 | $self->{'_config'} = $self->_load_config_files($files); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 18 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | sub has { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | return exists($self->{'_config'}->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 47 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=11 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 47 | my ($self, $key, $default) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # only return things if the are running in a non-void context | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 47 | ▶ | if (defined(wantarray())) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 47 | my $value = undef; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # if ->get is called without any arguments then this will return all | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # config values as either a hash or a hashref. used by template engines | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # to merge config values into the template vars. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 47 | ▶ | if (!defined($key)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return wantarray ? %{$self->{'_config'}} : $self->{'_config'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 47 | ▶ | if (exists($self->{'_config'}->{$key})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 29 | $value = $self->{'_config'}->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 18 | $value = $default; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # nothing to return | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 47 | ▶ | return unless defined($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # make a clone to avoid changing things | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # through inadvertent references. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 32 | ▶ | $value = dclone($value) if ref($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 32 | ▶ | if (wantarray() && ref($value)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
wantarray and ref $value
MC/DC100%2/2
wantarray and ref $value
wantarray
ref $value
Truth table100%3/3
A and B
A= wantarrayB= ref $value
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | # return a value rather than a reference | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | ▶ | if (ref($value) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | return %{$value}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | if (ref($value) eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | return @{$value}; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # return a reference | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 29 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=7 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | my $old = undef; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | $old = $self->get($key) if defined(wantarray()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | if (ref($value)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | # make a copy of the original value to avoid inadvertently changing | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # things through inadvertent references | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->{'_config'}->{$key} = dclone($value); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # can't clone non-references | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | $self->{'_config'}->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | if (wantarray() && ref($old)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
wantarray and ref $old
MC/DC50%1/2
wantarray and ref $old
wantarray
ref $old
Truth table66%2/3
A and B
A= wantarrayB= ref $old
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | # return a value rather than a reference | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if (ref($old) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | return %{$old}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | if (ref($old) eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | return @{$old}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | return $old; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | my $old = undef; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | $old = $self->get($key) if defined(wantarray()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | delete($self->{'_config'}->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | if (wantarray() && ref($old)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
wantarray and ref $old
MC/DC50%1/2
wantarray and ref $old
wantarray
ref $old
Truth table66%2/3
A and B
A= wantarrayB= ref $old
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | # return a value rather than a reference | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | if (ref($old) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return %{$old}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | if (ref($old) eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | return @{$old}; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | return $old; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 18 | ▶ | sub _build_file_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_file_list: called CC=11 SCAR=24.4
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 18 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # an undef location means no config files for the caller | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 18 | ▶ | return [] unless defined($path); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # if the path is a file or a link then there is only one config file | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 18 | ▶ | return [ $path ] if (-e $path && (-f $path || -l $path)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
-e $path and -f $path || -l $path
-f $path or -l $path
MC/DC33%1/3
-e $path and -f $path || -l $path
-e $path
-f $path
-l $path
Truth table50%2/4
A and B or C
A= -e $pathB= -f $pathC= -l $path
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # since we already handled files/symlinks then if the path is not a | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # directory then there is very little we can do | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 9 | ▶ | return [] unless (-d $path); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | # figure out what environment we are operating in by looking in several | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # well known (to the PSGI world) environment variables. if none of them | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # exist then we are probably in dev. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 9 | ▶ | my $env = $ENV{'ENVIRONMENT'} || $ENV{'PLACK_ENV'} || "development"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
$ENV{'ENVIRONMENT'} || $ENV{'PLACK_ENV'} || 'development'
$ENV{"ENVIRONMENT"} or $ENV{"PLACK_ENV"}
MC/DC50%1/2
$ENV{'ENVIRONMENT'} || $ENV{'PLACK_ENV'} || 'development'
$ENV{"ENVIRONMENT"}
$ENV{"PLACK_ENV"}
Truth table66%2/3
A or B
A= $ENV{"ENVIRONMENT"}B= $ENV{"PLACK_ENV"}
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 9 | my @files = (); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 9 | for my $ext (Config::Any->extensions()) { | |||||||||||||||||||||||||||||||||||||||||
| 147 | 90 | for my $file ( | |||||||||||||||||||||||||||||||||||||||||
| 148 | [ $path, "config.${ext}" ], | ||||||||||||||||||||||||||||||||||||||||||
| 149 | [ $path, "${env}.${ext}" ] | ||||||||||||||||||||||||||||||||||||||||||
| 150 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 180 | my $file_path = _normalize_file_path(@{$file}); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 180 | ▶ | push(@files, $file_path) if (-r $file_path); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 9 | return \@files; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 18 | ▶ | sub _load_config_files { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_config_files: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 18 | my ($self, $files) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | return _merge( | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 18 | map { $self->_load_config_file($_) } @{$files} | |||||||||||||||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 18 | ▶ | sub _load_config_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_config_file: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 18 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 18 | my $config = {}; | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 18 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 18 | my @files = ($file); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 18 | my $tmp = Config::Any->load_files({ | |||||||||||||||||||||||||||||||||||||||||
| 174 | 'files' => \@files, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 'use_ext' => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 176 | })->[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 18 | ▶ | ($file, $config) = %{$tmp} if defined($tmp); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $error = (defined($_) ? $_ : "unknown"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | croak "unable to parse ${file}: ${error}"; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 18 | return $config; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 180 | ▶ | sub _normalize_file_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_file_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 180 | my $path = File::Spec->catfile(@_); | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # this is a revised version of what is described in | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # http://www.linuxjournal.com/content/normalizing-path-names-bash | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # by Mitch Frazier | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 180 | my $seqregex = qr{ | |||||||||||||||||||||||||||||||||||||||||
| 193 | [^/]* # anything without a slash | ||||||||||||||||||||||||||||||||||||||||||
| 194 | /\.\.(/|\z) # that is accompanied by two dots as such | ||||||||||||||||||||||||||||||||||||||||||
| 195 | }x; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 180 | $path =~ s{/\./}{/}gx; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 180 | $path =~ s{$seqregex}{}gx; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 180 | $path =~ s{$seqregex}{}x; | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | # see https://rt.cpan.org/Public/Bug/Display.html?id=80077 | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 180 | $path =~ s{^//}{/}x; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 180 | return $path; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | # stolen from Hash::Merge::Simple | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 18 | ▶ | sub _merge { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 18 | my ($left, @right) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 18 | ▶ | return $left unless @right; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | ▶ | return _merge($left, _merge(@right)) if @right > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 3 | my ($right) = @right; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 3 | my %merged = %{$left}; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 3 | for my $key (keys %{$right}) { | |||||||||||||||||||||||||||||||||||||||||
| 217 | 3 | my ($hr, $hl) = map { ref($_->{$key}) eq "HASH" } $right, $left; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | ▶ | if ($hr and $hl) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$hr and $hl
MC/DC0%0/2
$hr and $hl
$hr
$hl
Truth table33%1/3
A and B
A= $hrB= $hl
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $merged{$key} = _merge($left->{$key}, $right->{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | $merged{$key} = $right->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 3 | return \%merged; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | Prancer::Config | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | # load a configuration file when creating a PSGI application | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # this loads only one configuration file | ||||||||||||||||||||||||||||||||||||||||||
| 239 | my $psgi = Foo->new("/path/to/foobar.yml")->to_psgi_app(); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | # just load the configuration and use it wherever | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # this loads all configuration files from the given path using logic | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # described below to figure out which configuration files take precedence | ||||||||||||||||||||||||||||||||||||||||||
| 244 | my $app = Prancer::Core->new("/path/to/mysite/conf"); | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # the configuration can be accessed as either a global method or as an | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # instance method, depending on how you loaded Prancer | ||||||||||||||||||||||||||||||||||||||||||
| 248 | print $app->config->get('foo'); | ||||||||||||||||||||||||||||||||||||||||||
| 249 | print config->get('bar'); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | Prancer uses L<Config::Any> to process configuration files. Anything supported | ||||||||||||||||||||||||||||||||||||||||||
| 254 | by that will be supported by this. It will load configuration files from the | ||||||||||||||||||||||||||||||||||||||||||
| 255 | configuration file or from configuration files in a path based on what you set | ||||||||||||||||||||||||||||||||||||||||||
| 256 | when you create your application. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | To find configuration files from given directory, Prancer::Config follows this | ||||||||||||||||||||||||||||||||||||||||||
| 259 | logic. First, it will look for a file named C<config.ext> where C<ext> is | ||||||||||||||||||||||||||||||||||||||||||
| 260 | something like C<yml> or C<ini>. Then it will look for a file named after the | ||||||||||||||||||||||||||||||||||||||||||
| 261 | currently defined environment like C<develoment.ext> or C<production.ext>. The | ||||||||||||||||||||||||||||||||||||||||||
| 262 | environment is determined by looking first for an environment variable called | ||||||||||||||||||||||||||||||||||||||||||
| 263 | C<ENVIRONMENT> and then for an environment variable called C<PLACK_ENV>. If | ||||||||||||||||||||||||||||||||||||||||||
| 264 | neither of those exist then the default is C<development>. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | Configuration files will be merged such that configuration values pulled out of | ||||||||||||||||||||||||||||||||||||||||||
| 267 | the environment configuration file will take precedence over values from the | ||||||||||||||||||||||||||||||||||||||||||
| 268 | global configuration file. For example, if you have two configuration files: | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | config.ini | ||||||||||||||||||||||||||||||||||||||||||
| 271 | ========== | ||||||||||||||||||||||||||||||||||||||||||
| 272 | foo = bar | ||||||||||||||||||||||||||||||||||||||||||
| 273 | baz = bat | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | development.ini | ||||||||||||||||||||||||||||||||||||||||||
| 276 | =============== | ||||||||||||||||||||||||||||||||||||||||||
| 277 | foo = bazbat | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | After loading these configuration files the value for C<foo> will be C<bazbat> | ||||||||||||||||||||||||||||||||||||||||||
| 280 | and the value for C<baz> will be C<bat>. | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | If you just have one configuration file and have no desire to load multiple | ||||||||||||||||||||||||||||||||||||||||||
| 283 | configuration files based on environments you can specify a file rather than a | ||||||||||||||||||||||||||||||||||||||||||
| 284 | directory when your application is created. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | Arbitrary configuration directives can be put into your configuration files | ||||||||||||||||||||||||||||||||||||||||||
| 287 | and they can be accessed like this: | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | $config->get('foo'); | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | The configuration accessors will only give you the configuration directives | ||||||||||||||||||||||||||||||||||||||||||
| 292 | found at the root of the configuration file. So if you use any data structures | ||||||||||||||||||||||||||||||||||||||||||
| 293 | you will have to decode them yourself. For example, if you create a YAML file | ||||||||||||||||||||||||||||||||||||||||||
| 294 | like this: | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | foo: | ||||||||||||||||||||||||||||||||||||||||||
| 297 | bar1: asdf | ||||||||||||||||||||||||||||||||||||||||||
| 298 | bar2: fdsa | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | Then you will only be able to get the value to C<bar1> like this: | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | my $foo = config->get('foo')->{'bar1'}; | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | =head2 Reserved Configuration Options | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | To support the components of Prancer, some keys are otherwise "reserved" in | ||||||||||||||||||||||||||||||||||||||||||
| 307 | that you aren't able to use them. For example, trying to use the config key | ||||||||||||||||||||||||||||||||||||||||||
| 308 | C<session> will only result in sessions being enabled and you not able to see | ||||||||||||||||||||||||||||||||||||||||||
| 309 | your configuration values. These reserved keys are: C<session> and C<static>. | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =item has I<key> | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | This will return true if the named key exists in the configuration: | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | if ($config->has('foo')) { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | print "I see you've set foo already.\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | It will return false otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =item get I<key> [I<default>] | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | The get method takes two arguments: a key and a default value. If the key does | ||||||||||||||||||||||||||||||||||||||||||
| 328 | not exist then the default value will be returned instead. If the value in the | ||||||||||||||||||||||||||||||||||||||||||
| 329 | configuration is a reference then a clone of the value will be returned to | ||||||||||||||||||||||||||||||||||||||||||
| 330 | avoid modifying the configuration in a strange way. Additionally, this method | ||||||||||||||||||||||||||||||||||||||||||
| 331 | is context sensitive. | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | my $foo = $config->get('foo'); | ||||||||||||||||||||||||||||||||||||||||||
| 334 | my %bar = $config->get('bar'); | ||||||||||||||||||||||||||||||||||||||||||
| 335 | my @baz = $config->get('baz'); | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =item set I<key> I<value> | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | The set method takes two arguments: a key and a value. If the key already | ||||||||||||||||||||||||||||||||||||||||||
| 340 | exists in the configuration then it will be overwritten and the old value will | ||||||||||||||||||||||||||||||||||||||||||
| 341 | be returned in a context sensitive way. If the value is a reference then it | ||||||||||||||||||||||||||||||||||||||||||
| 342 | will be cloned before being saved into the configuration to avoid any | ||||||||||||||||||||||||||||||||||||||||||
| 343 | strangeness. | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | my $old_foo = $config->set('foo', 'bar'); | ||||||||||||||||||||||||||||||||||||||||||
| 346 | my %old_bar = $config->set('bar', { 'baz' => 'bat' }); | ||||||||||||||||||||||||||||||||||||||||||
| 347 | my @old_baz = $config->set('baz', [ 'foo', 'bar', 'baz' ]); | ||||||||||||||||||||||||||||||||||||||||||
| 348 | $config->set('whatever', 'do not care'); | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | =item remove I<key> | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | The remove method takes one argument: the key to remove. The value that was | ||||||||||||||||||||||||||||||||||||||||||
| 353 | removed will be returned in a context sensitive way. | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | =item L<Config::Any> | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =cut | ||||||||||||||||||||||||||||||||||||||||||