Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored finding controller

  • Loading branch information...
commit a143bc44b2772faa7b8a90d0214f464716383fc4 1 parent fab1865
Zbigniew Lukasiak authored
View
26 lib/WebNano.pm
@@ -3,6 +3,8 @@ use warnings;
package WebNano;
+use base 'WebNano::FindController';
+
our $VERSION = '0.001';
use Plack::Response;
use Scalar::Util qw(blessed);
@@ -19,31 +21,13 @@ sub psgi_callback {
sub controller_search_path { [ ref(shift) ] };
-sub find_controller {
- my ( $self, $path_part ) = @_;
- my $controller_class;
- my @path = @{ $self->controller_search_path };
- for my $base ( @path ){
- $controller_class = $base . '::' . $path_part;
- try{
- my $controller_file = $controller_class;
- $controller_file =~ s{::}{/}g;
- $controller_file .= '.pm';
- require $controller_file;
- }
- catch {
- if( $_ && $_ !~ /Can't locate .*$path_part.pm in \@INC/ ){
- die $_;
- }
- };
- }
- return $controller_class;
-}
+sub self_path { '' };
+sub application { shift };
sub handle {
my( $self, $env ) = @_;
my $path = $env->{PATH_INFO};
- my $c_class = $self->find_controller( 'Controller' );
+ my $c_class = $self->find_nested( '' );
$path =~ s{^/}{};
my $out = $c_class->handle(
path => $path,
View
21 lib/WebNano/Controller.pm
@@ -2,6 +2,8 @@ use strict;
use warnings;
package WebNano::Controller;
+use base 'WebNano::FindController';
+
use Try::Tiny;
use URI::Escape 'uri_unescape';
use Plack::Request;
@@ -48,25 +50,8 @@ sub external_dispatch {
my( $path_part, $new_path ) = ( $path =~ qr{^([^/]*)/?(.*)} );
$path_part =~ s/::|'//g if defined( $path_part );
return if !length( $path_part );
- my $controller_class;
- my @path = @{ $self->application->controller_search_path };
- for my $base ( @path ){
- $base =~ s{::}{/}g;
- my $controller_file = "$base/Controller" . $self->self_path . "$path_part.pm";
- try{
- require $controller_file;
- $controller_class = $controller_file;
- $controller_class =~ s/.pm$//;
- $controller_class =~ s{/}{::}g;
- }
- catch {
- if( $_ !~ /Can't locate .*$path_part.pm in \@INC/ ){
- die $_;
- }
- };
- }
+ my $controller_class = $self->find_nested( $path_part );
return if !$controller_class;
- return if !$controller_class->isa( 'WebNano::Controller' );
return $controller_class->handle(
path => $new_path,
self_url => $self->self_url . $path_part . '/',
View
33 lib/WebNano/FindController.pm
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+
+package WebNano::FindController;
+use Try::Tiny;
+
+sub find_nested {
+ my( $self, $sub_path ) = @_;
+ $sub_path =~ s{::}{/};
+ my $controller_class;
+ my @path = @{ $self->application->controller_search_path };
+ for my $base ( @path ){
+ $base =~ s{::}{/}g;
+ my $controller_file = "$base/Controller" . $self->self_path . "$sub_path.pm";
+ try{
+ require $controller_file;
+ $controller_class = $controller_file;
+ $controller_class =~ s/.pm$//;
+ $controller_class =~ s{/}{::}g;
+ }
+ catch {
+ if( $_ !~ /Can't locate .*$sub_path.pm in \@INC/ ){
+ die $_;
+ }
+ };
+ }
+ return if !$controller_class;
+ return if !$controller_class->isa( 'WebNano::Controller' );
+ return $controller_class;
+}
+
+1;
+
Please sign in to comment.
Something went wrong with that request. Please try again.