File Coverage

lib/PEF/Front/Config.pm
Criterion Covered Total %
statement 54 138 39.1
branch 7 30 23.3
condition 2 11 18.1
subroutine 9 62 14.5
pod 0 54 0.0
total 72 295 24.4


line stmt bran cond sub pod time code
1             package PEF::Front::Config;
2 1     1   9 use warnings;
  1         4  
  1         50  
3 1     1   9 use strict;
  1         3  
  1         36  
4 1     1   7 use FindBin;
  1         3  
  1         76  
5 1     1   10 use File::Basename;
  1         4  
  1         101  
6 1     1   12 use feature 'state';
  1         3  
  1         650  
7             our $project_dir;
8             our $app_conf_dir;
9             our $app_namespace;
10              
11             sub normalize_path {
12 1     1 0 3 my $path = $_[0];
13 1 50       5 if (not $path) {
    50          
14 0         0 return '.';
15             } elsif (substr($path, -1, 1) ne '/') {
16 0         0 return $path;
17             } else {
18 1         3 return substr($path, 0, -1);
19             }
20             }
21              
22             my @std_const_params = qw{
23             cfg_upload_dir
24             cfg_captcha_db
25             cfg_captcha_font
26             cfg_captcha_image_init
27             cfg_captcha_image_class
28             cfg_captcha_secret
29             cfg_captcha_symbols
30             cfg_captcha_expire_sec
31             cfg_model_dir
32             cfg_cache_file
33             cfg_cache_size
34             cfg_cache_method_expire
35             cfg_cookie_unset_negative_expire
36             cfg_www_static_dir
37             cfg_www_static_captchas_dir
38             cfg_www_static_captchas_path
39             cfg_in_filter_dir
40             cfg_out_filter_dir
41             cfg_db_user
42             cfg_db_password
43             cfg_db_name
44             cfg_db_reconnect_trys
45             cfg_model_local_dir
46             cfg_project_dir
47             cfg_app_namespace
48             cfg_default_lang
49             cfg_url_contains_lang
50             cfg_url_only_camel_case
51             cfg_template_dir_contains_lang
52             cfg_no_multilang_support
53             cfg_location_error
54             cfg_template_cache
55             cfg_no_nls
56             cfg_handle_static
57             cfg_log_level_info
58             cfg_log_level_error
59             cfg_log_level_debug
60             cfg_session_db_file
61             cfg_session_ttl
62             cfg_session_request_field
63             cfg_oauth_connect_timeout
64             cfg_unknown_msgid_db
65             cfg_collect_unknown_msgid
66             cfg_model_rules_reload
67             };
68              
69             my @std_var_params = qw{
70             cfg_context_post_hook
71             cfg_template_dir
72             cfg_model_rpc
73             cfg_oauth_client_id
74             cfg_oauth_client_secret
75             cfg_oauth_scopes
76             cfg_logger
77             cfg_parse_extra_params
78             };
79              
80             our %config_export;
81              
82             sub import {
83 1     1   43 my ($modname) = grep {/AppFrontConfig\.pm$/} keys %INC;
  72         108  
84 1         4 die "no config" if 0 && !$modname;
85 1 50       4 $modname = 'fakemodule::' if !$modname;
86 1   50     51 (undef, $app_conf_dir, undef) = fileparse($INC{$modname} || '', ".pm");
87 1         4 $app_conf_dir = normalize_path($app_conf_dir);
88 1         3 $modname =~ s|\.pm||;
89 1         2 $modname =~ s|/|::|g;
90 1         5 ($app_namespace = $modname) =~ s/::[^:]*$/::/;
91 1         2 my $mp = __PACKAGE__;
92 1         3 my $cp = caller;
93 1     1   10 no strict 'refs';
  1         9  
  1         2431  
94 1         4 my @pa = (\@std_const_params, \@std_var_params);
95              
96 1         9 for (my $i = 0; $i < @pa; ++$i) {
97 2         4 for my $method (@{$pa[$i]}) {
  2         6  
98 52         334 (my $bmn = $method) =~ s/^cfg_//;
99 52   33     332 my $cref = "$modname"->can($method) || *{$mp . "::std_$bmn"};
100 52         88 *{$mp . "::$method"} = $cref;
  52         196  
101 52         89 *{$cp . "::$method"} = *{$mp . "::$method"};
  52         212  
  52         117  
102 52 50       208 *{$modname . "::$method"} = $cref if not "$modname"->can($method);
  52         146  
103 52 100       205 $config_export{$method} = $cref if $i == 0;
104             }
105             }
106 1         2 my $exports = \@{$modname . "::EXPORT"};
  1         5  
107 1         3 for my $e (@$exports) {
108 0 0       0 if ((my $cref = "$modname"->can($e))) {
109 0         0 *{$cp . "::$e"} = $cref;
  0         0  
110 0         0 $config_export{$e} = $cref;
111             }
112             }
113 1 50       6 if ("$modname"->can("project_dir")) {
114 0         0 $project_dir = normalize_path("$modname"->project_dir);
115             } else {
116 1         3 my $lpath = $FindBin::Bin;
117 1         5 $lpath =~ s'(/app$|/conf$|/bin$)'';
118 1         39 $project_dir = $lpath;
119             }
120             }
121              
122 0     0 0 0 sub std_project_dir {$project_dir}
123 0     0 0 0 sub std_no_nls {0}
124 0     0 0 0 sub std_unknown_msgid_db {cfg_project_dir() . "/var/cache/unknown-msgid.db"}
125 0     0 0 0 sub std_collect_unknown_msgid {0}
126 0     0 0 0 sub std_template_cache {cfg_project_dir() . "/var/tt_cache"}
127 0     0 0 0 sub std_location_error {"/appError?msgid=Internal\%20Error"}
128 0     0 0 0 sub std_db_reconnect_trys {30}
129 0     0 0 0 sub std_no_multilang_support {1}
130 0     0 0 0 sub std_default_lang {'en'}
131 0     0 0 0 sub std_url_contains_lang {0}
132 0     0 0 0 sub std_url_only_camel_case {1}
133 0     0 0 0 sub std_template_dir_contains_lang {0}
134 0     0 0 0 sub std_handle_static {0}
135 4     4 0 16 sub std_app_namespace {$app_namespace}
136 0     0 0   sub std_in_filter_dir {"$app_conf_dir/InFilter"}
137 0     0 0   sub std_model_local_dir {"$app_conf_dir/Local"}
138 0     0 0   sub std_out_filter_dir {"$app_conf_dir/OutFilter"}
139 0     0 0   sub std_upload_dir {cfg_project_dir() . "/var/upload"}
140 0     0 0   sub std_captcha_db {cfg_project_dir() . "/var/captcha-db"}
141 0     0 0   sub std_captcha_font {"giant"}
142 0     0 0   sub std_captcha_image_init {{}}
143 0     0 0   sub std_captcha_image_class {"PEF::Front::SecureCaptcha"}
144 0     0 0   sub std_captcha_secret {$app_namespace}
145 0     0 0   sub std_captcha_expire_sec {300}
146 0     0 0   sub std_cache_file {cfg_project_dir() . "/var/cache/shared.cache"}
147 0     0 0   sub std_cache_size {"8m"}
148 0     0 0   sub std_cache_method_expire {60}
149 0     0 0   sub std_model_rules_reload {0}
150 0     0 0   sub std_model_dir {cfg_project_dir() . "/model"}
151 0     0 0   sub std_www_static_dir {cfg_project_dir() . "/www-static"}
152 0     0 0   sub std_www_static_captchas_dir {cfg_project_dir() . "/www-static/captchas"}
153 0     0 0   sub std_db_user {"pef"}
154 0     0 0   sub std_db_password {"pef-pass"}
155 0     0 0   sub std_db_name {"pef"}
156 0     0 0   sub std_log_level_info {1}
157 0     0 0   sub std_log_level_error {1}
158 0     0 0   sub std_log_level_debug {0}
159 0     0 0   sub std_session_db_file {cfg_project_dir() . "/var/cache/session.db"}
160 0     0 0   sub std_session_ttl {86400 * 30}
161 0     0 0   sub std_session_request_field {'auth'}
162 0     0 0   sub std_cookie_unset_negative_expire {-3600}
163 0     0 0   sub std_oauth_connect_timeout {15}
164              
165             sub std_captcha_symbols {
166 0     0 0   state $symbols = ["0" .. "9", split //, "abCdEFgHiJKLMNOPqRSTUVWXyZ"];
167 0           $symbols;
168             }
169              
170             sub std_www_static_captchas_path {
171 0 0   0 0   if (substr(cfg_www_static_captchas_dir(), 0, length(cfg_www_static_dir())) eq cfg_www_static_dir()) {
172             # removes cfg_www_static_dir() from cfg_www_static_captchas_dir() and adds '/'
173 0           substr(cfg_www_static_captchas_dir(), length(cfg_www_static_dir())) . '/';
174             } else {
175             #must be overriden by user
176 0           '/captchas/';
177             }
178             }
179              
180       0 0   sub std_context_post_hook { }
181              
182             sub std_template_dir {
183 0 0   0 0   cfg_template_dir_contains_lang()
184             ? cfg_project_dir() . "/templates/$_[1]"
185             : cfg_project_dir() . "/templates";
186             }
187              
188             sub std_model_rpc {
189 0     0 0   PEF::Front::Model::chain_links($_[0]);
190             }
191              
192             sub std_oauth_client_id {
193 0     0 0   state $ids = {
194             yandex => 'anonymous',
195             google => 'anonymous',
196             facebook => 'anonymous',
197             v_kontakte => 'anonymous',
198             git_hub => 'anonymous',
199             };
200 0           $ids->{$_[0]};
201             }
202              
203             sub std_oauth_client_secret {
204 0     0 0   state $secrets = {
205             yandex => 'anonymous_secret',
206             google => 'anonymous_secret',
207             facebook => 'anonymous_secret',
208             v_kontakte => 'anonymous_secret',
209             git_hub => 'anonymous_secret',
210             };
211 0           $secrets->{$_[0]};
212             }
213              
214             sub std_oauth_scopes {
215 0     0 0   state $scopes = {
216             yandex => {user_info => undef},
217             v_kontakte => {user_info => undef},
218             git_hub => {user_info => 'user'},
219             google => {
220             email => 'https://www.googleapis.com/auth/userinfo.email',
221             share => 'https://www.googleapis.com/auth/plus.stream.write',
222             user_info => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
223             },
224             facebook => {
225             email => 'email',
226             share => 'publish_actions',
227             user_info => 'email',
228             offline => 'offline_access'
229             },
230             v_kontakte => {user_info => undef},
231             git_hub => {user_info => 'user'},
232             msn => {
233             email => 'wl.emails',
234             offline => 'wl.offline_access',
235             share => 'wl.share',
236             user_info => 'wl.basic wl.emails',
237             },
238             paypal => {
239             email => 'email',
240             user_info => 'email profile phone address',
241             all => 'email openid profile phone address'
242             . 'https://uri.paypal.com/services/paypalattributes'
243             . ' https://uri.paypal.com/services/expresscheckout',
244             },
245             linked_in => {
246             email => 'r_emailaddress',
247             share => 'rw_nus',
248             user_info => 'r_emailaddress r_fullprofile',
249             }
250              
251             };
252 0           $scopes->{$_[0]};
253             }
254              
255             sub std_logger {
256 0     0 0   my $request = $_[0];
257             $request->{env}{'psgix.logger'}
258 0     0     || sub {$request->{env}{'psgi.errors'}->print($_[0]->{message});}
259 0 0         }
260              
261             sub std_parse_extra_params {
262 0     0 0   my ($src, $params, $form) = @_;
263              
264 0 0 0       if (($src eq 'get' || $src eq 'app') && $params ne '') {
      0        
265 0           my @params = split /\//, $params;
266 0           my $i = 1;
267 0           for my $pv (@params) {
268 0           my ($p, $v) = split /-/, $pv, 2;
269 0 0         if (!defined($v)) {
270 0           $v = $p;
271 0           $p = 'cookie';
272 0 0         if (exists $form->{$p}) {
273 0           $p = "get_param_$i";
274 0           ++$i;
275             }
276             }
277 0           $form->{$p} = $v;
278             }
279             }
280             }
281              
282             sub cfg {
283 0     0 0   my $key = $_[0];
284 0           my $cfg_key = "cfg_" . $key;
285 0 0         if (exists $config_export{$cfg_key}) {
    0          
286 0           $config_export{$cfg_key}->();
287             } elsif (exists $config_export{$key}) {
288 0           $config_export{$key}->();
289             } else {
290 0           warn "Unknown config key: $key";
291 0           undef;
292             }
293             }
294              
295             1;
296              
297             __END__