| 1 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package WebNano::Controller; | ||||||||||||||||||||||||||||
| 5 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | $WebNano::Controller::VERSION = '0.007'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use URI::Escape 'uri_unescape'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use WebNano::FindController 'find_nested'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Object::Tiny::RW qw/ app env self_url url_map _req path /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 70 | ▶ | sub DEBUG { shift->app->DEBUG } | ||||||||||||||||||||||||||
|
Subroutine
DEBUG: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub req { | ||||||||||||||||||||||||||
|
Subroutine
req: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 19 | 2 | ▶ | return $self->_req if defined $self->_req; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 2 | my $req = Plack::Request->new( $self->env ); | |||||||||||||||||||||||||||
| 21 | 2 | $self->_req( $req ); | |||||||||||||||||||||||||||
| 22 | 2 | return $req; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | ▶ | sub template_search_path { [] } | ||||||||||||||||||||||||||
|
Subroutine
template_search_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 19 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 29 | 19 | return $self->app->renderer->render( c => $self, @_ ); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 71 | ▶ | sub local_dispatch { | ||||||||||||||||||||||||||
|
Subroutine
local_dispatch: called CC=15 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 71 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 34 | 71 | my @parts = @{ $self->path }; | |||||||||||||||||||||||||||
| 35 | 71 | my $name = uri_unescape( shift @parts ); | |||||||||||||||||||||||||||
| 36 | 71 | ▶ | $name = 'index' if !defined( $name ) || !length( $name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
MC/DC50%1/2
defined $name and length $name
defined $name
length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||
| 37 | 71 | my $action; | |||||||||||||||||||||||||||
| 38 | 71 | ▶ | if( my $map = $self->url_map ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 49 | ▶ | if( ref $map eq 'HASH' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 41 | ▶ | $action = $self->can( $map->{$name} ) if $map->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 49 | ▶ | if( ref $map eq 'ARRAY' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 8 | ▶ | $action = $self->can( $name ) if grep { $_ eq $name } @$map; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 71 | ▶ | $action = $self->can( $name . '_action' ) if !$action; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 71 | ▶ | if( ! $action ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 37 | my $method = uc( $self->env->{REQUEST_METHOD} ); | |||||||||||||||||||||||||||
| 49 | 37 | ▶ | $action = $self->can( $name . '_' . $method ) if $method eq 'GET' || $method eq 'POST'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method eq "GET" or $method eq "POST"
MC/DC0%0/2
$method eq "GET" or $method eq "POST"
$method eq "GET"
$method eq "POST"
Truth table66%2/3
A or B
A= $method eq "GET"B= $method eq "POST"
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 71 | my $out; | |||||||||||||||||||||||||||
| 52 | 71 | ▶ | if( $action ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 35 | $out = $action->( $self, @parts ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 71 | ▶ | warn 'No local action found in "' . ref($self) . qq{" for "$name"\n} if !defined( $out ) && $self->DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $out and $self->DEBUG
MC/DC0%0/2
not defined $out and $self->DEBUG
not defined $out
$self->DEBUG
Truth table66%2/3
not(A) and B
A= not defined $outB= $self->DEBUG
| |||||||||||||||||||||||||||||
| 56 | 71 | return $out; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 35 | ▶ | sub _self_path{ | ||||||||||||||||||||||||||
|
Subroutine
_self_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 35 | my $class = shift; | |||||||||||||||||||||||||||
| 62 | 35 | my $path = $class; | |||||||||||||||||||||||||||
| 63 | 35 | $path =~ s/.*::Controller(?=(::|$))//; | |||||||||||||||||||||||||||
| 64 | 35 | $path =~ s{::}{/}; | |||||||||||||||||||||||||||
| 65 | 35 | return $path . '/'; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 35 | ▶ | sub dispatch_to_class { | ||||||||||||||||||||||||||
|
Subroutine
dispatch_to_class: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 35 | my ( $self, $to ) = @_; | |||||||||||||||||||||||||||
| 70 | 35 | ▶ | $to =~ s/::|'//g if defined( $to ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 35 | ▶ | return if !length( $to ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 35 | my $class = ref $self; | |||||||||||||||||||||||||||
| 73 | 35 | my $controller_class = find_nested( $class->_self_path . $to, $self->app->controller_search_path ); | |||||||||||||||||||||||||||
| 74 | 34 | ▶ | if( !$controller_class ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 5 | ▶ | warn qq{No subcontroller found in "$class" for "} . $class->_self_path . $to. qq{"\n} if $self->DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 5 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 29 | ▶ | warn qq{Dispatching to "$controller_class"\n} if $self->DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | return $controller_class->handle( | ||||||||||||||||||||||||||||
| 80 | path => $self->path, | ||||||||||||||||||||||||||||
| 81 | app => $self->app, | ||||||||||||||||||||||||||||
| 82 | 29 | self_url => $self->{self_url} . $to. '/', | |||||||||||||||||||||||||||
| 83 | env => $self->env, | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 71 | ▶ | sub handle { | ||||||||||||||||||||||||||
|
Subroutine
handle: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 71 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 89 | 71 | my $self = $class->new( %args ); | |||||||||||||||||||||||||||
| 90 | 71 | my $out = $self->local_dispatch(); | |||||||||||||||||||||||||||
| 91 | 71 | ▶ | return $out if defined( $out ) || !$self->search_subcontrollers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $out or not $self->search_subcontrollers
MC/DC100%2/2
defined $out or not $self->search_subcontrollers
defined $out
not $self->search_subcontrollers
Truth table100%3/3
A or not(B)
A= defined $outB= not $self->search_subcontrollers
| |||||||||||||||||||||||||||||
| 92 | 35 | my $path_part = shift @{ $self->path }; | |||||||||||||||||||||||||||
| 93 | 35 | return $self->dispatch_to_class( $path_part ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub search_subcontrollers { 0 } | ||||||||||||||||||||||||||
|
Subroutine
search_subcontrollers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | 1; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =pod | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 NAME | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | WebNano::Controller - WebNano Controller | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 VERSION | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | version 0.007 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | This is the WebNano base controller. It's handle method dispatches the request | ||||||||||||||||||||||||||||
| 115 | to appropriate action method or to a next controller. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | The action method should return a string containing the HTML page, | ||||||||||||||||||||||||||||
| 118 | a Plack::Response object or a code ref. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | If there is no suitable method in the current class and the method search_subcontrollers | ||||||||||||||||||||||||||||
| 121 | returns a true value then child controller classes | ||||||||||||||||||||||||||||
| 122 | are tried out. If there is found one that matches the path part then it is | ||||||||||||||||||||||||||||
| 123 | instantiated with the current psgi env and it's handle method is called. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | In a path C</SomeDeepController/OtherController/LeaveController/method> all | ||||||||||||||||||||||||||||
| 126 | C<MyApp::Controoler>, C<MyApp::Controller::SomeDeepController> | ||||||||||||||||||||||||||||
| 127 | and C<MyApp::Controller::SomeDeepController::OtherController> need to | ||||||||||||||||||||||||||||
| 128 | override search_subcontrollers method to return 1. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 131 | With Moose: | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | package MyApp::Controller; | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | use Moose; | ||||||||||||||||||||||||||||
| 136 | use MooseX::NonMoose; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | extends 'WebNano::Controller'; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | has '+url_map' => ( default => sub { { 'Mapped Url' => 'mapped_url' } } ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub index_action { | ||||||||||||||||||||||||||||
| 143 | my $self = shift; | ||||||||||||||||||||||||||||
| 144 | return $self->render( 'index.tt' ); | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub mapped_url { 'This is the mapped url page' } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 METHODS | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 handle | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This is a class method - it receives the arguments, creates the controller | ||||||||||||||||||||||||||||
| 156 | object and then uses it's L<local_dispatch> method. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Should return a Plack::Response object, a string containing the HTML page, a code ref | ||||||||||||||||||||||||||||
| 159 | or undef (which is later interpreted as 404). | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 render | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Renders a template. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 local_dispatch | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Finds the method to be called for a given path and dispatches to it. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 req | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Plack::Reqest made from env | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 template_search_path | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 search_subcontrollers | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | If search_subcontrollers returns true and there are no local actions | ||||||||||||||||||||||||||||
| 178 | then subcontrollers are searched. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 dispatch_to_class | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 DEBUG | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | By default returns the DEBUG flag from the application. When this returns C<true> then | ||||||||||||||||||||||||||||
| 185 | some additional logging is directed to STDOUT. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 url_map | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | A hash that is used as path part to method map. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 app | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Links back to the application object. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 env | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | L<PSGI environment|http://search.cpan.org/~miyagawa/PSGI/PSGI.pod#The_Environment> | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 self_url | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 path | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Zbigniew Lukasiak <zby@cpan.org> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | This software is Copyright (c) 2010 by Zbigniew Lukasiak <zby@cpan.org>. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | __END__ | ||||||||||||||||||||||||||||