blib/lib/ASP4/Config.pm

filter: Statementstmt 89.4% 76 / 85 Branchbran 50.0% 12 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 3 totaltotal 76.5% 98 / 128 CC 20 SCAR 27.5
CC
20
Cov
79%
init_server_root
25.1
load_class
2.0
new
2.0
CRAP 15.6
« blib/lib/ASP4/ConfigFinder.pm ↑ Summary blib/lib/ASP4/ConfigNode/Web.pm »
1  
2 package ASP4::Config;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use base 'ASP4::ConfigNode';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 sub new
11 15 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
12 15 my ($class, $ref, $root) = @_;
13
14 15 my $s = $class->SUPER::new( $ref );
15
16 15 $s->init_server_root( $root );
17
18 15 $s->_init_inc();
19
20 15 my $vars = $s->system->env_vars;
21 15 foreach my $var ( keys %$vars )
22 {
23 30 $ENV{$var} = $vars->{$var};
24 }# end foreach()
25
26 15 map { $s->load_class( $_ ) } $s->system->load_modules;
27
28 15 return $s;
29 }# end new()
30  
31  
32 sub _init_inc
33 5160 ▶{
Subroutine _init_inc: called CC=1 SCAR=0.0
34 5160 my $s = shift;
35
36 5160 my %saw = map { $_ => 1 } @INC;
37 5160 my $web = $s->web;
38 5160 push @INC, grep { ! $saw{$_}++ } ( $s->system->libs, $web->handler_root, $web->page_cache_root );
39 }# end _init_inc()
40  
41  
42 sub init_server_root
43 15 ▶{
Subroutine init_server_root: called CC=18 SCAR=32.2
Pod undocumented
44 15 my ($s, $root) = @_;
45
46 15 ▶ my $project_root = (sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
47 15 my @parts = split /\//, $root;
48 15 pop(@parts);
49 15 join '/', @parts;
50 15 })->();
51 15 $s->{web}->{project_root} = $project_root;
52 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 15 foreach( @{ $s->{system}->{libs} } )
54 {
55 15 $_ =~ s/\@ServerRoot\@/$root/;
56 15 $_ =~ s/\@ProjectRoot\@/$project_root/;
57 }# end foreach()
58
59 15 my $settings = $s->{system}->{settings};
60 15 foreach( keys %$settings )
61 {
62 30 $settings->{$_} =~ s/\@ServerRoot\@/$root/;
63 30 $settings->{$_} =~ s/\@ProjectRoot\@/$project_root/;
64 }# end foreach()
65
66 15 foreach my $key (qw/ application handler www page_cache /)
67 {
68 60 $s->{web}->{"$key\_root"} =~ s/\@ServerRoot\@/$root/;
69 60 $s->{web}->{"$key\_root"} =~ s/\@ProjectRoot\@/$project_root/;
70 60 $s->{web}->{"$key\_root"} =~ s{\\\\}{\\}g;
71 }# end foreach()
72 15 $s->{web}->{project_root} = $project_root;
73
74 # Just in case we're dealing with a file-based db like SQLite:
75 15 foreach my $key (qw/ session main /)
76 {
77 30 $s->{data_connections}->{$key}->{dsn} =~ s/\@ServerRoot\@/$root/;
78 30 $s->{data_connections}->{$key}->{dsn} =~ s/\@ProjectRoot\@/$project_root/;
79 30 $s->{data_connections}->{$key}->{dsn} =~ s{\\\\}{\\}g;
80 }# end foreach()
81
82 # Make sure that $s->page_cache_root exists:
83 15 ▶ unless( $s->{web}{page_cache_root} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->{"web"}{"page_cache_root"}) 15 0 15
84 {
85 15 ▶ if( $^O =~ m{win32}i )
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /win32/i) { } 0 15 15
86 {
87 0 ▶ my $temp_root = $ENV{TMP} || $ENV{TEMP};
Condition0%0/3
$ENV{'TMP'} || $ENV{'TEMP'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'TMP'} || $ENV{'TEMP'}
$ENV{'TMP'} $ENV{'TEMP'}
Truth table0%0/3
A || B
A= $ENV{'TMP'}B= $ENV{'TEMP'}
ABresult
1X1
011
000
88 0 $s->{web}{page_cache_root} = "$temp_root\\PAGE_CACHE";
89 }
90 else
91 {
92 15 $s->{web}{page_cache_root} = "/tmp/PAGE_CACHE";
93 }# end if()
94 }# end unless()
95
96 15 ▶ unless( -d $s->{web}{page_cache_root} )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $s->{"web"}{"page_cache_root"}) 1 14 15
97 {
98 1 my @parts = split /[\/\\]/, $s->{web}{page_cache_root};
99 1 ▶ my $root = $^O =~ m{win32}i ? '' : "/";
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /win32/i ? : 0 1 1
100 1 ▶ while( my $next = shift(@parts) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @parts) 0 1 1
101 {
102 0 $root .= "$next/";
103 0 ▶ mkdir($root) unless -d $root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root 0 0 0
104 0 ▶ die "Cannot create path to '@{[ $s->web->page_cache_root ]}' - stopped at '$root': $!"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root 0 0 0
105 unless -d $root;
106 0 chmod(0777, $root);
107 }# end while()
108 }# end unless()
109
110 15 (my $compiled_root = $s->{web}->{page_cache_root} . '/' . $s->{web}->{application_name}) =~ s/::/\//g;
111 15 ▶ my $folder = $^O =~ m{win32}i ? '' : "/";
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /win32/i ? : 0 15 15
112 15 foreach my $part ( grep { $_ } split /[\/\\]/, $compiled_root )
113 {
114 45 $folder .= "$part/";
115 45 ▶ unless( -d $folder )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $folder) 2 43 45
116 {
117 2 mkdir($folder);
118 2 chmod(0777, $folder)
119 }# end unless()
120 45 ▶ confess "Folder '$folder' does not exist and cannot be created"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $folder 0 45 45
121 unless -d $folder;
122 }# end foreach()
123
124 15 ▶ confess "Folder '$folder' exists but cannot be written to"
Branch: 1 branch
BranchTrueFalseTotal
unless -w $folder 0 15 15
125 unless -w $folder;
126 }# end init_server_root()
127  
128  
129 sub load_class
130 28827 ▶{
Subroutine load_class: called CC=2 SCAR=7.0
Pod undocumented
131 28827 my ($s, $class) = @_;
132
133 28827 (my $file = "$class.pm") =~ s/::/\//g;
134 28827 ▶ eval { require $file; }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file } } or confess("Cannot load ${class}: $@") 28827 0 28827
135 or confess "Cannot load $class: $@";
136 }# end load_class()
137  
138  
139 sub DESTROY
140 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
141 0 my $s = shift;
142 0 undef(%$s);
143 }
144  
145  
146 15 1;# return true:
147  
148 =pod
149  
150 =head1 NAME
151  
152 ASP4::Config - Central configuration for ASP4
153  
154 =head1 SYNOPSIS
155  
156 # Settings:
157 $Config->system->settings->some_setting;
158 $Config->system->settings->another_setting;
159
160 # Error-handling:
161 $Config->errors->error_handler;
162 $Config->errors->mail_errors_to;
163 $Config->errors->mail_errors_from;
164 $Config->errors->smtp_server;
165
166 # Web:
167 $Config->web->application_name;
168 $Config->web->application_root;
169 $Config->web->project_root;
170 $Config->web->www_root;
171 $Config->web->handler_root;
172 $Config->web->media_manager_upload_root;
173 $Config->web->page_cache_root;
174
175 # Data Connections:
176 foreach my $conn ( map { $Config->data_connections->$_ } qw/ session application main / )
177 {
178 my $dbh = DBI->connect(
179 $conn->dsn,
180 $conn->username,
181 $conn->password
182 );
183 }# end foreach()
184  
185 =head1 JSON Config File
186  
187 ASP4::ASP keeps all of its configuration inside of C</conf/asp4-config.json>
188  
189 Here is an example:
190  
191 {
192 "system": {
193 "post_processors": [
194
195 ],
196 "libs": [
197 "@ServerRoot@/lib",
198 "@ProjectRoot@/lib"
199 ],
200 "load_modules": [
201 "DBI",
202 "DBD::SQLite"
203 ],
204 "env_vars": {
205 "myvar": "Some-Value",
206 "another_var": "Another Value"
207 },
208 "settings": {
209 "foo": "bar",
210 "baz": "bux"
211 }
212 },
213 "errors": {
214 "error_handler": "ASP4::ErrorHandler",
215 "mail_errors_to": "you@yours.com",
216 "mail_errors_from": "root@localhost",
217 "smtp_server": "localhost"
218 },
219 "web": {
220 "application_name": "DefaultApp",
221 "application_root": "@ServerRoot@",
222 "www_root": "@ServerRoot@/htdocs",
223 "handler_root": "@ServerRoot@/handlers",
224 "page_cache_root": "/tmp/PAGE_CACHE",
225 "handler_resolver": "ASP4::HandlerResolver",
226 "handler_runner": "ASP4::HandlerRunner",
227 "filter_resolver": "ASP4::FilterResolver",
228 "request_filters": [
229 {
230 "uri_match": "^/.*",
231 "class": "My::Filter"
232 }
233 ],
234 "disable_persistence": [
235 {
236 "uri_match": "^/handlers/dev\\.speed",
237 "disable_session": true
238 },
239 {
240 "uri_match": "^/index\\.asp",
241 "disable_session": true
242 }
243 ]
244 },
245 "data_connections": {
246 "session": {
247 "manager": "ASP4::SessionStateManager",
248 "cookie_name": "session-id",
249 "cookie_domain": ".mysite.com",
250 "session_timeout": 30,
251 "dsn": "DBI:SQLite:dbname=/tmp/db_asp4",
252 "username": "",
253 "password": ""
254 },
255 "main": {
256 "dsn": "DBI:SQLite:dbname=/tmp/db_asp4",
257 "username": "",
258 "password": ""
259 }
260 }
261 }
262  
263 =head1 BUGS
264  
265 It's possible that some bugs have found their way into this release.
266  
267 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
268  
269 =head1 HOMEPAGE
270  
271 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
272 of ASP4 in action.
273  
274 =head1 AUTHOR
275  
276 John Drago <jdrago_999@yahoo.com>
277  
278 =head1 COPYRIGHT
279  
280 Copyright 2008 John Drago. All rights reserved.
281  
282 =head1 LICENSE
283  
284 This software is Free software and is licensed under the same terms as perl itself.
285  
286 =cut
287  
288  
« blib/lib/ASP4/ConfigFinder.pm ↑ Summary blib/lib/ASP4/ConfigNode/Web.pm »