TraitFor/Component/ConfigPerSite.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 77.7% 14 / 18 Conditioncond 50.0% 9 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 80.1% 105 / 131 CC 17 SCAR 22.7
CC
17
Cov
85%
get_site_config
16.5
get_from_instance_cache
3.2
put_in_instance_cache
2.0
CRAP 9.6
↑ Summary TraitFor/Model/DBIC/ConfigPerSite.pm »
1 package Catalyst::TraitFor::Component::ConfigPerSite;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp qw(carp cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Catalyst::TraitFor::Component::ConfigPerSite - Extend Catalyst Components to share application accross sites
9  
10 =head1 DESCRIPTIONS
11  
12 This Role or Trait allows you to share an application between sites, clients, etc
13 with different configuration for templates and databases (and possibly other parts).
14  
15 Compose this role into your trait to extend a catalyst component such as a model or view
16  
17 =head1 SYNOPSIS
18  
19 in testblogapp.conf:
20  
21 name TestBlogApp
22 site_name TestBlog
23 default_view TT
24  
25 <Model::DB>
26 schema_class TestBlogApp::Schema
27 <connect_info>
28 dsn dbi:SQLite:dbname=t/test.db
29 user username
30 password password
31 </connect_info>
32 </Model::DB>
33  
34 <View::TT>
35 TEMPLATE_EXTENSION .tt
36 WRAPPER site-wrapper.tt
37 INCLUDE_PATH t/templates
38 </View::TT>
39  
40 <TraitFor::Component::ConfigPerSite>
41 <foo.bar>
42 <Model::DB>
43 schema_class TestBlogApp::Schema
44 <connect_info>
45 dsn dbi:SQLite:dbname=t/test2.db
46 user username
47 password password
48 </connect_info>
49 instance_cache_key foo_bar_model_db
50 </Model::DB>
51  
52 <View::TT>
53 TEMPLATE_EXTENSION .tt
54 WRAPPER site-wrapper.tt
55 INCLUDE_PATH t/more_templates
56 instance_cache_key foo_bar_view_tt
57 </View::TT>
58  
59 </foo.bar>
60 </TraitFor::Component::ConfigPerSite>
61  
62 =head1 VERSION
63  
64 0.12
65  
66 =cut
67  
68 5 our $VERSION = '0.12';
69  
70 5 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 5 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 5 my $site_config_cache = { };
75  
76 5 my $shared_config;
77  
78 5 has '_site_config' => ( is => 'ro' );
79  
80 =head1 METHODS
81  
82 =head2 get_site_config
83  
84 return (possibly cached) site-specific configuration based on host and path for this request
85  
86 my $site_config = $self->get_site_config($c);
87  
88 =cut
89  
90 6 ▶sub get_site_config {
Subroutine get_site_config: called CC=14 SCAR=28.0
Pod documented
91 6 my ($self, $c) = @_;
92  
93 6 ▶ $shared_config ||= $c->config->{'TraitFor::Component::ConfigPerSite'};
Condition66%2/3
$shared_config ||= $c->config->{"TraitFor::Component::ConfigPerSite"}
l!l&&r!l&&!r
330
MC/DC0%0/2
$shared_config ||= $c->config->{"TraitFor::Component::ConfigPerSite"}
$shared_config $c->config->{"TraitFor::Component::ConfigPerSite"}
Truth table66%2/3
A ||= B
A= $shared_configB= $c->config->{"TraitFor::Component::ConfigPerSite"}
ABresult
1X1
011
000
94  
95 # get configuration from host and/or path
96 6 my $req = $c->request;
97 6 my $host = $req->uri->host;
98 6 my $path = $req->uri->path;
99  
100 6 my $cache_key = $host.$path;
101 6 my $site_config = $site_config_cache->{$cache_key};
102  
103 6 ▶ if ( not defined $site_config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $site_config) 3 3 6
104 3 ▶ if (my $host_config = $shared_config->{$host} || $shared_config->{ALL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host_config = $shared_config->{$host} || $shared_config->{'ALL'}) { } 2 1 3
Condition66%2/3
$shared_config->{$host} || $shared_config->{'ALL'}
l!l&&r!l&&!r
201
MC/DC50%1/2
$shared_config->{$host} || $shared_config->{'ALL'}
$shared_config->{$host} $shared_config->{'ALL'}
Truth table66%2/3
A || B
A= $shared_config->{$host}B= $shared_config->{'ALL'}
ABresult
1X1
011
000
105 2 ▶ if (scalar keys %$host_config >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$host_config >= 1) { } 2 0 2
106 2 my @path_parts = split(/\/+/, $path);
107 2 ▶ while (my $last_path_part = pop(@path_parts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last_path_part = pop @path_parts) 2 2 4
108 2 my $match_path = join ('/',@path_parts,$last_path_part);
109 2 ▶ if ( $site_config = $host_config->{"/$match_path"} || $host_config->{"$match_path"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site_config = $$host_config{"/$match_path"} || $$host_config{"$match_path"}) 0 2 2
Condition33%1/3
$$host_config{"/$match_path"} || $$host_config{"$match_path"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$$host_config{"/$match_path"} || $$host_config{"$match_path"}
$$host_config{"/$match_path"} $$host_config{"$match_path"}
Truth table33%1/3
A || B
A= $$host_config{"/$match_path"}B= $$host_config{"$match_path"}
ABresult
1X1
011
000
110 0 last;
111 }
112 }
113 2 ▶ $site_config ||= $host_config->{ALL} || $host_config;
Condition33%2/6
$site_config ||= $host_config->{"ALL"} || $host_config
l!l&&r!l&&!r
020
$host_config->{'ALL'} || $host_config
l!l&&r!l&&!r
020
MC/DC0%0/3
$site_config ||= $host_config->{"ALL"} || $host_config
$site_config $host_config->{'ALL'} $host_config
Truth table0%0/4
A ||= B || C
A= $site_configB= $host_config->{'ALL'}C= $host_config
ABCresult
1XX1
01X1
0011
0000
114 } else {
115 0 ($site_config) = values %$host_config;
116 }
117 2 $site_config->{site_name} = "host:$host";
118  
119 # inherit top level config where not over-ridden
120 2 my $top_level_config = $c->config;
121 2 foreach my $key (keys %$top_level_config) {
122 22 ▶ unless (defined $site_config->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $site_config->{$key}) 17 5 22
123 17 $site_config->{$key} = $top_level_config->{$key};
124 }
125 }
126  
127 } else {
128 # if none found fall back to top level config for DBIC, and warn
129 1 $site_config = { site_name => 'top_level_fallback', %{$c->config} };
130 1 ▶ carp "falling back to top level config" if ($c->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
131 }
132  
133 3 $site_config_cache->{$cache_key} = $site_config;
134 }
135  
136 6 return $site_config;
137 }
138  
139 =head2 get_component_config
140  
141 return appropriate configuration for this component for this site
142  
143 my $config = $self->get_component_config;
144  
145 =cut
146  
147 6 ▶sub get_component_config {
Subroutine get_component_config: called CC=1 SCAR=0.0
Pod documented
148 6 my ($self, $c) = @_;
149  
150 6 my $component_name = $self->catalyst_component_name;
151 6 my $site_config = $self->get_site_config($c);
152 6 my $appname = $site_config->{name}.'::';
153 6 $component_name =~ s/$appname//;
154 6 my $component_config = $site_config->{$component_name};
155 6 $component_config->{site_name} = $site_config->{site_name};
156 6 return $component_config;
157 }
158  
159 =head2 get_from_instance_cache
160  
161 if (my $instance = $self->get_from_instance_cache($config)) {
162 return $instance;
163 }
164  
165 =cut
166  
167 5 our $instances = {};
168  
169 6 ▶sub get_from_instance_cache {
Subroutine get_from_instance_cache: called CC=3 SCAR=11.6
Pod documented
170 6 my ($self,$config) = @_;
171 6 my $instance_cache_key = $config->{instance_cache_key};
172 6 my $instance;
173 6 ▶ if ($instance_cache_key && $instances->{$instance_cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance_cache_key and $Catalyst::TraitFor::Component::ConfigPerSite::instances->{$instance_cache_key}) 0 6 6
Condition66%2/3
$instance_cache_key and $Catalyst::TraitFor::Component::ConfigPerSite::instances->{$instance_cache_key}
!ll&&!rl&&r
330
MC/DC0%0/2
$instance_cache_key and $Catalyst::TraitFor::Component::ConfigPerSite::instances->{$instance_cache_key}
$instance_cache_key $Catalyst::TraitFor::Component::ConfigPerSite::instances->{$instance_cache_key}
Truth table66%2/3
A and B
A= $instance_cache_keyB= $Catalyst::TraitFor::Component::ConfigPerSite::instances->{$instance_cache_key}
ABresult
0X0
100
111
174 0 $instance = $instances->{$instance_cache_key};
175 }
176 6 return $instance;
177 }
178  
179 =head2 put_in_instance_cache
180  
181 $self->put_in_instance_cache($config, $instance);
182  
183 =cut
184  
185 6 ▶sub put_in_instance_cache {
Subroutine put_in_instance_cache: called CC=2 SCAR=6.9
Pod documented
186 6 my ($self,$config, $instance) = @_;
187 6 my $instance_cache_key = $config->{instance_cache_key};
188 6 ▶ return undef unless ($instance_cache_key);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance_cache_key 3 3 6
189 3 $instances->{$instance_cache_key} = $instance;
190 3 return;
191 }
192  
193  
194 =head1 SEE ALSO
195  
196 Catalyst::Component::InstancePerContext
197  
198 Catalyst::TraitFor::View::TT::ConfigPerSite
199  
200 Moose::Role
201  
202 =head1 AUTHOR
203  
204 Aaron Trevena, E<lt>aaron@aarontrevena.co.ukE<gt>
205  
206 =head1 COPYRIGHT AND LICENSE
207  
208 Copyright (C) 2010-2013 by Aaron Trevena
209  
210 This library is free software; you can redistribute it and/or modify
211 it under the same terms as Perl itself, either Perl version 5.10.0 or,
212 at your option, any later version of Perl 5 you may have available.
213  
214  
215 =cut
216  
217 5 1;
↑ Summary TraitFor/Model/DBIC/ConfigPerSite.pm »