File Coverage

blib/lib/ASP4/Config.pm
Criterion Covered Total %
statement 57 59 96.6
branch 8 12 66.6
condition n/a
subroutine 9 10 90.0
pod 0 3 0.0
total 74 84 88.1


line stmt bran cond sub pod time code
1              
2             package ASP4::Config;
3              
4 9     9   714 use strict;
  9         10  
  9         241  
5 9     9   27 use warnings 'all';
  9         9  
  9         254  
6 9     9   31 use Carp 'confess';
  9         9  
  9         366  
7 9     9   33 use base 'ASP4::ConfigNode';
  9         12  
  9         3417  
8              
9              
10             sub new
11             {
12 9     9 0 22 my ($class, $ref, $root) = @_;
13            
14 9         139 my $s = $class->SUPER::new( $ref );
15            
16 9         44 $s->init_server_root( $root );
17            
18 9         110 $s->_init_inc();
19            
20 9         46 my $vars = $s->system->env_vars;
21 9         34 foreach my $var ( keys %$vars )
22             {
23 18         110 $ENV{$var} = $vars->{$var};
24             }# end foreach()
25            
26 9         128 map { $s->load_class( $_ ) } $s->system->load_modules;
  18         43034  
27            
28 9         85599 return $s;
29             }# end new()
30              
31              
32             sub _init_inc
33             {
34 5131     5131   4073 my $s = shift;
35            
36 5131         6414 my %saw = map { $_ => 1 } @INC;
  76938         94133  
37 5131         23765 push @INC, grep { ! $saw{$_}++ } ( $s->system->libs, $s->web->handler_root, $s->web->page_cache_root );
  15393         29131  
38             }# end _init_inc()
39              
40              
41             sub init_server_root
42             {
43 9     9 0 25 my ($s, $root) = @_;
44            
45 9     9   43 no warnings 'uninitialized';
  9         14  
  9         4790  
46 9         13 foreach( @{ $s->{system}->{libs} } )
  9         33  
47             {
48 9         75 $_ =~ s/\@ServerRoot\@/$root/;
49             }# end foreach()
50            
51 9         21 my $settings = $s->{system}->{settings};
52 9         26 foreach( keys %$settings )
53             {
54 18         35 $settings->{$_} =~ s/\@ServerRoot\@/$root/;
55             }# end foreach()
56            
57 9         27 foreach my $key (qw/ application handler www page_cache /)
58             {
59 36         113 $s->{web}->{"$key\_root"} =~ s/\@ServerRoot\@/$root/;
60 36         66 $s->{web}->{"$key\_root"} =~ s{\\\\}{\\}g;
61             }# end foreach()
62            
63             mkdir( $s->{web}->{page_cache_root}, 0777 )
64 9 100       313 unless -d $s->{web}->{page_cache_root};
65 9         385 chmod( 0777, $s->{web}->{page_cache_root} );
66             confess "Folder '$s->{web}->{page_cache_root}' does not exist and cannot be created"
67 9 50       114 unless -d $s->{web}->{page_cache_root};
68            
69 9         53 (my $compiled_root = $s->{web}->{page_cache_root} . '/' . $s->{web}->{application_name}) =~ s/::/\//g;
70 9         19 my $folder = "";
71 9         53 foreach my $part ( grep { $_ } split /\//, $compiled_root )
  36         61  
72             {
73 27         48 $folder .= "/$part";
74 27 100       236 unless( -d $folder )
75             {
76 1         29 mkdir($folder);
77 1         11 chmod(0777, $folder)
78             }# end unless()
79 27 50       205 confess "Folder '$folder' does not exist and cannot be created"
80             unless -d $folder;
81             }# end foreach()
82            
83 9 50       115 confess "Folder '$folder' exists but cannot be written to"
84             unless -w $folder;
85             }# end init_server_root()
86              
87              
88             sub load_class
89             {
90 33810     33810 0 27385 my ($s, $class) = @_;
91            
92 33810         84335 (my $file = "$class.pm") =~ s/::/\//g;
93 33810 50       28805 eval { require $file }
  33810         201972  
94             or confess "Cannot load $class: $@";
95             }# end load_class()
96              
97              
98             sub DESTROY
99             {
100 0     0     my $s = shift;
101 0           undef(%$s);
102             }
103              
104              
105             1;# return true:
106              
107             =pod
108              
109             =head1 NAME
110              
111             ASP4::Config - Central configuration for ASP4
112              
113             =head1 SYNOPSIS
114              
115             # Settings:
116             $Config->system->settings->some_setting;
117             $Config->system->settings->another_setting;
118            
119             # Error-handling:
120             $Config->errors->error_handler;
121             $Config->errors->mail_errors_to;
122             $Config->errors->mail_errors_from;
123             $Config->errors->smtp_server;
124            
125             # Web:
126             $Config->web->application_name;
127             $Config->web->application_root;
128             $Config->web->www_root;
129             $Config->web->handler_root;
130             $Config->web->media_manager_upload_root;
131             $Config->web->page_cache_root;
132            
133             # Data Connections:
134             foreach my $conn ( map { $Config->data_connections->$_ } qw/ session application main / )
135             {
136             my $dbh = DBI->connect(
137             $conn->dsn,
138             $conn->username,
139             $conn->password
140             );
141             }# end foreach()
142              
143             =head1 JSON Config File
144              
145             ASP4::ASP keeps all of its configuration inside of C
146              
147             Here is an example:
148              
149             {
150             "system": {
151             "post_processors": [
152            
153             ],
154             "libs": [
155             "@ServerRoot@/lib"
156             ],
157             "load_modules": [
158             "DBI",
159             "DBD::SQLite"
160             ],
161             "env_vars": {
162             "myvar": "Some-Value",
163             "another_var": "Another Value"
164             },
165             "settings": {
166             "foo": "bar",
167             "baz": "bux"
168             }
169             },
170             "errors": {
171             "error_handler": "ASP4::ErrorHandler",
172             "mail_errors_to": "you@yours.com",
173             "mail_errors_from": "root@localhost",
174             "smtp_server": "localhost"
175             },
176             "web": {
177             "application_name": "DefaultApp",
178             "application_root": "@ServerRoot@",
179             "www_root": "@ServerRoot@/htdocs",
180             "handler_root": "@ServerRoot@/handlers",
181             "page_cache_root": "/tmp/PAGE_CACHE",
182             "handler_resolver": "ASP4::HandlerResolver",
183             "handler_runner": "ASP4::HandlerRunner",
184             "filter_resolver": "ASP4::FilterResolver",
185             "request_filters": [
186             {
187             "uri_match": "^/.*",
188             "class": "My::Filter"
189             }
190             ],
191             "disable_persistence": [
192             {
193             "uri_match": "^/handlers/dev\\.speed",
194             "disable_session": true
195             },
196             {
197             "uri_match": "^/index\\.asp",
198             "disable_session": true
199             }
200             ]
201             },
202             "data_connections": {
203             "session": {
204             "manager": "ASP4::SessionStateManager",
205             "cookie_name": "session-id",
206             "cookie_domain": ".mysite.com",
207             "session_timeout": 30,
208             "dsn": "DBI:SQLite:dbname=/tmp/db_asp4",
209             "username": "",
210             "password": ""
211             },
212             "main": {
213             "dsn": "DBI:SQLite:dbname=/tmp/db_asp4",
214             "username": "",
215             "password": ""
216             }
217             }
218             }
219            
220             =head1 BUGS
221            
222             It's possible that some bugs have found their way into this release.
223            
224             Use RT L to submit bug reports.
225            
226             =head1 HOMEPAGE
227            
228             Please visit the ASP4 homepage at L to see examples
229             of ASP4 in action.
230              
231             =head1 AUTHOR
232              
233             John Drago
234              
235             =head1 COPYRIGHT
236              
237             Copyright 2008 John Drago. All rights reserved.
238              
239             =head1 LICENSE
240              
241             This software is Free software and is licensed under the same terms as perl itself.
242              
243             =cut
244