| 1 | package Catalyst::Action::SubDomain; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use base 'Catalyst::Action'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 13 | 0 | ▶ | return $self->check_subdomain_constraints($_[0]->request->uri->host) && $self->next::method(@_); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->check_subdomain_constraints($_[0]->request->uri->host) && $self->next::method(@_)
MC/DC0%0/2
$self->check_subdomain_constraints($_[0]->request->uri->host) && $self->next::method(@_)
$self->check_subdomain_constraints($_[0]->request->uri->host)
$self->next::method(@_)
Truth table0%0/3
A && B
A= $self->check_subdomain_constraints($_[0]->request->uri->host)B= $self->next::method(@_)
| |||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub check_subdomain_constraints { | ||||||||||||||||||||||||||
|
Subroutine
check_subdomain_constraints: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | my ( $self, $host ) = @_; | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | return !! scalar @{$self->_cached_domains->{$host}} if exists $self->_cached_domains->{$host}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | my @domains = reverse split(/\./, $host); | |||||||||||||||||||||||||||
| 20 | 0 | $self->_cached_domains->{$host} = \@domains; | |||||||||||||||||||||||||||
| 21 | 0 | ▶ | return 1 unless exists $self->attributes->{SubDomain}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | foreach my $cnf (grep { $_->[0]=~/^\d+$/ } map([split(/,/, $_, 2)], @{$self->attributes->{SubDomain}})) { | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ((scalar(@domains) >= $cnf->[0]?$domains[$cnf->[0] - 1]:'') !~ qr#$cnf->[1]#) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 24 | 0 | $self->_cached_domains->{$host} = []; | |||||||||||||||||||||||||||
| 25 | 0 | return undef; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 0 | return 1; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub domain { | ||||||||||||||||||||||||||
|
Subroutine
domain: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($self, $c, $level) = @_; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | return undef unless defined($c) && defined($level); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $c and defined $level
MC/DC0%0/2
defined $c and defined $level
defined $c
defined $level
Truth table0%0/3
A and B
A= defined $cB= defined $level
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return $self->number_of_domains($c) >= $level?$self->_cached_domains->{$c->request->uri->host}->[$level-1]:undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub number_of_domains { | ||||||||||||||||||||||||||
|
Subroutine
number_of_domains: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 39 | 0 | return scalar(@{$self->_cached_domains->{$c->request->uri->host}}); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub _cached_domains { | ||||||||||||||||||||||||||
|
Subroutine
_cached_domains: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 43 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | return $self->{'_cached_domains'} ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_cached_domains"} ||= {}
MC/DC0%0/1
$self->{"_cached_domains"} ||= {}
$self->{"_cached_domains"}
Truth table0%0/2
A
A= $self->{"_cached_domains"}
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | 1; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 NAME | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Catalyst::Action::SubDomain - Match action against names of subdomains | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 VERSION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Version 0.07 | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Match subdomain name | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub method : ActionClass('SubDomain') :SubDomain('level,regexp') { | ||||||||||||||||||||||||||||
| 62 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||
| 63 | .. | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Get number of domain levels and subdomain name at last level. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub method : ActionClass('SubDomain') { | ||||||||||||||||||||||||||||
| 69 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||
| 70 | my $max_level = $c->action->number_of_domains($c); | ||||||||||||||||||||||||||||
| 71 | my $subdomain = $c->action->domain($c, $max_level); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 EXAMPLES | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Root controller action for main site and subdomain with no more than 3 chars | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub default :Path('/') : ActionClass('SubDomain') : SubDomain('3,^\w{0,3}$') { | ||||||||||||||||||||||||||||
| 79 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Foo controller action for rest subdomains | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub index :Path('/') :ActionClass('SubDomain') :SubDomain('3,^\w{4,}$') { | ||||||||||||||||||||||||||||
| 85 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | This example shows that actions will be match only when 3-rd level domain exists and contains alpha-numerical chars (foo123.example.com). | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub index :Path('/') :Args(0) :ActionClass('SubDomain') :SubDomain('3,^\w+$') { | ||||||||||||||||||||||||||||
| 91 | my ( $self, $c ) = @_; | ||||||||||||||||||||||||||||
| 92 | $c->response->body('Matched My::App::Controller'); | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | foo123.example.com/test | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub test :Path('/test') :ActionClass('SubDomain') :SubDomain('3,^\w+$') { | ||||||||||||||||||||||||||||
| 98 | my ( $self, $c, @args ) = @_; | ||||||||||||||||||||||||||||
| 99 | $c->response->body(join('.', map($c->action->domain($c, $_), 1..$c->action->number_of_domains($c)))); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | You can specify more that one subdomain constraint. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub test :Local :ActionClass('SubDomain') :SubDomain('3,^\w+$') :SubDomain('2,^example$') { | ||||||||||||||||||||||||||||
| 105 | my ( $self, $c, @args ) = @_; | ||||||||||||||||||||||||||||
| 106 | my $name = $self->action->domain($c, 3); | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Note: When combining :ActionClass('SubDomain') with :Chained action you should access action a little bit different way | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | .. | ||||||||||||||||||||||||||||
| 112 | my $action = $c->action->isa('Catalyst::ActionChain')?$c->action->chain->[-1]:$c->action; | ||||||||||||||||||||||||||||
| 113 | my $name = $action->domain($c, 3); | ||||||||||||||||||||||||||||
| 114 | .. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 METHODS | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 domain($context, $level) | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Returns domain name of specified level. Works only for matched action. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 number_of_domains($context) | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Get number of domain levels | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 match | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | See L<Catalyst::Action/METHODS/match>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 check_subdomain_constraints | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Check subdomains constraints | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 _cached_domains | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Cached domains | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Egor Korablev, C<< <egor.korablev at gmail.com> >> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 147 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||