| 1 | package MasonX::MiniMVC::Dispatcher; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use base qw(Class::Accessor); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | __PACKAGE__->mk_accessors(qw(controllers)); | |||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | MasonX::MiniMVC::Dispatcher -- Dispatcher class for MasonX::MiniMVC | ||||||||||
| 12 | |||||||||||
| 13 | =head1 VERSION | ||||||||||
| 14 | |||||||||||
| 15 | Version 0.01 | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 2 | our $VERSION = '0.01'; | |||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | For detailed documentation on how to use MasonX::MiniMVC, see the docs | ||||||||||
| 24 | for that module. | ||||||||||
| 25 | |||||||||||
| 26 | =head2 new(\%controllers) | ||||||||||
| 27 | |||||||||||
| 28 | Takes a hashref of urls/controllers. | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 33 | 0 | my ($class, $controllers) = @_; | |||||||||
| 34 | 0 | my $self = { | |||||||||
| 35 | controllers => $controllers, | ||||||||||
| 36 | }; | ||||||||||
| 37 | 0 | bless $self, $class; | |||||||||
| 38 | 0 | return $self; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | =head2 dispatch($m) | ||||||||||
| 42 | |||||||||||
| 43 | Dispatches to the appropriate controller. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | sub dispatch { | ||||||||
|
Subroutine
dispatch: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 48 | 0 | my ($self, $m) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 0 | my $dhandler_args = $m->dhandler_arg(); | |||||||||
| 51 | |||||||||||
| 52 | 0 | $dhandler_args =~ s/\/$//; # strip trailing slash | |||||||||
| 53 | |||||||||||
| 54 | 0 | my ($class, $method, @args) = $self->_find_controller($dhandler_args); | |||||||||
| 55 | 0 | ▶ | if ($class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | eval "require $class"; | |||||||||
| 57 | 0 | ▶ | if ($method) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | ▶ | if ($class->can($method)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | $class->$method($m, @args); | |||||||||
| 60 | } else { | ||||||||||
| 61 | 0 | $class->not_found($m, $method, @args); | |||||||||
| 62 | } | ||||||||||
| 63 | } else { | ||||||||||
| 64 | 0 | $class->default($m, @args); | |||||||||
| 65 | } | ||||||||||
| 66 | } else { | ||||||||||
| 67 | # we want a 404 if we can't find a controller. | ||||||||||
| 68 | # however, this isn't working for me under CGIHandler. | ||||||||||
| 69 | 0 | $m->clear_and_abort(404); | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub _find_controller { | ||||||||
|
Subroutine
_find_controller: not called CC=4 SCAR=30.0
| |||||||||||
| 74 | 0 | my ($self, $desired_component, @extra_args) = @_; | |||||||||
| 75 | |||||||||||
| 76 | # this probably means we've exhausted the arg stack | ||||||||||
| 77 | 0 | ▶ | return undef unless $desired_component; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | foreach my $controller (sort keys %{$self->controllers()}) { | |||||||||
| 80 | 0 | ▶ | if ($controller =~ /^$desired_component/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | return $self->controllers->{$controller}, @extra_args; | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | # nothing found yet, so shift the rightmost part of the desired | ||||||||||
| 86 | # component into the extra args. ie. "foo/bar/baz", "quux" becomes | ||||||||||
| 87 | # "foo/bar", "baz", "quux". | ||||||||||
| 88 | 0 | my @parts = split "/", $desired_component; | |||||||||
| 89 | 0 | unshift @extra_args, pop @parts; | |||||||||
| 90 | 0 | $desired_component = join "/", @parts; | |||||||||
| 91 | |||||||||||
| 92 | 0 | $self->_find_controller($desired_component, @extra_args); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =head1 AUTHOR | ||||||||||
| 96 | |||||||||||
| 97 | Kirrily "Skud" Robert, C<< <skud at cpan.org> >> | ||||||||||
| 98 | |||||||||||
| 99 | =head1 BUGS | ||||||||||
| 100 | |||||||||||
| 101 | Please report any bugs or feature requests to | ||||||||||
| 102 | C<bug-masonx-minimvc at rt.cpan.org>, or through the web interface at | ||||||||||
| 103 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MasonX-MiniMVC>. | ||||||||||
| 104 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||
| 105 | your bug as I make changes. | ||||||||||
| 106 | |||||||||||
| 107 | =head1 SUPPORT | ||||||||||
| 108 | |||||||||||
| 109 | You can find documentation for this module with the perldoc command. | ||||||||||
| 110 | |||||||||||
| 111 | perldoc MasonX::MiniMVC | ||||||||||
| 112 | |||||||||||
| 113 | You can also look for information at: | ||||||||||
| 114 | |||||||||||
| 115 | =over 4 | ||||||||||
| 116 | |||||||||||
| 117 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 118 | |||||||||||
| 119 | L<http://annocpan.org/dist/MasonX-MiniMVC> | ||||||||||
| 120 | |||||||||||
| 121 | =item * CPAN Ratings | ||||||||||
| 122 | |||||||||||
| 123 | L<http://cpanratings.perl.org/d/MasonX-MiniMVC> | ||||||||||
| 124 | |||||||||||
| 125 | =item * RT: CPAN's request tracker | ||||||||||
| 126 | |||||||||||
| 127 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MasonX-MiniMVC> | ||||||||||
| 128 | |||||||||||
| 129 | =item * Search CPAN | ||||||||||
| 130 | |||||||||||
| 131 | L<http://search.cpan.org/dist/MasonX-MiniMVC> | ||||||||||
| 132 | |||||||||||
| 133 | =back | ||||||||||
| 134 | |||||||||||
| 135 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 136 | |||||||||||
| 137 | Copyright 2007 Kirrily "Skud" Robert, all rights reserved. | ||||||||||
| 138 | |||||||||||
| 139 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 140 | under the same terms as Perl itself. | ||||||||||
| 141 | |||||||||||
| 142 | =cut | ||||||||||
| 143 | |||||||||||
| 144 | 2 | 1; | |||||||||