blib/lib/ClearPress/util.pm

filter: Statementstmt 89.6% 138 / 154 Branchbran 60.0% 30 / 50 Conditioncond 53.8% 14 / 26 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 33 / 33 Podpod 100% 18 / 18 totaltotal 79.4% 236 / 297 CC 38 SCAR 16.4
CC
38
Cov
79%
_singleton_key
16.0
config
9.3
END
7.5
CRAP 5.1
« blib/lib/ClearPress/driver/SQLite.pm ↑ Summary blib/lib/ClearPress/Localize.pm »
1 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2 # vim:ts=8:sw=2:et:sta:sts=2
3 #########
4 # Author: rmp
5 #
6 package ClearPress::util;
7 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use ClearPress::driver;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use IO::Capture::Stderr;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 13 our $VERSION = q[2026.07.25];
20  
21 13 our $DEBUG_UTIL = 0;
22 13 our $DEFAULT_TRANSACTIONS = 1;
23 13 our $DEFAULT_DRIVER = 'mysql';
24 13 my $INSTANCES = {}; # per-process table of singletons (nasty!)
25  
26 13 __PACKAGE__->mk_accessors(qw(transactions username requestor profiler session));
27  
28 13 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=18.5
29 0 ▶ use constant MP2 => eval { require Apache2::RequestUtil; Apache2::RequestUtil->can('request') && $Apache2::RequestUtil::VERSION > 1.99 }; ## no critic (ProhibitConstantPragma, RequireCheckingReturnValueOfEval)
Subroutine BEGIN: called CC=2 SCAR=9.2
Condition0%0/3
'Apache2::RequestUtil'->can('request') && $Apache2::RequestUtil::VERSION > 1.99
!ll&&!rl&&r
000
MC/DC0%0/2
'Apache2::RequestUtil'->can('request') && $Apache2::RequestUtil::VERSION > 1.99
'Apache2::RequestUtil'->can('request') $Apache2::RequestUtil::VERSION > 1.99
Truth table0%0/3
A && B
A= 'Apache2::RequestUtil'->can('request')B= $Apache2::RequestUtil::VERSION > 1.99
ABresult
0X0
100
111
30  
31 13 ▶ if(MP2) {
Branch: 1 branch
BranchTrueFalseTotal
if (&MP2()) { } 0 13 13
32 0 ▶ $DEBUG_UTIL && carp q[Using request-based singletons [mod_perl2 found]];
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::util::DEBUG_UTIL and carp("Using request-based singletons [mod_perl2 found]") 0 0 0
33 } else {
34 13 ▶ $DEBUG_UTIL && carp q[Using process-based singletons [mod_perl2 not found]];
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::util::DEBUG_UTIL and carp("Using process-based singletons [mod_perl2 not found]") 0 13 13
35 }
36 }
37  
38 44 ▶sub _singleton_key {
Subroutine _singleton_key: called CC=6 SCAR=27.7
39 44 my ($self) = @_;
40 #########
41 # classic mode
42 #
43 44 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
17270
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
44 44 my $singleton_key = $class;
45  
46 #########
47 # per-request mode - should support mpm_worker & mpm_event
48 # Could this be done using $ENV{request-id} ||= uuid->new in regular CGI mode?
49 #
50 44 ▶ if(MP2) {
Branch: 1 branch
BranchTrueFalseTotal
if (&MP2()) 0 44 44
51 0 my $request = Apache2::RequestUtil->request;
52 0 $singleton_key = $request->pnotes($class);
53  
54 0 ▶ if(!$singleton_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $singleton_key) { } 0 0 0
55 0 $singleton_key = Data::UUID->new->create_str;
56 0 $request->pnotes($class => $singleton_key);
57 0 ▶ $DEBUG_UTIL and carp qq[new util singleton = $singleton_key];
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::util::DEBUG_UTIL and carp("new util singleton = $singleton_key") 0 0 0
58 } else {
59 0 ▶ $DEBUG_UTIL and carp qq[reuse util singleton = $singleton_key];
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::util::DEBUG_UTIL and carp("reuse util singleton = $singleton_key") 0 0 0
60 }
61 }
62  
63 44 return $singleton_key;
64 }
65  
66 27 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
67 27 my ($class, $ref) = @_;
68  
69 27 my $self = {};
70 27 my $singleton_key = $class->_singleton_key;
71  
72 27 ▶ if(exists $INSTANCES->{$singleton_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INSTANCES->{$singleton_key}) 8 19 27
73 8 $self = $INSTANCES->{$singleton_key};
74 }
75  
76 27 ▶ if($ref && ref $ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and ref $ref eq "HASH") 2 25 27
Condition66%2/3
$ref and ref $ref eq "HASH"
!ll&&!rl&&r
2502
MC/DC50%1/2
$ref and ref $ref eq "HASH"
$ref ref $ref eq "HASH"
Truth table66%2/3
A and B
A= $refB= ref $ref eq "HASH"
ABresult
0X0
100
111
77 2 ▶ while(my ($k, $v) = each %{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ref;}) 2 2 4
78 2 $self->{$k} = $v;
79 }
80 }
81  
82 27 ▶ if(!exists $self->{transactions}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"transactions"}) 19 8 27
83 19 $self->{transactions} = $DEFAULT_TRANSACTIONS;
84 }
85  
86 27 $INSTANCES->{$singleton_key} = bless $self, $class;
87  
88 27 return $INSTANCES->{$singleton_key};
89 }
90  
91 56 ▶sub cgi {
Subroutine cgi: called CC=3 SCAR=11.0
Pod documented
92 56 my ($self, $cgi) = @_;
93  
94 56 ▶ if($cgi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cgi) 17 39 56
95 17 $self->{cgi} = $cgi;
96 }
97  
98 56 ▶ if(!$self->{cgi}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cgi"}) 4 52 56
99 4 $self->{cgi} = CGI->new();
100 }
101  
102 56 return $self->{cgi};
103 }
104  
105 2 ▶sub data_path {
Subroutine data_path: called CC=1 SCAR=0.0
Pod documented
106 2 return q(data);
107 }
108  
109 848 ▶sub configpath {
Subroutine configpath: called CC=3 SCAR=11.0
Pod documented
110 848 my ($self, @args) = @_;
111  
112 848 ▶ if(scalar @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 1 847 848
113 1 $self->{configpath} = shift @args;
114 }
115  
116 848 ▶ return $self->{configpath} || $self->data_path().'/config.ini';
Condition100%2/2
$self->{'configpath'} || $self->data_path . '/config.ini'
l!l
8840
MC/DC100%1/1
$self->{'configpath'} || $self->data_path . '/config.ini'
$self->{'configpath'}
Truth table100%2/2
A
A= $self->{'configpath'}
Aresult
11
00
117 }
118  
119 205 ▶sub dbsection {
Subroutine dbsection: called CC=2 SCAR=6.9
Pod documented
120 205 ▶ return $ENV{dev} || 'live';
Condition100%2/2
$ENV{'dev'} || 'live'
l!l
1978
MC/DC100%1/1
$ENV{'dev'} || 'live'
$ENV{'dev'}
Truth table100%2/2
A
A= $ENV{'dev'}
Aresult
11
00
121 }
122  
123 846 ▶sub config {
Subroutine config: called CC=8 SCAR=22.3
Pod documented
124 846 my $self = shift;
125 846 ▶ my $configpath = $self->configpath() || q();
Condition50%1/2
$self->configpath || ''
l!l
8460
MC/DC0%0/1
$self->configpath || ''
$self->configpath
Truth table50%1/2
A
A= $self->configpath
Aresult
11
00
126 846 my $dtconfigpath;
127  
128 846 ▶ if(!$self->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}) 21 825 846
129 21 ($dtconfigpath) = $configpath =~ m{([[:lower:][:digit:]_/.\-]+)}smix;
130 21 ▶ $dtconfigpath ||= q();
Condition50%1/2
$dtconfigpath ||= ""
l!l
210
MC/DC0%0/1
$dtconfigpath ||= ""
$dtconfigpath
Truth table50%1/2
A
A= $dtconfigpath
Aresult
11
00
131  
132 21 ▶ if($dtconfigpath ne $configpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dtconfigpath ne $configpath) 0 21 21
133 0 croak qq(Failed to detaint configpath: '$configpath');
134 }
135  
136 21 ▶ if(!-e $dtconfigpath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dtconfigpath) 1 20 21
137 1 croak qq(No such file: $dtconfigpath);
138 }
139  
140 20 ▶ $self->{config} ||= Config::IniFiles->new(
Condition33%1/3
$self->{"config"} ||= "Config::IniFiles"->new("-file", $dtconfigpath)
l!l&&r!l&&!r
0200
MC/DC0%0/2
$self->{"config"} ||= "Config::IniFiles"->new("-file", $dtconfigpath)
$self->{"config"} "Config::IniFiles"->new("-file", $dtconfigpath)
Truth table33%1/3
A ||= B
A= $self->{"config"}B= "Config::IniFiles"->new("-file", $dtconfigpath)
ABresult
1X1
011
000
141 -file => $dtconfigpath,
142 );
143 }
144  
145 845 ▶ if(!$self->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}) 0 845 845
146 0 croak qq(No configuration available:\n). join q(, ), @Config::IniFiles::errors; ## no critic (Variables::ProhibitPackageVars)
147 }
148  
149 845 return $self->{config};
150 }
151  
152 186 ▶sub dbh {
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
153 186 my $self = shift;
154  
155 186 return $self->driver->dbh();
156 }
157  
158 1 ▶sub quote {
Subroutine quote: called CC=1 SCAR=0.0
Pod documented
159 1 my ($self, $str) = @_;
160 1 return $self->dbh->quote($str);
161 }
162  
163 232 ▶sub driver {
Subroutine driver: called CC=6 SCAR=18.6
Pod documented
164 232 my ($self, @args) = @_;
165  
166 232 ▶ if(!$self->{driver}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"driver"}) 29 203 232
167 29 my $dbsection = $self->dbsection();
168 29 my $config = $self->config();
169  
170 29 ▶ if(!$dbsection || !$config->SectionExists($dbsection)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbsection and $config->SectionExists($dbsection)) 0 29 29
Condition33%1/3
$dbsection and $config->SectionExists($dbsection)
!ll&&!rl&&r
0029
MC/DC0%0/2
$dbsection and $config->SectionExists($dbsection)
$dbsection $config->SectionExists($dbsection)
Truth table33%1/3
A and B
A= $dbsectionB= $config->SectionExists($dbsection)
ABresult
0X0
100
111
171 0 croak q[Unable to determine config set to use. Try adding [live] [dev] or [test] sections to config.ini];
172 }
173  
174 29 ▶ my $drivername = $config->val($dbsection, 'driver') || $DEFAULT_DRIVER;
Condition66%2/3
$config->val($dbsection, 'driver') || $ClearPress::util::DEFAULT_DRIVER
l!l&&r!l&&!r
2810
MC/DC0%0/2
$config->val($dbsection, 'driver') || $ClearPress::util::DEFAULT_DRIVER
$config->val($dbsection, 'driver') $ClearPress::util::DEFAULT_DRIVER
Truth table66%2/3
A || B
A= $config->val($dbsection, 'driver')B= $ClearPress::util::DEFAULT_DRIVER
ABresult
1X1
011
000
175 29 my $ref = {};
176  
177 29 for my $field (qw(dbname dbhost dbport dbuser dbpass dsn_opts)) {
178 174 $ref->{$field} = $self->$field()
179 }
180  
181 29 $self->{driver} = ClearPress::driver->new_driver($drivername, $ref);
182 }
183  
184 232 return $self->{driver};
185 }
186  
187 1 ▶sub log { ## no critic (homonym)
Subroutine log: called CC=2 SCAR=7.0
Pod documented
188 1 my ($self, @args) = @_;
189 1 ▶ print {*STDERR} map { (strftime '[%Y-%m-%dT%H:%M:%S] ', localtime). "$_\n" } @args or croak $ERRNO;
Branch: 1 branch
BranchTrueFalseTotal
print {*STDERR;} map({strftime("[%Y-%m-%dT%H:%M:%S] ", localtime) . "$_\n";} @args) or croak($ClearPress::util::ERRNO) 1 0 1
190 1 return 1;
191 }
192  
193 17 ▶sub cleanup {
Subroutine cleanup: called CC=2 SCAR=6.9
Pod documented
194 17 my $self = shift;
195  
196 #########
197 # cleanup() is called by controller at the end of a request:response
198 # cycle. Here we neutralise the singleton instance so it doesn't
199 # carry over any stateful information to the next request - CGI,
200 # DBH, TT and anything else cached in data members.
201 #
202 17 my $singleton_key = $self->_singleton_key;
203  
204 17 delete $INSTANCES->{$singleton_key};
205  
206 17 ▶ if(exists $self->{driver}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"driver"}) 16 1 17
207 16 delete $self->{driver}; # should trigger driver::DESTROY
208 }
209  
210 17 return 1;
211 }
212  
213 174 ▶sub db_credentials {
Subroutine db_credentials: called CC=2 SCAR=6.9
Pod documented
214 174 my $self = shift;
215 174 my $cfg = $self->config();
216 174 my $dbsection = $self->dbsection();
217 174 my $ref = {};
218  
219 174 for my $field (qw(dbuser dbpass dbhost dbport dbname dsn_opts)) {
220 1044 $ref->{$field} = $cfg->val($dbsection, $field);
221 }
222  
223 174 return $ref;
224 }
225  
226 29 ▶sub dbname {
Subroutine dbname: called CC=1 SCAR=0.0
Pod documented
227 29 my $self = shift;
228 29 return $self->db_credentials->{dbname};
229 }
230  
231 29 ▶sub dbuser {
Subroutine dbuser: called CC=1 SCAR=0.0
Pod documented
232 29 my $self = shift;
233 29 return $self->db_credentials->{dbuser};
234 }
235  
236 29 ▶sub dbpass {
Subroutine dbpass: called CC=1 SCAR=0.0
Pod documented
237 29 my $self = shift;
238 29 return $self->db_credentials->{dbpass};
239 }
240  
241 29 ▶sub dbhost {
Subroutine dbhost: called CC=1 SCAR=0.0
Pod documented
242 29 my $self = shift;
243 29 return $self->db_credentials->{dbhost};
244 }
245  
246 29 ▶sub dbport {
Subroutine dbport: called CC=1 SCAR=0.0
Pod documented
247 29 my $self = shift;
248 29 return $self->db_credentials->{dbport};
249 }
250  
251 29 ▶sub dsn_opts {
Subroutine dsn_opts: called CC=1 SCAR=0.0
Pod documented
252 29 my $self = shift;
253 29 return $self->db_credentials->{dsn_opts};
254 }
255  
256 12 ▶END {
Subroutine END: called CC=4 SCAR=20.1
257 # dereferences and causes orderly destruction of all instances
258 12 my $cap = IO::Capture::Stderr->new();
259 12 $cap->start;
260 12 undef $INSTANCES;
261 12 $cap->stop;
262 12 ▶ while(my $line = $cap->read()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $cap->read) 0 12 12
263 0 ▶ if($line =~ /MySQL[ ]server[ ]has[ ]gone[ ]away/smix) { # brute force do not display these copious, noisy warnings
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /MySQL[ ]server[ ]has[ ]gone[ ]away/imsx) 0 0 0
264 0 next;
265 }
266  
267 0 ▶ print {*STDERR} $line or croak qq[Error printing: $ERRNO];
Branch: 1 branch
BranchTrueFalseTotal
print {*STDERR;} $line or croak("Error printing: $ClearPress::util::ERRNO") 0 0 0
268 }
269 }
270  
271 13 1;
272  
273 __END__
« blib/lib/ClearPress/driver/SQLite.pm ↑ Summary blib/lib/ClearPress/Localize.pm »