| 1 | |||||||||||
| 2 | package ASP4::Config; | ||||||||||
| 3 | |||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 9 | ▶ | use warnings 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use Carp 'confess'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use base 'ASP4::ConfigNode'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | sub new | ||||||||||
| 11 | { | ||||||||||
| 12 | 3 | ▶ | my ($class, $ref, $root) = @_; | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | my $s = $class->SUPER::new( $ref ); | |||||||||
| 15 | |||||||||||
| 16 | 3 | $s->init_server_root( $root ); | |||||||||
| 17 | |||||||||||
| 18 | 3 | $s->_init_inc(); | |||||||||
| 19 | |||||||||||
| 20 | 3 | my $vars = $s->system->env_vars; | |||||||||
| 21 | 3 | foreach my $var ( keys %$vars ) | |||||||||
| 22 | { | ||||||||||
| 23 | 6 | $ENV{$var} = $vars->{$var}; | |||||||||
| 24 | }# end foreach() | ||||||||||
| 25 | |||||||||||
| 26 | 3 | map { $s->load_class( $_ ) } $s->system->load_modules; | |||||||||
| 27 | |||||||||||
| 28 | 3 | return $s; | |||||||||
| 29 | }# end new() | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | sub _init_inc | ||||||||||
| 33 | { | ||||||||||
| 34 | 3 | ▶ | my $s = shift; | ||||||||
|
Subroutine
_init_inc: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 3 | my %saw = map { $_ => 1 } @INC; | |||||||||
| 37 | 3 | push @INC, grep { ! $saw{$_}++ } ( $s->system->libs, $s->web->handler_root, $s->web->page_cache_root ); | |||||||||
| 38 | }# end _init_inc() | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | sub init_server_root | ||||||||||
| 42 | { | ||||||||||
| 43 | 3 | ▶ | my ($s, $root) = @_; | ||||||||
|
Subroutine
init_server_root: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||
| 44 | |||||||||||
| 45 | 9 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 3 | foreach( @{ $s->{system}->{libs} } ) | |||||||||
| 47 | { | ||||||||||
| 48 | 3 | $_ =~ s/\@ServerRoot\@/$root/; | |||||||||
| 49 | }# end foreach() | ||||||||||
| 50 | |||||||||||
| 51 | 3 | my $settings = $s->{system}->{settings}; | |||||||||
| 52 | 3 | foreach( keys %$settings ) | |||||||||
| 53 | { | ||||||||||
| 54 | 6 | $settings->{$_} =~ s/\@ServerRoot\@/$root/; | |||||||||
| 55 | }# end foreach() | ||||||||||
| 56 | |||||||||||
| 57 | 3 | foreach my $key (qw/ application handler www page_cache /) | |||||||||
| 58 | { | ||||||||||
| 59 | 12 | $s->{web}->{"$key\_root"} =~ s/\@ServerRoot\@/$root/; | |||||||||
| 60 | 12 | $s->{web}->{"$key\_root"} =~ s{\\\\}{\\}g; | |||||||||
| 61 | }# end foreach() | ||||||||||
| 62 | |||||||||||
| 63 | mkdir( $s->{web}->{page_cache_root}, 0777 ) | ||||||||||
| 64 | 3 | ▶ | unless -d $s->{web}->{page_cache_root}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 3 | chmod( 0777, $s->{web}->{page_cache_root} ); | |||||||||
| 66 | confess "Folder '$s->{web}->{page_cache_root}' does not exist and cannot be created" | ||||||||||
| 67 | 3 | ▶ | unless -d $s->{web}->{page_cache_root}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 3 | (my $compiled_root = $s->{web}->{page_cache_root} . '/' . $s->{web}->{application_name}) =~ s/::/\//g; | |||||||||
| 70 | 3 | my $folder = ""; | |||||||||
| 71 | 3 | foreach my $part ( grep { $_ } split /\//, $compiled_root ) | |||||||||
| 72 | { | ||||||||||
| 73 | 9 | $folder .= "/$part"; | |||||||||
| 74 | 9 | ▶ | unless( -d $folder ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | { | ||||||||||
| 76 | 1 | mkdir($folder); | |||||||||
| 77 | 1 | chmod(0777, $folder) | |||||||||
| 78 | }# end unless() | ||||||||||
| 79 | 9 | ▶ | confess "Folder '$folder' does not exist and cannot be created" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | unless -d $folder; | ||||||||||
| 81 | }# end foreach() | ||||||||||
| 82 | |||||||||||
| 83 | 3 | ▶ | confess "Folder '$folder' exists but cannot be written to" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | unless -w $folder; | ||||||||||
| 85 | }# end init_server_root() | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | sub load_class | ||||||||||
| 89 | { | ||||||||||
| 90 | 6 | ▶ | my ($s, $class) = @_; | ||||||||
|
Subroutine
load_class: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 91 | |||||||||||
| 92 | 6 | (my $file = "$class.pm") =~ s/::/\//g; | |||||||||
| 93 | 6 | ▶ | eval { require $file } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | or confess "Cannot load $class: $@"; | ||||||||||
| 95 | }# end load_class() | ||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | sub DESTROY | ||||||||||
| 99 | { | ||||||||||
| 100 | 0 | ▶ | my $s = shift; | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||
| 101 | 0 | undef(%$s); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | 9 | 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</conf/asp4-config.json> | ||||||||||
| 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||||||||||
| 225 | |||||||||||
| 226 | =head1 HOMEPAGE | ||||||||||
| 227 | |||||||||||
| 228 | Please visit the ASP4 homepage at L<http://www.devstack.com/> to see examples | ||||||||||
| 229 | of ASP4 in action. | ||||||||||
| 230 | |||||||||||
| 231 | =head1 AUTHOR | ||||||||||
| 232 | |||||||||||
| 233 | John Drago <jdrago_999@yahoo.com> | ||||||||||
| 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 | |||||||||||