Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback during search for config file. Site root is considered when no file is found #676

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,9 @@ sub BUILD {
my $self = shift;
$self->init_route_handlers();
$self->_init_hooks();

#Siteroot is saved as fallback when no config file is found using App's path
Dancer2->runner->set_siteroot($self->location) if($self->name && $self->name eq 'main');
}

sub finish {
Expand Down
23 changes: 19 additions & 4 deletions lib/Dancer2/Core/Role/ConfigReader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ sub _build_default_config { +{} }
sub _build_environment { 'development' }

sub _build_config_files {
my ($self) = @_;
my ($self, $forced_location) = @_;

my $location = $self->config_location;
my $location = $forced_location || $self->config_location;
# an undef location means no config files for the caller
return [] unless defined $location;

Expand All @@ -124,8 +124,23 @@ sub _build_config_files {
push @files, $path;
}
}

return [ sort @files ];
if(@files)
{
return [ sort @files ];
}
else
{
#If no file is found in the app config path, search will be repeated under the siteroot.
#This fallback will not be activated if a forced_location is provided (to avoid loop)
if(! $forced_location && Dancer2->runner->config->{'siteroot'})
{
return $self->_build_config_files(Dancer2->runner->config->{'siteroot'});
}
else
{
return [ sort @files ]; #Dumb answer, @files is still empty
}
}
}

sub _build_config {
Expand Down
6 changes: 6 additions & 0 deletions lib/Dancer2/Core/Runner.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ sub _build_config {
};
}

sub set_siteroot {
my $self = shift;
my $siteroot = shift;
$self->config->{'siteroot'} = $siteroot;
}

sub BUILD {
my $self = shift;

Expand Down