blib/lib/Mojolicious/Plugin/Facets.pm

filter: Statementstmt 100% 93 / 93 Branchbran 84.6% 22 / 26 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 94.9% 131 / 138 CC 18 SCAR 15.3
CC
18
Cov
96%
_detect_facet
9.0
_add_facet
5.1
__ANON__
4.0
CRAP 4.6
↑ Summary
1 package Mojolicious::Plugin::Facets;
2  
3 6 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Mojolicious::Routes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Mojolicious::Static;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mojolicious::Sessions;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Mojo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Mojo::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use constant DEBUG => $ENV{DEBUG_FACETS};
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = "0.07";
12  
13  
14 6 my @facets;
15  
16 6 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
17 6 my ($self, $app, $config) = @_;
18  
19 6 $app->hook(around_dispatch => \&_detect_facet);
20  
21 6 $app->helper(facet_do => \&_facet_do);
22  
23 3 ▶ $app->helper(has_facet => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
24 3 my ($c, $name) = @_;
25 3 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
26 3 for (@facets) {
27 5 ▶ return 1 if $_->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"name"} eq $name 2 3 5
28 }
29 1 return;
30 6 });
31  
32 1 ▶ $app->helper(add_facet => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 shift;
34 1 $self->_add_facet($app, @_);
35 6 });
36  
37 6 foreach my $facet_name (keys %$config) {
38 6 $self->_add_facet($app, $facet_name, $config->{$facet_name});
39 }
40 }
41  
42 7 ▶sub _add_facet {
Subroutine _add_facet: called CC=5 SCAR=16.2
43 7 my ($self, $app, $facet_name, $facet_config) = @_;
44  
45 7 ▶ die "Missing 'setup' key on facet '$facet_name' config." unless $facet_config->{setup};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_config->{"setup"} 0 7 7
46 die "Missing 'host' or 'path' key on facet '$facet_name' config."
47 7 ▶ unless $facet_config->{host} || $facet_config->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_config->{"host"} or $facet_config->{"path"} 0 7 7
Condition66%2/3
$facet_config->{"host"} or $facet_config->{"path"}
l!l&&r!l&&!r
430
MC/DC0%0/2
$facet_config->{"host"} or $facet_config->{"path"}
$facet_config->{"host"} $facet_config->{"path"}
Truth table66%2/3
A or B
A= $facet_config->{"host"}B= $facet_config->{"path"}
ABresult
1X1
011
000
48  
49 my $facet = {
50 name => $facet_name,
51 host => $facet_config->{host},
52 7 routes => Mojolicious::Routes->new(namespaces => [@{ $app->routes->namespaces }]),
53 static => Mojolicious::Static->new,
54 sessions => Mojolicious::Sessions->new,
55 7 ▶ renderer_paths => [@{ $app->renderer->paths }],
Branch: 1 branch
BranchTrueFalseTotal
$facet_config->{'path'} ? : 3 4 7
56 renderer_cache => Mojo::Cache->new,
57 7 $facet_config->{path} ? ( path => Mojo::Path->new($facet_config->{path})->leading_slash(1)->trailing_slash(0) ) : (),
58 };
59  
60 # localize
61 7 local $app->{routes} = $facet->{routes};
62 7 local $app->{static} = $facet->{static};
63 7 local $app->{sessions} = $facet->{sessions};
64 7 local $app->renderer->{paths} = $facet->{renderer_paths};
65 7 local $app->renderer->{cache} = $facet->{renderer_cache};
66  
67 # setup
68 7 $facet_config->{setup}->($app);
69  
70 # store
71 7 push @facets, $facet;
72 }
73  
74  
75 23 ▶sub _detect_facet {
Subroutine _detect_facet: called CC=9 SCAR=22.0
76 23 my ($next, $c) = @_;
77  
78 # detect facet
79 23 my $active_facet;
80 23 my $req_host = $c->req->headers->host;
81 23 $req_host =~ s/:\d+$//;
82  
83 23 foreach my $facet (@facets) {
84  
85 24 my $match = 0;
86  
87 24 ▶ if ($facet->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($facet->{"host"}) 17 7 24
88 17 ▶ $match = 1 if $req_host eq $facet->{host};
Branch: 1 branch
BranchTrueFalseTotal
if $req_host eq $facet->{"host"} 7 10 17
89 }
90  
91 24 ▶ if ($facet->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($facet->{"path"}) 7 17 24
92  
93 7 ▶ if ($c->req->url->path->contains($facet->{path})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->url->path->contains($facet->{'path'})) { } 3 4 7
94 3 $match = 1;
95  
96 # rebase
97 3 my $path_length = scalar @{$facet->{path}};
98 3 my $base_path = $c->req->url->base->path->trailing_slash(1);
99 3 my $req_path = $c->req->url->path->leading_slash(0);
100  
101 3 ▶ while ($path_length--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_length--) 3 3 6
102 3 push @$base_path, shift @$req_path;
103 }
104 }
105 else {
106 4 $match = 0;
107 }
108 }
109  
110 24 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 10 14 24
111 10 $active_facet = $facet;
112 last
113 10 }
114 }
115  
116 # localize relevant data and continue dispatch chain
117 23 ▶ if ($active_facet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_facet) { } 10 13 23
118 10 DEBUG and $c->app->log->debug(qq/Dispatching facet "$active_facet->{name}"/);
119  
120 10 $c->stash->{'mojox.facet'} = $active_facet->{name};
121  
122 10 local $c->app->{routes} = $active_facet->{routes};
123 10 local $c->app->{static} = $active_facet->{static};
124 10 local $c->app->{sessions} = $active_facet->{sessions};
125 10 local $c->app->renderer->{paths} = $active_facet->{renderer_paths};
126 10 local $c->app->renderer->{cache} = $active_facet->{renderer_cache};
127 10 $next->();
128 }
129 else {
130 # no facet, continue dispatch
131 13 $next->();
132 }
133 }
134  
135  
136 1 ▶sub _facet_do {
Subroutine _facet_do: called CC=2 SCAR=6.9
137 1 my ($c, $facet_name, $code) = @_;
138  
139 1 my ($facet) = grep { $_->{name} eq $facet_name } @facets;
140 1 ▶ die "Facet '$facet_name' do not exist." unless $facet;
Branch: 1 branch
BranchTrueFalseTotal
unless $facet 0 1 1
141  
142 1 local $c->app->{routes} = $facet->{routes};
143 1 local $c->app->{static} = $facet->{static};
144 1 local $c->app->{sessions} = $facet->{sessions};
145 1 local $c->app->renderer->{paths} = $facet->{renderer_paths};
146 1 local $c->app->renderer->{cache} = $facet->{renderer_cache};
147 1 local $c->{stash} = {};
148 1 $code->($c);
149 }
150  
151  
152  
153  
154  
155 6 1;
156 __END__
↑ Summary