MasonX::MiniMVC::Dispatcher - Dispatcher class for MasonX::MiniMVC


MasonX-MiniMVC documentation Contained in the MasonX-MiniMVC distribution.

Index


Code Index:

NAME

Top

MasonX::MiniMVC::Dispatcher -- Dispatcher class for MasonX::MiniMVC

VERSION

Top

Version 0.01

DESCRIPTION

Top

For detailed documentation on how to use MasonX::MiniMVC, see the docs for that module.

new(\%controllers)

Takes a hashref of urls/controllers.

dispatch($m)

Dispatches to the appropriate controller.

AUTHOR

Top

Kirrily "Skud" Robert, <skud at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-masonx-minimvc at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MasonX-MiniMVC. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc MasonX::MiniMVC

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/MasonX-MiniMVC

* CPAN Ratings

http://cpanratings.perl.org/d/MasonX-MiniMVC

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=MasonX-MiniMVC

* Search CPAN

http://search.cpan.org/dist/MasonX-MiniMVC

COPYRIGHT & LICENSE

Top


MasonX-MiniMVC documentation Contained in the MasonX-MiniMVC distribution.
package MasonX::MiniMVC::Dispatcher;

use strict;
use warnings;
use base qw(Class::Accessor);

__PACKAGE__->mk_accessors(qw(controllers));

our $VERSION = '0.01';

sub new {
    my ($class, $controllers) = @_;
    my $self = {
        controllers => $controllers,
    };
    bless $self, $class;
    return $self;
}

sub dispatch {
    my ($self, $m) = @_;

    my $dhandler_args = $m->dhandler_arg();

    $dhandler_args =~ s/\/$//; # strip trailing slash

    my ($class, $method, @args) = $self->_find_controller($dhandler_args);
    if ($class) {
        eval "require $class";
        if ($method) {
            if ($class->can($method)) {
                $class->$method($m, @args);
            } else {
                $class->not_found($m, $method, @args);
            }
        } else {
            $class->default($m, @args);
        }
    } else {
        # we want a 404 if we can't find a controller.
        # however, this isn't working for me under CGIHandler.
        $m->clear_and_abort(404);
    }
}

sub _find_controller {
    my ($self, $desired_component, @extra_args) = @_;

    # this probably means we've exhausted the arg stack
    return undef unless $desired_component;

    foreach my $controller (sort keys %{$self->controllers()}) {
        if ($controller =~ /^$desired_component/) {
            return $self->controllers->{$controller}, @extra_args;
        }
    }

    # nothing found yet, so shift the rightmost part of the desired
    # component into the extra args.  ie. "foo/bar/baz", "quux" becomes
    # "foo/bar", "baz", "quux".
    my @parts = split "/", $desired_component;
    unshift @extra_args, pop @parts;
    $desired_component = join "/", @parts;

    $self->_find_controller($desired_component, @extra_args);
}

1;