| 1 | # ABSTRACT: Role that represents the config of Dancer2 App | ||||||||||
| 2 | package Dancer2::Core::Role::HasConfig; | ||||||||||
| 3 | 185 | $Dancer2::Core::Role::HasConfig::VERSION = '2.3.0'; | |||||||||
| 4 | 185 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 185 | ▶ | use Config::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 185 | ▶ | use Hash::Merge::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 185 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 185 | ▶ | use Module::Runtime qw{ require_module use_module }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 185 | ▶ | use Dancer2::Core::Factory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 185 | ▶ | use Dancer2::Core; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 185 | ▶ | use Dancer2::Core::Types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 185 | ▶ | use Dancer2::ConfigUtils 'normalize_config_entry'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 185 | 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 | 2 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 185 | ); | |||||||||
| 28 | |||||||||||
| 29 | has global_triggers => ( | ||||||||||
| 30 | is => 'ro', | ||||||||||
| 31 | isa => HashRef, | ||||||||||
| 32 | 297 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 33 | my $triggers = { | ||||||||||
| 34 | 1 | ▶ | traces => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||
| 35 | 1 | my ( $self, $traces ) = @_; | |||||||||
| 36 | # Carp is already a dependency | ||||||||||
| 37 | 1 | ▶ | $Carp::Verbose = $traces ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | }, | ||||||||||
| 39 | 297 | }; | |||||||||
| 40 | |||||||||||
| 41 | 297 | my $runner_config; | |||||||||
| 42 | { | ||||||||||
| 43 | 185 | ▶ | no warnings 'once'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 297 | ▶ | $runner_config = defined $Dancer2::runner | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | ? Dancer2->runner->config | ||||||||||
| 46 | : {}; | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 297 | for my $global ( keys %$runner_config ) { | |||||||||
| 50 | 1974 | ▶ | next if exists $triggers->{$global}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 3 | ▶ | $triggers->{$global} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 3 | my ($self, $value) = @_; | |||||||||
| 53 | 3 | Dancer2->runner->config->{$global} = $value; | |||||||||
| 54 | } | ||||||||||
| 55 | 1692 | } | |||||||||
| 56 | |||||||||||
| 57 | 297 | return $triggers; | |||||||||
| 58 | }, | ||||||||||
| 59 | 185 | ); | |||||||||
| 60 | |||||||||||
| 61 | 238 | ▶ | sub _set_config_entries { | ||||||||
|
Subroutine
_set_config_entries: called CC=2 SCAR=6.9
| |||||||||||
| 62 | 238 | my ( $self, @args ) = @_; | |||||||||
| 63 | 238 | my $no = scalar @args; | |||||||||
| 64 | 238 | ▶ | while (@args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 244 | $self->_set_config_entry( shift(@args), shift(@args) ); | |||||||||
| 66 | } | ||||||||||
| 67 | 237 | return $no; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 244 | ▶ | sub _set_config_entry { | ||||||||
|
Subroutine
_set_config_entry: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 244 | my ( $self, $name, $value ) = @_; | |||||||||
| 72 | |||||||||||
| 73 | 244 | $value = normalize_config_entry( $name, $value ); | |||||||||
| 74 | 244 | $value = $self->_compile_config_entry( $name, $value, $self->config ); | |||||||||
| 75 | 243 | $self->config->{$name} = $value; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 3 | ▶ | sub settings { shift->config } | ||||||||
|
Subroutine
settings: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 79 | |||||||||||
| 80 | 707 | ▶ | sub setting { | ||||||||
|
Subroutine
setting: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 81 | 707 | my $self = shift; | |||||||||
| 82 | 707 | my @args = @_; | |||||||||
| 83 | |||||||||||
| 84 | return ( scalar @args == 1 ) | ||||||||||
| 85 | 707 | ▶ | ? $self->settings->{ $args[0] } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | : $self->_set_config_entries(@args); | ||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 4 | ▶ | sub has_setting { | ||||||||
|
Subroutine
has_setting: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | 4 | my ( $self, $name ) = @_; | |||||||||
| 91 | 4 | return exists $self->config->{$name}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # private | ||||||||||
| 95 | |||||||||||
| 96 | 244 | ▶ | sub _compile_config_entry { | ||||||||
|
Subroutine
_compile_config_entry: called CC=3 SCAR=11.0
| |||||||||||
| 97 | 244 | my ( $self, $name, $value, $config ) = @_; | |||||||||
| 98 | |||||||||||
| 99 | my $trigger = exists $self->local_triggers->{$name} ? | ||||||||||
| 100 | $self->local_triggers->{$name} : | ||||||||||
| 101 | 244 | ▶ | $self->global_triggers->{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | 244 | ▶ | defined $trigger or return $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 160 | return $trigger->( $self, $value, $config ); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 185 | 1; | |||||||||
| 109 | |||||||||||
| 110 | __END__ | ||||||||||