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
|
# ABSTRACT: Config reader role for Dancer2 core objects
package Dancer2::Core::Role::ConfigReader;
$Dancer2::Core::Role::ConfigReader::VERSION = '2.0.1';
use Moo::Role;
use File::Spec;
use Config::Any;
use Hash::Merge::Simple;
use Carp 'croak';
use Module::Runtime 'require_module';
use Dancer2::Core::Factory;
use Dancer2::Core;
use Dancer2::Core::Types;
use Dancer2::FileUtils 'path';
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' );
},
);
# It is required to get environment from the caller.
# Environment should be passed down from Dancer2::Core::App.
has environment => (
is => 'ro',
isa => Str,
required => 1,
);
# It is required to get location from the caller.
has location => (
is => 'ro',
isa => ReadableFilePath,
required => 1,
);
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Core::Role::ConfigReader - Config reader role for Dancer2 core objects
=head1 VERSION
version 2.0.1
=head1 DESCRIPTION
This role is implemented by different
config readers. A config reader creates the
configuration for Dancer2 app.
Config can be created by reading configuration
files, from environment variables, by fetching
it from a cloud service, or any other means.
Default config reader is C<Dancer2::ConfigReader::Config::Any>
but user can create his own config reader
if he wants to replace or augment
the default method of config creation.
That method should implement this role.
The implementing module gets the following parameters
during creation:
=head1 ATTRIBUTES
=head2 environment
The name of the environment used, e.g.
production, development, staging.
=head2 location
The absolute path to the directory where the server started.
=head2 default_config
A hash ref which contains the default values.
These arguments are passed when the object is created by
C<Dancer2::Core::App>.
ConfigReader then passes C<environment> and C<location> forward to every
config reader class when it instantiates them.
How the config reader applies them, depend on its needs.
Provides a C<config> attribute that - when accessing
the first time - 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.
=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 where the environment files are stored.
=head2 config
Returns the whole configuration.
=head2 environments
Returns the name of the environment.
=head1 METHODS
=head2 read_config
Load the configuration.
Whatever source the config comes from, files, env vars, etc.
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2025 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
|