| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||
| 3 | # File: GX/Route/Static.pm | ||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 5 | |||||||||||
| 6 | package GX::Route::Static; | ||||||||||
| 7 | |||||||||||
| 8 | 23 | ▶ | use GX::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 12 | # Class setup | ||||||||||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 23 | ▶ | use GX::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 23 | extends 'GX::Route'; | |||||||||
| 18 | |||||||||||
| 19 | has 'action' => ( | ||||||||||
| 20 | isa => 'Object', | ||||||||||
| 21 | required => 1, | ||||||||||
| 22 | 67 | ▶ | constraint => sub { $_->isa( 'GX::Action' ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 23 | accessor => { type => 'get' } | |||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | has 'host' => ( | ||||||||||
| 27 | isa => 'Scalar', | ||||||||||
| 28 | initialize => 1, | ||||||||||
| 29 | 19 | ▶ | constraint => sub { defined && length }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 30 | 23 | accessor => { type => 'get' } | |||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 23 | has 'is_reversible' => ( | |||||||||
| 34 | isa => 'Bool', | ||||||||||
| 35 | initialize => 1, | ||||||||||
| 36 | default => 1, | ||||||||||
| 37 | accessor => { type => 'get' } | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | has 'method' => ( | ||||||||||
| 41 | isa => 'Scalar', | ||||||||||
| 42 | initialize => 1, | ||||||||||
| 43 | 13 | ▶ | constraint => sub { defined && length }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 44 | 13 | ▶ | processor => sub { $_ = uc }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 23 | accessor => { type => 'get' } | |||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | has 'path' => ( | ||||||||||
| 49 | isa => 'String', | ||||||||||
| 50 | required => 1, | ||||||||||
| 51 | 67 | ▶ | constraint => sub { /^\// }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 23 | accessor => { type => 'get' } | |||||||||
| 53 | ); | ||||||||||
| 54 | |||||||||||
| 55 | has 'scheme' => ( | ||||||||||
| 56 | isa => 'Scalar', | ||||||||||
| 57 | initialize => 1, | ||||||||||
| 58 | 13 | ▶ | constraint => sub { defined && length }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 59 | 13 | ▶ | processor => sub { $_ = lc }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 23 | accessor => { type => 'get' } | |||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | 23 | build; | |||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 67 | # Public methods | ||||||||||
| 68 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 69 | |||||||||||
| 70 | 11 | ▶ | sub construct_path { | ||||||||
|
Subroutine
construct_path: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 71 | |||||||||||
| 72 | 11 | my $self = shift; | |||||||||
| 73 | |||||||||||
| 74 | 11 | ▶ | if ( ! $self->{'is_reversible'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | complain "Route is not reversible"; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 11 | return $self->{'path'}; | |||||||||
| 79 | |||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 24 | ▶ | sub construct_uri { | ||||||||
|
Subroutine
construct_uri: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 83 | |||||||||||
| 84 | 24 | my $self = shift; | |||||||||
| 85 | 24 | my %args = @_; | |||||||||
| 86 | |||||||||||
| 87 | 24 | ▶ | if ( ! $self->{'is_reversible'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | complain "Route is not reversible"; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 24 | ▶ | if ( defined $self->{'scheme'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 2 | $args{'scheme'} = $self->{'scheme'}; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 24 | ▶ | if ( defined $self->{'host'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 4 | $args{'host'} = $self->{'host'}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 24 | $args{'path'} = $self->{'path'}; | |||||||||
| 100 | |||||||||||
| 101 | 24 | my $uri = eval { $self->_construct_uri( %args ) }; | |||||||||
| 102 | |||||||||||
| 103 | 24 | ▶ | if ( $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 1 | complain $@; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 23 | return $uri; | |||||||||
| 108 | |||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 25 | ▶ | sub match { | ||||||||
|
Subroutine
match: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||
| 112 | |||||||||||
| 113 | 25 | my $self = shift; | |||||||||
| 114 | 25 | my $context = shift; | |||||||||
| 115 | |||||||||||
| 116 | 25 | ▶ | my $request = $context->request or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | |||||||||||
| 118 | 23 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 119 | |||||||||||
| 120 | 25 | ▶ | if ( defined $self->{'method'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 4 | ▶ | return undef if $request->method ne $self->{'method'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 22 | ▶ | if ( defined $self->{'scheme'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 4 | ▶ | return undef if $request->scheme ne $self->{'scheme'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 19 | ▶ | if ( defined $self->{'host'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 4 | ▶ | return undef if $request->host ne $self->{'host'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 16 | ▶ | return undef if $request->path ne $self->{'path'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | 6 | return GX::Route::Match->new( action => $self->{'action'} ); | |||||||||
| 135 | |||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | 23 | 1; | |||||||||
| 140 | |||||||||||
| 141 | __END__ | ||||||||||