/usr/share/perl5/Dancer2/Core/Role/ConfigReader.pm is in libdancer2-perl 0.152000+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | # ABSTRACT: Config role for Dancer2 core objects
package Dancer2::Core::Role::ConfigReader;
$Dancer2::Core::Role::ConfigReader::VERSION = '0.152000';
use Moo::Role;
use File::Spec;
use Config::Any;
use Hash::Merge::Simple;
use Carp 'croak';
use Dancer2::Core::Factory;
use Dancer2::Core;
use Dancer2::Core::Types;
use Dancer2::FileUtils 'path';
with 'Dancer2::Core::Role::HasLocation';
has default_config => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => '_build_default_config',
);
has config_location => (
is => 'ro',
isa => ReadableFilePath,
lazy => 1,
default => sub { $ENV{DANCER_CONFDIR} || $_[0]->location },
);
# The type for this attribute is Str because we don't require
# an existing directory with configuration files for the
# environments. An application without environments is still
# valid and works.
has environments_location => (
is => 'ro',
isa => Str,
lazy => 1,
default => sub {
$ENV{DANCER_ENVDIR}
|| File::Spec->catdir( $_[0]->config_location, 'environments' )
|| File::Spec->catdir( $_[0]->location, 'environments' );
},
);
has config => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => '_build_config',
);
has environment => (
is => 'ro',
isa => Str,
lazy => 1,
builder => '_build_environment',
);
has config_files => (
is => 'ro',
lazy => 1,
isa => ArrayRef,
builder => '_build_config_files',
);
has local_triggers => (
is => 'ro',
isa => HashRef,
default => sub { +{} },
);
has global_triggers => (
is => 'ro',
isa => HashRef,
default => sub {
my $triggers = {
traces => sub {
my ( $self, $traces ) = @_;
# Carp is already a dependency
$Carp::Verbose = $traces ? 1 : 0;
},
};
my $runner_config = defined $Dancer2::runner
? Dancer2->runner->config
: {};
for my $global ( keys %$runner_config ) {
next if exists $triggers->{$global};
$triggers->{$global} = sub {
my ($self, $value) = @_;
Dancer2->runner->config->{$global} = $value;
}
}
return $triggers;
},
);
sub _build_default_config { +{} }
sub _build_environment { 'development' }
sub _build_config_files {
my ($self) = @_;
my $location = $self->config_location;
# an undef location means no config files for the caller
return [] unless defined $location;
my $running_env = $self->environment;
my @exts = Config::Any->extensions;
my @files;
foreach my $ext (@exts) {
foreach my $file ( [ $location, "config.$ext" ],
[ $self->environments_location, "$running_env.$ext" ] )
{
my $path = path( @{$file} );
next if !-r $path;
push @files, $path;
}
}
return [ sort @files ];
}
sub _build_config {
my ($self) = @_;
my $location = $self->config_location;
my $default = $self->default_config;
my $config = Hash::Merge::Simple->merge(
$default,
map +( $self->load_config_file($_) ), @{ $self->config_files }
);
$config = $self->_normalize_config($config);
return $config;
}
sub _set_config_entries {
my ( $self, @args ) = @_;
my $no = scalar @args;
while (@args) {
$self->_set_config_entry( shift(@args), shift(@args) );
}
return $no;
}
sub _set_config_entry {
my ( $self, $name, $value ) = @_;
$value = $self->_normalize_config_entry( $name, $value );
$value = $self->_compile_config_entry( $name, $value, $self->config );
$self->config->{$name} = $value;
}
sub _normalize_config {
my ( $self, $config ) = @_;
foreach my $key ( keys %{$config} ) {
my $value = $config->{$key};
$config->{$key} = $self->_normalize_config_entry( $key, $value );
}
return $config;
}
sub _compile_config {
my ( $self, $config ) = @_;
foreach my $key ( keys %{$config} ) {
my $value = $config->{$key};
$config->{$key} =
$self->_compile_config_entry( $key, $value, $config );
}
return $config;
}
sub settings { shift->config }
sub setting {
my $self = shift;
my @args = @_;
return ( scalar @args == 1 )
? $self->settings->{ $args[0] }
: $self->_set_config_entries(@args);
}
sub has_setting {
my ( $self, $name ) = @_;
return exists $self->config->{$name};
}
sub load_config_file {
my ( $self, $file ) = @_;
my $config;
eval {
my @files = ($file);
my $tmpconfig =
Config::Any->load_files( { files => \@files, use_ext => 1 } )->[0];
( $file, $config ) = %{$tmpconfig};
};
if ( my $err = $@ || ( !$config ) ) {
croak "Unable to parse the configuration file: $file: $@";
}
# TODO handle mergeable entries
return $config;
}
sub get_postponed_hooks {
my ($self) = @_;
return $self->postponed_hooks;
}
# private
my $_normalizers = {
charset => sub {
my ($charset) = @_;
return $charset if !length( $charset || '' );
require Encode;
my $encoding = Encode::find_encoding($charset);
croak
"Charset defined in configuration is wrong : couldn't identify '$charset'"
unless defined $encoding;
my $name = $encoding->name;
# Perl makes a distinction between the usual perl utf8, and the strict
# utf8 charset. But we don't want to make this distinction
$name = 'utf-8' if $name eq 'utf-8-strict';
return $name;
},
};
sub _normalize_config_entry {
my ( $self, $name, $value ) = @_;
$value = $_normalizers->{$name}->($value)
if exists $_normalizers->{$name};
return $value;
}
sub _compile_config_entry {
my ( $self, $name, $value, $config ) = @_;
my $trigger = exists $self->local_triggers->{$name} ?
$self->local_triggers->{$name} :
$self->global_triggers->{$name};
defined $trigger or return $value;
return $trigger->( $self, $value, $config );
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Core::Role::ConfigReader - Config role for Dancer2 core objects
=head1 VERSION
version 0.152000
=head1 DESCRIPTION
Provides a C<config> attribute that feeds itself by finding and parsing
configuration files.
Also provides a C<setting()> method which is supposed to be used by externals to
read/write config entries.
=head1 ATTRIBUTES
=head2 location
Absolute path to the directory where the server started.
=head2 config_location
Gets the location from the configuration. Same as C<< $object->location >>.
=head2 environments_location
Gets the directory were the environment files are stored.
=head2 config
Returns the whole configuration.
=head2 environments
Returns the name of the environment.
=head2 config_files
List of all the configuration files.
=head1 ATTRIBUTES
=head1 METHODS
=head2 settings
Alias for config. Equivalent to <<$object->config>>.
=head2 setting
Get or set an element from the configuration.
=head2 has_setting
Verifies that a key exists in the configuration.
=head2 load_config_file
Load the configuration files.
=head2 get_postponed_hooks
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|