| 1 | package CatalystX::ASP::Dispatcher; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Catalyst::Action; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | extends 'Catalyst::DispatchType'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Catalyst::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Text::SimpleTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | CatalystX::ASP::Dispatcher - Catalyst DispatchType to match .asp requests | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package MyApp; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | after 'setup_dispatcher' => sub { | ||||||||||||||||||||||||||||
| 21 | push @{$shift->dispatcher->preload_dispatch_types}, '+CatalystX::ASP::Dispatcher'; | ||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | __PACKAGE__->config('CatalystX::ASP' => { | ||||||||||||||||||||||||||||
| 25 | Dispatcher => { | ||||||||||||||||||||||||||||
| 26 | match_pattern => '\.asp$' | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | }); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This DispatchType will match any requests ending with .asp. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has 'default_action' => ( | ||||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||
| 38 | isa => 'Catalyst::Action', | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | return Catalyst::Action->new( | ||||||||||||||||||||||||||||
| 41 | name => 'asp', | ||||||||||||||||||||||||||||
| 42 | 9 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 9 | my ( $self, $c, @args ) = @_; | |||||||||||||||||||||||||||
| 44 | 9 | $c->forward( $c->view( 'ASP' ), \@args ); | |||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||
| 46 | 1 | reverse => '.asp', | |||||||||||||||||||||||||||
| 47 | namespace => '', | ||||||||||||||||||||||||||||
| 48 | class => 'CatalystX::ASP::Controller', | ||||||||||||||||||||||||||||
| 49 | attributes => [qw(ASP)], | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | 1 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | has '_config' => ( | |||||||||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||||||||||
| 56 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 57 | predicate => '_has_config', | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | coerce 'Regexp' | ||||||||||||||||||||||||||||
| 61 | => from 'Str' | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | => via {qr/$_/i}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | has 'match_pattern' => ( | ||||||||||||||||||||||||||||
| 65 | is => 'rw', | ||||||||||||||||||||||||||||
| 66 | isa => 'Regexp', | ||||||||||||||||||||||||||||
| 67 | coerce => 1, | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | default => sub {qr/\.asp$/i}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 1 | ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | has '_registered_actions' => ( | ||||||||||||||||||||||||||||
| 72 | is => 'rw', | ||||||||||||||||||||||||||||
| 73 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 1 | traits => [qw(Hash)], | |||||||||||||||||||||||||||
| 76 | handles => { | ||||||||||||||||||||||||||||
| 77 | _register_action => 'set', | ||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 METHODS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =over | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =item $self->list($c) | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Debug output for ASP dispatch points | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 0 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 93 | 0 | my $avail_width = Catalyst::Utils::term_width() - 9; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $col1_width = ( $avail_width * .50 ) < 35 ? 35 : int( $avail_width * .50 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $col2_width = $avail_width - $col1_width; | |||||||||||||||||||||||||||
| 96 | 0 | my $asp = Text::SimpleTable->new( | |||||||||||||||||||||||||||
| 97 | [ $col1_width, 'Path' ], [ $col2_width, 'Private' ] | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | 0 | $self->_init_config( $c->config->{'CatalystX::ASP'}{Dispatcher} ); | |||||||||||||||||||||||||||
| 100 | 0 | $asp->row( $self->match_pattern, '/asp' ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $c->log->debug( "Loaded ASP actions:\n" . $asp->draw . "\n" ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item $self->match($c, $path) | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Checks if request path ends with .asp, and if file exists. Then creates custom | ||||||||||||||||||||||||||||
| 108 | action to forward to ASP View. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 15 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 15 | my ( $self, $c, $path ) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 15 | $self->_init_config( $c->config->{'CatalystX::ASP'}{Dispatcher} ); | |||||||||||||||||||||||||||
| 116 | 15 | my $match_pattern = $self->match_pattern; | |||||||||||||||||||||||||||
| 117 | 15 | ▶ | if ( $c->req->path =~ m/$match_pattern/ && -f $c->path_to( 'root', $c->req->path ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->req->path =~ /$match_pattern/i and -f $c->path_to("root", $c->req->path)
MC/DC100%2/2
$c->req->path =~ /$match_pattern/i and -f $c->path_to("root", $c->req->path)
$c->req->path =~ /$match_pattern/i
-f $c->path_to("root", $c->req->path)
Truth table100%3/3
A and B
A= $c->req->path =~ /$match_pattern/iB= -f $c->path_to("root", $c->req->path)
| |||||||||||||||||||||||||||||
| 118 | 9 | $c->req->action( $path ); | |||||||||||||||||||||||||||
| 119 | 9 | $c->req->match( $path ); | |||||||||||||||||||||||||||
| 120 | 9 | $c->action( $self->default_action ); | |||||||||||||||||||||||||||
| 121 | 9 | $c->namespace( $self->default_action->namespace ); | |||||||||||||||||||||||||||
| 122 | 9 | return 1; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 6 | return 0; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item $self->register( $c, $action ) | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Registers the generated action | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 12 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 12 | my ( $self, $c, $action ) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 12 | ▶ | return $self->_register_action( $action->name => 1 ) if $action->attributes->{ASP}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item $self->uri_for_action($action, $captures) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Get a URI part for an action | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub uri_for_action { | ||||||||||||||||||||||||||
|
Subroutine
uri_for_action: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 0 | my ( $self, $c, $action, $captures ) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | return $action->private_path; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 15 | ▶ | sub _init_config { | ||||||||||||||||||||||||||
|
Subroutine
_init_config: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 153 | 15 | my ( $self, $config ) = @_; | |||||||||||||||||||||||||||
| 154 | 15 | ▶ | return if $self->_has_config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | $self->_config( $config || {} ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$config || {}
MC/DC0%0/1
$config || {}
$config
Truth table50%1/2
A
A= $config
| |||||||||||||||||||||||||||||
| 156 | 1 | $self->$_( $config->{$_} ) for ( keys %$config ); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =back | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =over | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item * L<CatalystX::ASP::Role> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item * L<CatalystX::ASP::View> | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =back | ||||||||||||||||||||||||||||