| 1 | package Web::Components::ConfigLoader; | ||
| 2 | |||
| 3 | use Web::ComposableRequest::Constants qw( FALSE TRUE ); | ||
| 4 | use File::DataClass::Types qw( Directory File Path ); | ||
| 5 | use Web::Components::Util qw( ns_environment ); | ||
| 6 | use File::DataClass::IO qw( io ); | ||
| 7 | use File::DataClass::Schema; | ||
| 8 | use Moo::Role; | ||
| 9 | |||
| 10 | =encoding utf-8 | ||
| 11 | |||
| 12 | =head1 Name | ||
| 13 | |||
| 14 | Web::Components::ConfigLoader - Configuration file loading role | ||
| 15 | |||
| 16 | =head1 Synopsis | ||
| 17 | |||
| 18 | use Moo; | ||
| 19 | |||
| 20 | with 'Web::Components::ConfigLoader'; | ||
| 21 | |||
| 22 | =head1 Description | ||
| 23 | |||
| 24 | Finds and loads the configuration file(s) | ||
| 25 | |||
| 26 | =head1 Configuration and Environment | ||
| 27 | |||
| 28 | Defines the following attributes; | ||
| 29 | |||
| 30 | =over 3 | ||
| 31 | |||
| 32 | =item C<config_file> | ||
| 33 | |||
| 34 | The configuration file is discovered by the loader once the 'home' attribute | ||
| 35 | has been established | ||
| 36 | |||
| 37 | =item C<has_config_file> | ||
| 38 | |||
| 39 | Predicate | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | has 'config_file' => is => 'ro', isa => File, predicate => 'has_config_file'; | ||
| 44 | |||
| 45 | =item C<config_home> | ||
| 46 | |||
| 47 | The directory containing the configuration file(s) | ||
| 48 | |||
| 49 | =item C<has_config_home> | ||
| 50 | |||
| 51 | Predicate | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | has 'config_home' => | ||
| 56 | is => 'ro', | ||
| 57 | isa => Directory, | ||
| 58 | predicate => 'has_config_home'; | ||
| 59 | |||
| 60 | =item C<home> | ||
| 61 | |||
| 62 | This is the directory that the loader has chosen to call 'home' | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | has 'home' => is => 'ro', isa => Directory; | ||
| 67 | |||
| 68 | =item C<local_config_file> | ||
| 69 | |||
| 70 | The name of the local configuration file which is optionally set in the | ||
| 71 | main configuration file | ||
| 72 | |||
| 73 | =item C<has_local_config_file> | ||
| 74 | |||
| 75 | Predicate | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | has 'local_config_file' => | ||
| 80 | is => 'ro', | ||
| 81 | isa => File|Path, | ||
| 82 | coerce => TRUE, | ||
| 83 | predicate => 'has_local_config_file'; | ||
| 84 | |||
| 85 | sub _config_file_list ($) { | ||
| 86 | my $attr = shift; | ||
| 87 | (my $name = lc $attr->{appclass}) =~ s{ :: }{-}gmx; | ||
| 88 | my $file = $attr->{config_file} | ||
| 89 | // ns_environment($attr->{appclass}, 'config') | ||
| 90 | // $name; | ||
| 91 | my $extensions = $attr->{config_extensions} // 'json yaml'; | ||
| 92 | |||
| 93 | return map { "${file}.${_}" } split m{ \s }mx, $extensions; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _home_indicator_dirs () { | ||
| 97 | return qw( var ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _dist_indicator_files () { | ||
| 101 | return qw( Makefile.PL Build.PL dist.ini cpanfile ); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _find_config ($) { | ||
| 105 | my $attr = shift; | ||
| 106 | my $home = $attr->{home}; | ||
| 107 | |||
| 108 | my ($config_home, $config_file); | ||
| 109 | |||
| 110 | for my $dir ($home->catdir('var', 'etc'), $home->catdir('etc'), $home) { | ||
| 111 | for my $file (_config_file_list $attr) { | ||
| 112 | if ($dir->catfile($file)->exists) { | ||
| 113 | $config_home = $dir; | ||
| 114 | $config_file = $dir->catfile($file); | ||
| 115 | last; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | last if $config_file; | ||
| 120 | } | ||
| 121 | |||
| 122 | return ($config_home, $config_file); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _find_home ($) { | ||
| 126 | my $attr = shift; | ||
| 127 | my $class = $attr->{appclass}; | ||
| 128 | (my $file = "$class.pm") =~ s{::}{/}g; | ||
| 129 | my $inc_entry = $INC{$file} or return; | ||
| 130 | (my $path = $inc_entry) =~ s{ $file \z }{}mx; | ||
| 131 | |||
| 132 | $path ||= io->cwd if !defined $path || !length $path; | ||
| 133 | |||
| 134 | my $home = io($path)->absolute; | ||
| 135 | |||
| 136 | $home = $home->parent while $home =~ m{ b?lib \z }mx; | ||
| 137 | |||
| 138 | return $home if $home =~ m{ xt \z }mx; | ||
| 139 | |||
| 140 | return $home if grep { $home->catfile($_)->exists } _dist_indicator_files; | ||
| 141 | |||
| 142 | return $home if grep { $home->catdir($_)->exists } _home_indicator_dirs; | ||
| 143 | |||
| 144 | ($path = $inc_entry) =~ s{ \.pm \z }{}mx; | ||
| 145 | $home = io($path)->absolute; | ||
| 146 | |||
| 147 | return $home if $home->exists; | ||
| 148 | |||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | =head1 Subroutines/Methods | ||
| 155 | |||
| 156 | Defines the following methods; | ||
| 157 | |||
| 158 | =over 3 | ||
| 159 | |||
| 160 | =item C<BUILDARGS> | ||
| 161 | |||
| 162 | Modifies the method in the base class. Starting with C<appclass> it discovers | ||
| 163 | C<home>, then it discovers C<config_home> and C<config_file>, then it loads the | ||
| 164 | configuration file. If this defines C<local_config_file> that to is loaded | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | around 'BUILDARGS' => sub { | ||
| 169 | my ($orig, $self, @args) = @_; | ||
| 170 | |||
| 171 | my $attr = $orig->($self, @args); | ||
| 172 | |||
| 173 | if ($attr->{appclass}) { | ||
| 174 | my $home = io $attr->{home} if defined $attr->{home} and -d $attr->{home}; | ||
| 175 | my $env_var = ns_environment $attr->{appclass}, 'home'; | ||
| 176 | |||
| 177 | $home = io $env_var if !$home and $env_var and -d $env_var; | ||
| 178 | $home = _find_home $attr if !$home; | ||
| 179 | $attr->{home} = $home if $home; | ||
| 180 | } | ||
| 181 | |||
| 182 | if ($attr->{appclass} && $attr->{home}) { | ||
| 183 | my ($config_home, $config_file) = _find_config $attr; | ||
| 184 | |||
| 185 | $attr->{config_home} = $config_home if $config_home; | ||
| 186 | $attr->{config_file} = $config_file if $config_file; | ||
| 187 | } | ||
| 188 | |||
| 189 | my $schema = File::DataClass::Schema->new( storage_class => 'Any' ); | ||
| 190 | |||
| 191 | if ($attr->{config_file}) { | ||
| 192 | $attr = { %{$attr}, %{$schema->load($attr->{config_file})} }; | ||
| 193 | } | ||
| 194 | |||
| 195 | if (my $file = $attr->{local_config_file}) { | ||
| 196 | my $config_file = $attr->{config_home}->catfile($file); | ||
| 197 | |||
| 198 | if ($config_file->exists) { | ||
| 199 | $attr->{local_config_file} = $config_file; | ||
| 200 | $attr = { %{$attr}, %{$schema->load($config_file)} }; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | if ($attr->{home} && $attr->{home}->catdir('var')->exists) { | ||
| 205 | $attr->{vardir} = $attr->{home}->catdir('var'); | ||
| 206 | } | ||
| 207 | |||
| 208 | return $attr; | ||
| 209 | }; | ||
| 210 | |||
| 211 | use namespace::autoclean; | ||
| 212 | |||
| 213 | 1; | ||
| 214 | |||
| 215 | __END__ |