ASP4x-Router

 view release on metacpan or  search on metacpan

lib/ASP4x/Router.pm  view on Meta::CPAN


package ASP4x::Router;

use strict;
use warnings 'all';
use base 'ASP4::RequestFilter';
BEGIN {
  # Only conditionally inherit from ASP4::TransHandler':
  eval { require ASP4::TransHandler };
  push @ASP4x::Router::ISA, 'ASP4::TransHandler' unless $@;
}
use Router::Generic;
use ASP4::ConfigLoader;
use vars __PACKAGE__->VARS;

our $VERSION = '0.022';

our %routers = ( );


sub handler : method
{
  my ($class, $r) = @_;
  
  return -1 if $r->pnotes('__routed');
  $r->pnotes( __routed => 1 );
  
  my $res = $class->SUPER::handler( $r );
  my $Config = ASP4::ConfigLoader->load;
  
  if( my $app = eval { $Config->app } )
  {
    map {
      $Config->load_class( $_ );
      $_->import;
    } @$app;
  }# end if()
  
  my $router = $class->get_router();
  $r->pnotes( route => $router->route_for( $r->uri, $r->method ) );
  
  my $fullpath = $r->document_root . $r->uri;
  if( $fullpath =~ m{/$} && -f $fullpath . 'index.asp' )
  {
    $r->uri( $r->uri . 'index.asp' );
    return -1;
  }
  elsif( -f $fullpath )
  {
    return -1;
  }
  elsif( $r->uri =~ m{^/handlers/} )
  {
    (my $handler_path = $r->uri) =~ s{\.}{/}g;
    $handler_path = $Config->web->application_root . "$handler_path.pm";
    if( -f $handler_path )
    {
      return -1;
    }# end if()
  }# end if()
  
  return $res unless $router;
  
  my @matches = $router->match( $r->uri . ( $r->args ? '?' . $r->args : '' ), $r->method )
    or return -1;
  
  # TODO: Check matches to see if maybe they point to another route not on disk:
  my ($new_uri) = grep {
    my ($path) = split /\?/, $_;
    if( m{^/handlers/} )
    {
      $path =~ s/\./\//g;
      $path .= ".pm";
      if( -f $Config->web->application_root . $path )
      {
        1;
      }# end if()
    }
    else
    {
      -f ($r->document_root . $path);
    }# end if()
  } @matches
    or return -1;
  
  # Require a trailing '/' on the end of the URI:
  unless( $r->uri =~ m{\.[^/]+$} || $r->uri =~ m{/$} )
  {



( run in 0.829 second using v1.01-cache-2.11-cpan-39bf76dae61 )