From c1ac7da04dd32809c97b630e2390f5afe3f73d6d Mon Sep 17 00:00:00 2001 From: cym0n Date: Mon, 1 Sep 2014 00:16:11 +0200 Subject: [PATCH 1/3] Fallback on siteroot when no config file is found in the path calculated about app --- lib/Dancer2/Core/App.pm | 3 +++ lib/Dancer2/Core/Role/ConfigReader.pm | 31 +++++++++++++++++++++++++++ lib/Dancer2/Core/Runner.pm | 5 +++++ 3 files changed, 39 insertions(+) diff --git a/lib/Dancer2/Core/App.pm b/lib/Dancer2/Core/App.pm index d88096bd6..b86741681 100644 --- a/lib/Dancer2/Core/App.pm +++ b/lib/Dancer2/Core/App.pm @@ -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->siteroot($self->location) if($self->name eq 'main'); } sub finish { diff --git a/lib/Dancer2/Core/Role/ConfigReader.pm b/lib/Dancer2/Core/Role/ConfigReader.pm index 8392aa847..108bff15e 100644 --- a/lib/Dancer2/Core/Role/ConfigReader.pm +++ b/lib/Dancer2/Core/Role/ConfigReader.pm @@ -114,6 +114,37 @@ sub _build_config_files { my @exts = Config::Any->extensions; my @files; + foreach my $ext (@exts) { + foreach my $file ( [ $location, "config.$ext" ], + [ $self->environments_location, "$running_env.$ext" ] ) + { + my $path = path( @{$file} ); + next if !-r $path; + + push @files, $path; + } + } + if(@files) + { + return [ sort @files ]; + } + else + { + return $self->_build_config_files_from_siteroot(); + } +} + +sub _build_config_files_from_siteroot { + my ($self) = @_; + + my $location = Dancer2->runner->siteroot; + # an undef location means no config files for the caller + return [] unless defined $location; + + my $running_env = $self->environment; + my @exts = Config::Any->extensions; + my @files; + foreach my $ext (@exts) { foreach my $file ( [ $location, "config.$ext" ], [ $self->environments_location, "$running_env.$ext" ] ) diff --git a/lib/Dancer2/Core/Runner.pm b/lib/Dancer2/Core/Runner.pm index 9100be0c0..b970dc64e 100644 --- a/lib/Dancer2/Core/Runner.pm +++ b/lib/Dancer2/Core/Runner.pm @@ -73,6 +73,11 @@ has timeout => ( default => sub { $_[0]->config->{'timeout'} }, ); +has siteroot => ( + is => 'rw', + lazy => 1, +); + sub _build_server { my $self = shift; From cf98d4bcfc2abcbb280b4ac7c0ac4ee9fd559b4b Mon Sep 17 00:00:00 2001 From: cym0n Date: Mon, 1 Sep 2014 00:53:24 +0200 Subject: [PATCH 2/3] Better string comparison --- lib/Dancer2/Core/App.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Dancer2/Core/App.pm b/lib/Dancer2/Core/App.pm index b86741681..7d24470b2 100644 --- a/lib/Dancer2/Core/App.pm +++ b/lib/Dancer2/Core/App.pm @@ -729,7 +729,7 @@ sub BUILD { $self->_init_hooks(); #Siteroot is saved as fallback when no config file is found using App's path - Dancer2->runner->siteroot($self->location) if($self->name eq 'main'); + Dancer2->runner->siteroot($self->location) if($self->name && $self->name eq 'main'); } sub finish { From d99372aa520c9defae8196ff53098a6f2b4baaef Mon Sep 17 00:00:00 2001 From: cym0n Date: Sat, 6 Sep 2014 22:48:15 +0200 Subject: [PATCH 3/3] Refactoring. Siteroot is now in the config file and fallback on siteroot is written in a better way --- lib/Dancer2/Core/App.pm | 2 +- lib/Dancer2/Core/Role/ConfigReader.pm | 36 ++++++++------------------- lib/Dancer2/Core/Runner.pm | 11 ++++---- 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/lib/Dancer2/Core/App.pm b/lib/Dancer2/Core/App.pm index 7d24470b2..d7164232f 100644 --- a/lib/Dancer2/Core/App.pm +++ b/lib/Dancer2/Core/App.pm @@ -729,7 +729,7 @@ sub BUILD { $self->_init_hooks(); #Siteroot is saved as fallback when no config file is found using App's path - Dancer2->runner->siteroot($self->location) if($self->name && $self->name eq 'main'); + Dancer2->runner->set_siteroot($self->location) if($self->name && $self->name eq 'main'); } sub finish { diff --git a/lib/Dancer2/Core/Role/ConfigReader.pm b/lib/Dancer2/Core/Role/ConfigReader.pm index 108bff15e..67a4653b8 100644 --- a/lib/Dancer2/Core/Role/ConfigReader.pm +++ b/lib/Dancer2/Core/Role/ConfigReader.pm @@ -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; @@ -130,33 +130,17 @@ sub _build_config_files { } else { - return $self->_build_config_files_from_siteroot(); - } -} - -sub _build_config_files_from_siteroot { - my ($self) = @_; - - my $location = Dancer2->runner->siteroot; - # an undef location means no config files for the caller - return [] unless defined $location; - - my $running_env = $self->environment; - my @exts = Config::Any->extensions; - my @files; - - foreach my $ext (@exts) { - foreach my $file ( [ $location, "config.$ext" ], - [ $self->environments_location, "$running_env.$ext" ] ) + #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'}) { - my $path = path( @{$file} ); - next if !-r $path; - - push @files, $path; + return $self->_build_config_files(Dancer2->runner->config->{'siteroot'}); + } + else + { + return [ sort @files ]; #Dumb answer, @files is still empty } } - - return [ sort @files ]; } sub _build_config { diff --git a/lib/Dancer2/Core/Runner.pm b/lib/Dancer2/Core/Runner.pm index b970dc64e..2b5cb187c 100644 --- a/lib/Dancer2/Core/Runner.pm +++ b/lib/Dancer2/Core/Runner.pm @@ -73,11 +73,6 @@ has timeout => ( default => sub { $_[0]->config->{'timeout'} }, ); -has siteroot => ( - is => 'rw', - lazy => 1, -); - sub _build_server { my $self = shift; @@ -111,6 +106,12 @@ sub _build_config { }; } +sub set_siteroot { + my $self = shift; + my $siteroot = shift; + $self->config->{'siteroot'} = $siteroot; +} + sub BUILD { my $self = shift;