| 1 |
# ABSTRACT: Config reader role for Dancer2 core objects |
| 2 |
package Dancer2::Core::Role::ConfigReader; |
| 3 |
$Dancer2::Core::Role::ConfigReader::VERSION = '2.2.2'; |
| 4 |
use Moo::Role; |
| 5 |
|
| 6 |
use Carp 'croak'; |
| 7 |
use Path::Tiny (); |
| 8 |
use Config::Any; |
| 9 |
use Hash::Merge::Simple; |
| 10 |
use Module::Runtime 'require_module'; |
| 11 |
|
| 12 |
use Dancer2::Core::Factory; |
| 13 |
use Dancer2::Core; |
| 14 |
use Dancer2::Core::Types; |
| 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 |
my $self = shift; |
| 68 |
return Path::Tiny::path( $self->config_location ); |
| 69 |
} |
| 70 |
|
| 71 |
sub _build_environments_location_path { |
| 72 |
my $self = shift; |
| 73 |
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__ |
| 94 |
|
| 95 |
=pod |
| 96 |
|
| 97 |
=encoding UTF-8 |
| 98 |
|
| 99 |
=head1 NAME |
| 100 |
|
| 101 |
Dancer2::Core::Role::ConfigReader - Config reader role for Dancer2 core objects |
| 102 |
|
| 103 |
=head1 VERSION |
| 104 |
|
| 105 |
version 2.2.2 |
| 106 |
|
| 107 |
=head1 DESCRIPTION |
| 108 |
|
| 109 |
This role is implemented by different |
| 110 |
config readers. A config reader creates the |
| 111 |
configuration for Dancer2 app. |
| 112 |
Config can be created by reading configuration |
| 113 |
files, from environment variables, by fetching |
| 114 |
it from a cloud service, or any other means. |
| 115 |
|
| 116 |
Default config reader is C<Dancer2::ConfigReader::Config::Any> |
| 117 |
but user can create his own config reader |
| 118 |
if he wants to replace or augment |
| 119 |
the default method of config creation. |
| 120 |
That method should implement this role. |
| 121 |
|
| 122 |
The implementing module gets the following parameters |
| 123 |
during creation: |
| 124 |
|
| 125 |
=head1 ATTRIBUTES |
| 126 |
|
| 127 |
=head2 environment |
| 128 |
|
| 129 |
The name of the environment used, e.g. |
| 130 |
production, development, staging. |
| 131 |
|
| 132 |
=head2 location |
| 133 |
|
| 134 |
The absolute path to the directory where the server started. |
| 135 |
|
| 136 |
=head2 default_config |
| 137 |
|
| 138 |
A hash ref which contains the default values. |
| 139 |
|
| 140 |
These arguments are passed when the object is created by |
| 141 |
C<Dancer2::Core::App>. |
| 142 |
ConfigReader then passes C<environment> and C<location> forward to every |
| 143 |
config reader class when it instantiates them. |
| 144 |
How the config reader applies them, depend on its needs. |
| 145 |
|
| 146 |
Provides a C<config> attribute that - when accessing |
| 147 |
the first time - feeds itself by finding and parsing |
| 148 |
configuration files. |
| 149 |
|
| 150 |
Also provides a C<setting()> method which is |
| 151 |
supposed to be used by externals to |
| 152 |
read/write config entries. |
| 153 |
|
| 154 |
=head2 location |
| 155 |
|
| 156 |
Absolute path to the directory where the server started. |
| 157 |
|
| 158 |
=head2 config_location |
| 159 |
|
| 160 |
Gets the location from the configuration. Same as C<< $object->location >>. |
| 161 |
|
| 162 |
=head2 environments_location |
| 163 |
|
| 164 |
Gets the directory where the environment files are stored. |
| 165 |
|
| 166 |
=head2 config |
| 167 |
|
| 168 |
Returns the whole configuration. |
| 169 |
|
| 170 |
=head2 environments |
| 171 |
|
| 172 |
Returns the name of the environment. |
| 173 |
|
| 174 |
=head1 METHODS |
| 175 |
|
| 176 |
=head2 read_config |
| 177 |
|
| 178 |
Load the configuration. |
| 179 |
Whatever source the config comes from, files, env vars, etc. |
| 180 |
|
| 181 |
=head1 AUTHOR |
| 182 |
|
| 183 |
Dancer Core Developers |
| 184 |
|
| 185 |
=head1 COPYRIGHT AND LICENSE |
| 186 |
|
| 187 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 188 |
|
| 189 |
This is free software; you can redistribute it and/or modify it under |
| 190 |
the same terms as the Perl 5 programming language system itself. |
| 191 |
|
| 192 |
=cut |