Web-Components

 view release on metacpan or  search on metacpan

lib/Web/Components/ConfigLoader.pm  view on Meta::CPAN


The configuration file is discovered by the loader once the 'home' attribute
has been established

=item C<has_config_file>

Predicate

=cut

has 'config_file' => is => 'ro', isa => File, predicate => 'has_config_file';

=item C<config_home>

The directory containing the configuration file(s)

=item C<has_config_home>

Predicate

=cut

has 'config_home' =>
   is        => 'ro',
   isa       => Directory,
   predicate => 'has_config_home';

=item C<home>

This is the directory that the loader has chosen to call 'home'

=cut

has 'home' => is => 'ro', isa => Directory;

=item C<local_config_file>

The name of the local configuration file which is optionally set in the
main configuration file

=item C<has_local_config_file>

Predicate

=cut

has 'local_config_file' =>
   is        => 'ro',
   isa       => File|Path,
   coerce    => TRUE,
   predicate => 'has_local_config_file';

sub _config_file_list ($) {
   my $attr = shift;
   (my $name = lc $attr->{appclass}) =~ s{ :: }{-}gmx;
   my $file = $attr->{config_file}
      // ns_environment($attr->{appclass}, 'config')
      // $name;
   my $extensions = $attr->{config_extensions} // 'json yaml';

   return map { "${file}.${_}" } split m{ \s }mx, $extensions;
}

sub _home_indicator_dirs () {
   return qw( var );
}

sub _dist_indicator_files () {
   return qw( Makefile.PL Build.PL dist.ini cpanfile );
}

sub _find_config ($) {
   my $attr = shift;
   my $home = $attr->{home};

   my ($config_home, $config_file);

   for my $dir ($home->catdir('var', 'etc'), $home->catdir('etc'), $home) {
      for my $file (_config_file_list $attr) {
         if ($dir->catfile($file)->exists) {
            $config_home = $dir;
            $config_file = $dir->catfile($file);
            last;
         }
      }

      last if $config_file;
   }

   return ($config_home, $config_file);
}

sub _find_home ($) {
   my $attr  = shift;
   my $class = $attr->{appclass};
   (my $file = "$class.pm") =~ s{::}{/}g;
   my $inc_entry = $INC{$file} or return;
   (my $path = $inc_entry) =~ s{ $file \z }{}mx;

   $path ||= io->cwd if !defined $path || !length $path;

   my $home = io($path)->absolute;

   $home = $home->parent while $home =~ m{ b?lib \z }mx;

   return $home if $home =~ m{ xt \z }mx;

   return $home if grep { $home->catfile($_)->exists } _dist_indicator_files;

   return $home if grep { $home->catdir($_)->exists } _home_indicator_dirs;

   ($path = $inc_entry) =~ s{ \.pm \z }{}mx;
   $home = io($path)->absolute;

   return $home if $home->exists;

   return;
}

=back



( run in 0.912 second using v1.01-cache-2.11-cpan-5511b514fd6 )