Silki/Config.pm

filter: Statementstmt 94.3% 151 / 160 Branchbran 86.1% 31 / 36 Conditioncond 60.0% 12 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 88.8% 32 / 36 Podpod 0.0% 0 / 2 totaltotal 86.0% 229 / 266 CC 27 SCAR 11.2
CC
27
Cov
90%
_build_config_file
8.3
_pick_dir
5.0
_build_database_connection
4.0
CRAP 3.1
« Silki/Schema/TextSearchResult.pm ↑ Summary Silki/Schema/Locale.pm »
1 package Silki::Config;
2 {
3 7 $Silki::Config::VERSION = '0.29';
4 }
5  
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use autodie qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Silki::Types qw( Bool Str Int HashRef Dir File );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Silki::Util qw( string_is_empty );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use MooseX::Configuration;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use MooseX::Params::Validate qw( validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 has is_production => (
23 is => 'rw',
24 isa => Bool,
25 default => 0,
26 section => 'Silki',
27 key => 'is_production',
28 documentation =>
29 'A flag indicating whether or not this is a production install. This should probably be true unless you are actively developing Silki.',
30 writer => '_set_is_production',
31 );
32  
33 7 has max_upload_size => (
34 is => 'ro',
35 isa => Int,
36 default => ( 10 * 1024 * 1024 ),
37 section => 'Silki',
38 key => 'max_upload_size',
39 documentation =>
40 'The maximum size of an upload in bytes.',
41 );
42  
43 7 has path_prefix => (
44 is => 'ro',
45 isa => Str,
46 default => q{},
47 section => 'Silki',
48 key => 'path_prefix',
49 documentation =>
50 'The URI path prefix for your Silki install. By default, this is empty. This affects URI generation and resolution.',
51 writer => '_set_path_prefix',
52 );
53  
54 7 has serve_static_files => (
55 is => 'ro',
56 isa => Bool,
57 builder => '_build_serve_static_files',
58 section => 'Silki',
59 key => 'static',
60 documentation =>
61 'If this is true, the Silki application will serve static files itself. Defaults to false when is_production is true.',
62 );
63  
64 7 has secret => (
65 is => 'ro',
66 isa => Str,
67 builder => '_build_secret',
68 section => 'Silki',
69 key => 'secret',
70 documentation =>
71 'A secret used as salt for digests in some URIs and for user authentication cookies. Changing this will invalidate all existing cookies.',
72 );
73  
74 7 has mod_rewrite_hack => (
75 is => 'ro',
76 isa => Bool,
77 default => q{},
78 section => 'Silki',
79 key => 'mod_rewrite_hack',
80 documentation =>
81 'The Apache mod_rewrite module does not pass the original path to the app server. Turn on this hack to work around that.',
82 );
83  
84 7 has is_profiling => (
85 is => 'rw',
86 isa => Bool,
87 lazy => 1,
88 builder => '_build_is_profiling',
89 writer => '_set_is_profiling',
90 );
91  
92 7 has database_connection => (
93 is => 'ro',
94 isa => HashRef,
95 lazy => 1,
96 builder => '_build_database_connection',
97 );
98  
99 7 has database_name => (
100 is => 'ro',
101 isa => Str,
102 default => 'Silki',
103 section => 'database',
104 key => 'name',
105 documentation =>
106 'The name of the database.',
107 writer => '_set_database_name',
108 );
109  
110 7 has database_username => (
111 is => 'ro',
112 isa => Str,
113 default => q{},
114 section => 'database',
115 key => 'username',
116 documentation =>
117 'The username to use when connecting to the database. By default, this is empty.',
118 writer => '_set_database_username',
119 );
120  
121 7 has database_password => (
122 is => 'ro',
123 isa => Str,
124 default => q{},
125 section => 'database',
126 key => 'password',
127 documentation =>
128 'The password to use when connecting to the database. By default, this is empty.',
129 writer => '_set_database_password',
130 );
131  
132 7 has database_host => (
133 is => 'ro',
134 isa => Str,
135 default => q{},
136 section => 'database',
137 key => 'host',
138 documentation =>
139 'The host to use when connecting to the database. By default, this is empty.',
140 writer => '_set_database_host',
141 );
142  
143 7 has database_port => (
144 is => 'ro',
145 isa => Str,
146 default => q{},
147 section => 'database',
148 key => 'port',
149 documentation =>
150 'The port to use when connecting to the database. By default, this is empty.',
151 writer => '_set_database_port',
152 );
153  
154 7 has database_ssl => (
155 is => 'ro',
156 isa => Bool,
157 default => 0,
158 section => 'database',
159 key => 'ssl',
160 documentation =>
161 'If this is true, then the database connection with require SSL.',
162 writer => '_set_database_ssl',
163 );
164  
165 7 has share_dir => (
166 is => 'ro',
167 isa => Dir,
168 coerce => 1,
169 builder => '_build_share_dir',
170 section => 'dirs',
171 key => 'share',
172 documentation =>
173 'The directory where share files are located. By default, these are installed in the Perl module directory tree, but you might want to change this to something like /usr/local/share/Silki.',
174 writer => '_set_share_dir',
175 );
176  
177 7 has cache_dir => (
178 is => 'ro',
179 isa => Dir,
180 coerce => 1,
181 builder => '_build_cache_dir',
182 section => 'dirs',
183 key => 'cache',
184 documentation =>
185 'The directory where generated files are stored. Defaults to /var/cache/silki.',
186 writer => '_set_cache_dir',
187 );
188  
189 7 has var_lib_dir => (
190 is => 'ro',
191 isa => Dir,
192 coerce => 1,
193 builder => '_build_var_lib_dir',
194 section => 'dirs',
195 key => 'var_lib',
196 documentation =>
197 'This directory stores files generated at install time (CSS and Javascript). Defaults to /var/lib/silki.',
198 );
199  
200 has _home_dir => (
201 is => 'rw',
202 isa => Dir,
203 lazy => 1,
204 7 ▶ default => sub { dir( File::HomeDir->my_home() ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
205 7 writer => '_set_home_dir',
206 );
207  
208 7 has etc_dir => (
209 is => 'rw',
210 isa => Dir,
211 coerce => 1,
212 lazy => 1,
213 builder => '_build_etc_dir',
214 writer => '_set_etc_dir',
215 );
216  
217 7 has files_dir => (
218 is => 'ro',
219 isa => Dir,
220 lazy => 1,
221 builder => '_build_files_dir',
222 );
223  
224 7 has small_image_dir => (
225 is => 'ro',
226 isa => Dir,
227 lazy => 1,
228 builder => '_build_small_image_dir',
229 );
230  
231 7 has thumbnails_dir => (
232 is => 'ro',
233 isa => Dir,
234 lazy => 1,
235 builder => '_build_thumbnails_dir',
236 );
237  
238 7 has mini_image_dir => (
239 is => 'ro',
240 isa => Dir,
241 lazy => 1,
242 builder => '_build_mini_image_dir',
243 );
244  
245 7 has temp_dir => (
246 is => 'ro',
247 isa => Dir,
248 lazy => 1,
249 default => '_build_temp_dir',
250 );
251  
252 7 has antispam_server => (
253 is => 'ro',
254 isa => Str,
255 default => q{api.antispam.typepad.com},
256 section => 'antispam',
257 key => 'server',
258 documentation =>
259 'The antispam server to use.',
260 );
261  
262 7 has antispam_key => (
263 is => 'ro',
264 isa => Str,
265 default => q{},
266 section => 'antispam',
267 key => 'key',
268 documentation =>
269 'A key for your antispam server. If this is empty, Silki will not be able to check for spam links.',
270 );
271  
272 {
273 7 my $Instance;
274  
275 8 ▶ sub instance {
Subroutine instance: called CC=2 SCAR=7.2
Pod undocumented
276 8 ▶ return $Instance ||= shift->new(@_);
Condition66%2/3
$Instance ||= (shift())->new(@_)
l!l&&r!l&&!r
260
MC/DC0%0/2
$Instance ||= (shift())->new(@_)
$Instance (shift())->new(@_)
Truth table66%2/3
A ||= B
A= $InstanceB= (shift())->new(@_)
ABresult
1X1
011
000
277 }
278  
279 0 ▶ sub _clear_instance {
Subroutine _clear_instance: not called CC=1 SCAR=6.9
280 0 undef $Instance;
281 }
282 }
283  
284 7 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
285 25 my $self = shift;
286  
287 25 ▶ return unless $self->is_production();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_production 22 2 24
288  
289 2 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
if string_is_empty($self->secret) 1 1 2
290 'You must supply a value for [Silki] - secret when running Silki in production'
291 if string_is_empty( $self->secret() );
292 }
293  
294 25 ▶sub _build_config_file {
Subroutine _build_config_file: called CC=8 SCAR=21.2
295 25 my $self = shift;
296  
297 25 ▶ if ( !string_is_empty( $ENV{SILKI_CONFIG} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (string_is_empty($ENV{"SILKI_CONFIG"})) 6 19 25
298 die
299 "Nonexistent config file in SILKI_CONFIG env var: $ENV{SILKI_CONFIG}"
300 6 ▶ unless -f $ENV{SILKI_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $ENV{"SILKI_CONFIG"} 1 5 6
301  
302 5 return file( $ENV{SILKI_CONFIG} );
303 }
304  
305 19 ▶ return if $ENV{SILKI_CONFIG_TESTING};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SILKI_CONFIG_TESTING"} 13 6 19
306  
307 6 my @dirs = dir('/etc/silki');
308 6 ▶ push @dirs, $self->_home_dir()->subdir( '.silki', 'etc' )
Branch: 1 branch
BranchTrueFalseTotal
if $> and $self->_home_dir 6 0 6
Condition33%1/3
$> and $self->_home_dir
!ll&&!rl&&r
006
MC/DC0%0/2
$> and $self->_home_dir
$> $self->_home_dir
Truth table33%1/3
A and B
A= $>B= $self->_home_dir
ABresult
0X0
100
111
309 if $> && $self->_home_dir();
310  
311 6 for my $dir (@dirs) {
312 12 my $file = $dir->file('silki.conf');
313  
314 12 ▶ return $file if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 0 12 12
315 }
316  
317 6 return;
318 }
319  
320 4 ▶sub _build_serve_static_files {
Subroutine _build_serve_static_files: called CC=3 SCAR=11.0
321 4 my $self = shift;
322  
323 return !( $ENV{MOD_PERL}
324 4 ▶ || $self->is_production()
Condition100%6/6
$ENV{'MOD_PERL'} || $self->is_production || $self->is_profiling
l!l&&r!l&&!r
211
$ENV{"MOD_PERL"} or $self->is_production
l!l&&r!l&&!r
112
MC/DC100%3/3
$ENV{'MOD_PERL'} || $self->is_production || $self->is_profiling
$ENV{"MOD_PERL"} $self->is_production $self->is_profiling
Truth table100%4/4
A or B || C
A= $ENV{"MOD_PERL"}B= $self->is_productionC= $self->is_profiling
ABCresult
1XX1
01X1
0011
0000
325 || $self->is_profiling() );
326 }
327  
328 {
329 7 my @Profilers = qw(
330 Devel/DProf.pm
331 Devel/FastProf.pm
332 Devel/NYTProf.pm
333 Devel/Profile.pm
334 Devel/Profiler.pm
335 Devel/SmallProf.pm
336 );
337  
338 3 ▶ sub _build_is_profiling {
Subroutine _build_is_profiling: called CC=2 SCAR=6.9
339 3 ▶ return 1 if grep { $INC{$_} } @Profilers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$INC{$_};} @Profilers 1 2 3
340 2 return 0;
341 }
342 }
343  
344 7 ▶sub _build_var_lib_dir {
Subroutine _build_var_lib_dir: called CC=1 SCAR=0.0
345 2 my $self = shift;
346  
347 2 return $self->_dir(
348 [ 'var', 'lib' ],
349 '/var/lib/silki',
350 );
351 }
352  
353 7 ▶sub _build_share_dir {
Subroutine _build_share_dir: called CC=1 SCAR=0.0
354 7 my $self = shift;
355  
356 # I'd like to use File::ShareDir, but it blows up if the directory doesn't
357 # exist, which isn't very fucking helpful. This is equivalent to
358 # dist_dir('Silki')
359 my $share_dir = dir(
360 7 dir( $INC{'Silki/Config.pm'} )->parent(),
361 'auto', 'share', 'dist',
362 'Silki'
363 )->absolute()->cleanup();
364  
365 7 return $self->_dir(
366 ['share'],
367 $share_dir,
368 dir('share')->absolute(),
369 );
370 }
371  
372 3 ▶sub _build_cache_dir {
Subroutine _build_cache_dir: called CC=1 SCAR=0.0
373 3 my $self = shift;
374  
375 3 return $self->_dir(
376 ['cache'],
377 '/var/cache/silki',
378 );
379 }
380  
381 2 ▶sub _build_etc_dir {
Subroutine _build_etc_dir: called CC=3 SCAR=12.5
382 2 my $self = shift;
383  
384 2 ▶ return $self->config_file()->dir()
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config_file and -f $self->config_file 0 2 2
Condition33%1/3
defined $self->config_file and -f $self->config_file
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->config_file and -f $self->config_file
defined $self->config_file -f $self->config_file
Truth table33%1/3
A and B
A= defined $self->config_fileB= -f $self->config_file
ABresult
0X0
100
111
385 if defined $self->config_file() && -f $self->config_file();
386  
387 2 return $self->_pick_dir(
388 ['etc'],
389 '/etc/silki',
390 );
391 }
392  
393 2 ▶sub _build_files_dir {
Subroutine _build_files_dir: called CC=1 SCAR=0.0
394 2 my $self = shift;
395  
396 2 return $self->_cache_subdir('files');
397 }
398  
399 0 ▶sub _build_small_image_dir {
Subroutine _build_small_image_dir: not called CC=1 SCAR=6.9
400 0 my $self = shift;
401  
402 0 return $self->_cache_subdir('small-image');
403 }
404  
405 2 ▶sub _build_thumbnails_dir {
Subroutine _build_thumbnails_dir: called CC=1 SCAR=0.0
406 2 my $self = shift;
407  
408 2 return $self->_cache_subdir('thumbnails');
409 }
410  
411 0 ▶sub _build_mini_image_dir {
Subroutine _build_mini_image_dir: not called CC=1 SCAR=6.9
412 0 my $self = shift;
413  
414 0 return $self->_cache_subdir('mini-image');
415 }
416  
417 0 ▶sub _build_temp_dir {
Subroutine _build_temp_dir: not called CC=1 SCAR=6.9
418 0 my $self = shift;
419  
420 0 my $temp = dir( File::Spec->tmpdir() )->subdir('silki');
421  
422 0 $self->_ensure_dir($temp);
423  
424 0 return $temp;
425 }
426  
427 4 ▶sub _cache_subdir {
Subroutine _cache_subdir: called CC=1 SCAR=0.0
428 4 my $self = shift;
429 4 my $name = shift;
430  
431 4 my $subdir = $self->cache_dir()->subdir($name);
432  
433 4 $self->_ensure_dir($subdir);
434  
435 4 return $subdir;
436 }
437  
438 12 ▶sub _dir {
Subroutine _dir: called CC=1 SCAR=0.0
439 12 my $self = shift;
440  
441 12 my $dir = $self->_pick_dir(@_);
442  
443 12 $self->_ensure_dir($dir);
444  
445 12 return $dir;
446 }
447  
448 7 my $TestingRootDir;
449  
450 14 ▶sub _pick_dir {
Subroutine _pick_dir: called CC=5 SCAR=16.1
451 14 my $self = shift;
452 14 my $pieces = shift;
453 14 my $prod_default = shift;
454 14 my $dev_default = shift;
455  
456 14 ▶ return dir($prod_default)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_production 4 10 14
457 if $self->is_production();
458  
459 10 ▶ return $dev_default
Branch: 1 branch
BranchTrueFalseTotal
if defined $dev_default 6 4 10
460 if defined $dev_default;
461  
462 4 ▶ if ( $ENV{HARNESS_ACTIVE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HARNESS_ACTIVE"}) 1 3 4
463 1 ▶ $TestingRootDir ||= tempdir( CLEANUP => 1 );
Condition33%1/3
$TestingRootDir ||= tempdir("CLEANUP", 1)
l!l&&r!l&&!r
010
MC/DC0%0/2
$TestingRootDir ||= tempdir("CLEANUP", 1)
$TestingRootDir tempdir("CLEANUP", 1)
Truth table33%1/3
A ||= B
A= $TestingRootDirB= tempdir("CLEANUP", 1)
ABresult
1X1
011
000
464  
465 1 return dir( $TestingRootDir, @{$pieces} );
466 }
467  
468 3 return dir( $self->_home_dir(), '.silki', @{$pieces} );
469 }
470  
471 12 ▶sub _ensure_dir {
Subroutine _ensure_dir: called CC=3 SCAR=11.0
472 12 my $self = shift;
473 12 my $dir = shift;
474  
475 12 ▶ return if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 6 6 12
476  
477 6 ▶ $dir->mkpath( 0, 0755 )
Branch: 1 branch
BranchTrueFalseTotal
$dir->mkpath(0, 493) or die "Cannot make ${dir}: $!" 6 0 6
478 or die "Cannot make $dir: $!";
479  
480 6 return;
481 }
482  
483 2 ▶sub _build_database_connection {
Subroutine _build_database_connection: called CC=4 SCAR=13.9
484 2 my $self = shift;
485  
486 2 my $dsn = 'dbi:Pg:dbname=' . $self->database_name();
487  
488 2 ▶ if ( my $host = $self->database_host() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $self->database_host) 1 1 2
489 1 $dsn .= ';host=' . $host;
490 }
491  
492 2 ▶ if ( my $port = $self->database_port() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = $self->database_port) 1 1 2
493 1 $dsn .= ';port=' . $port;
494 }
495  
496 2 ▶ $dsn .= ';sslmode=require'
Branch: 1 branch
BranchTrueFalseTotal
if $self->database_ssl 0 2 2
497 if $self->database_ssl();
498  
499 return {
500 2 dsn => $dsn,
501 username => $self->database_username(),
502 password => $self->database_password(),
503 };
504 }
505  
506 2 ▶sub _build_secret {
Subroutine _build_secret: called CC=2 SCAR=6.9
507 2 my $self = shift;
508  
509 2 ▶ return $self->is_production()
Branch: 1 branch
BranchTrueFalseTotal
$self->is_production ? : 1 1 2
510 ? q{} # will cause an error in BUILD
511 : 'a big secret';
512 }
513  
514 1 ▶around write_config_file => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
515 1 my $orig = shift;
516 1 my $self = shift;
517  
518 1 ▶ my $version = $Silki::Config::VERSION || '(working copy)';
Condition50%1/2
$Silki::Config::VERSION || '(working copy)'
l!l
10
MC/DC0%0/1
$Silki::Config::VERSION || '(working copy)'
$Silki::Config::VERSION
Truth table50%1/2
A
A= $Silki::Config::VERSION
Aresult
11
00
519 1 my $generated = "Config file generated by Silki version $version";
520  
521 1 $self->$orig( generated_by => $generated, @_ );
522 7 };
523  
524 7 __PACKAGE__->meta()->make_immutable();
525  
526 7 1;
527  
528 # ABSTRACT: Configuration information for Silki
529  
530 __END__
« Silki/Schema/TextSearchResult.pm ↑ Summary Silki/Schema/Locale.pm »