Debian Codegraph @

lib/Dancer2/Core/Role/HasConfig.pm

raw
1 # ABSTRACT: Role that represents the config of Dancer2 App
2 package Dancer2::Core::Role::HasConfig;
3 $Dancer2::Core::Role::HasConfig::VERSION = '2.2.2';
4 use Moo::Role;
5
6 use Config::Any;
7 use Hash::Merge::Simple;
8 use Carp 'croak';
9 use Module::Runtime qw{ require_module use_module };
10
11 use Dancer2::Core::Factory;
12 use Dancer2::Core;
13 use Dancer2::Core::Types;
14 use Dancer2::ConfigUtils 'normalize_config_entry';
15
16 has config => (
17 is => 'ro',
18 isa => HashRef,
19 lazy => 1,
20 builder => '_build_config',
21 );
22
23 has local_triggers => (
24 is => 'ro',
25 isa => HashRef,
26 default => sub { +{} },
27 );
28
29 has global_triggers => (
30 is => 'ro',
31 isa => HashRef,
32 default => sub {
33 my $triggers = {
34 traces => sub {
35 my ( $self, $traces ) = @_;
36 # Carp is already a dependency
37 $Carp::Verbose = $traces ? 1 : 0;
38 },
39 };
40
41 my $runner_config;
42 {
43 no warnings 'once';
44 $runner_config = defined $Dancer2::runner
45 ? Dancer2->runner->config
46 : {};
47 }
48
49 for my $global ( keys %$runner_config ) {
50 next if exists $triggers->{$global};
51 $triggers->{$global} = sub {
52 my ($self, $value) = @_;
53 Dancer2->runner->config->{$global} = $value;
54 }
55 }
56
57 return $triggers;
58 },
59 );
60
61 sub _set_config_entries {
62 my ( $self, @args ) = @_;
63 my $no = scalar @args;
64 while (@args) {
65 $self->_set_config_entry( shift(@args), shift(@args) );
66 }
67 return $no;
68 }
69
70 sub _set_config_entry {
71 my ( $self, $name, $value ) = @_;
72
73 $value = normalize_config_entry( $name, $value );
74 $value = $self->_compile_config_entry( $name, $value, $self->config );
75 $self->config->{$name} = $value;
76 }
77
78 sub settings { shift->config }
79
80 sub setting {
81 my $self = shift;
82 my @args = @_;
83
84 return ( scalar @args == 1 )
85 ? $self->settings->{ $args[0] }
86 : $self->_set_config_entries(@args);
87 }
88
89 sub has_setting {
90 my ( $self, $name ) = @_;
91 return exists $self->config->{$name};
92 }
93
94 # private
95
96 sub _compile_config_entry {
97 my ( $self, $name, $value, $config ) = @_;
98
99 my $trigger = exists $self->local_triggers->{$name} ?
100 $self->local_triggers->{$name} :
101 $self->global_triggers->{$name};
102
103 defined $trigger or return $value;
104
105 return $trigger->( $self, $value, $config );
106 }
107
108 1;
109
110 __END__
111
112 =pod
113
114 =encoding UTF-8
115
116 =head1 NAME
117
118 Dancer2::Core::Role::HasConfig - Role that represents the config of Dancer2 App
119
120 =head1 VERSION
121
122 version 2.2.2
123
124 =head1 DESCRIPTION
125
126 This role provides a C<config> attribute that is
127 used to read the configuration.
128 When accessing
129 the first time, it calls method C<_build_config()> which
130 must be implemented by the using class.
131 This method should return the whole config which has been
132 created by executing one or more
133 B<ConfigReader> packages.
134
135 Also provides a C<setting()> method which is supposed to be used by externals to
136 read/write config entries.
137
138 =head1 ATTRIBUTES
139
140 =head2 config
141
142 Returns the whole configuration.
143
144 =head1 METHODS
145
146 =head2 settings
147
148 Alias for config. Equivalent to <<$object->config>>.
149
150 =head2 setting
151
152 Get or set an element from the configuration.
153
154 =head2 has_setting
155
156 Verifies that a key exists in the configuration.
157
158 =head1 AUTHOR
159
160 Dancer Core Developers
161
162 =head1 COPYRIGHT AND LICENSE
163
164 This software is copyright (c) 2026 by Alexis Sukrieh.
165
166 This is free software; you can redistribute it and/or modify it under
167 the same terms as the Perl 5 programming language system itself.
168
169 =cut