WSST/Templates/php/lib.pm untested

filter: Statementstmt 0.0% 0 / 318 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 548 CC 49 SCAR 32.6
CC
49
Cov
0%
make_current_page
56.0
make_params_conf
42.0
make_is_error
42.0
CRAP 26.2
1 use POSIX qw(strftime);
2 use Digest::MD5 ();
3  
4 # sort files
5 {
6 my @package_xml = grep(/package\.xml\.tmpl$/, @$files);
7 my @other = grep(!/package\.xml\.tmpl$/, @$files);
8 push(@other, @package_xml);
9 @$files = @other;
10 }
11  
12 sub package_name {
13 return sprintf("Services_%s_%s",
14 $tmpl->get('company_name'),
15 $tmpl->get('service_name'));
16 }
17  
18 sub package_dir {
19 return sprintf("Services/%s/%s",
20 $tmpl->get('company_name'),
21 $tmpl->get('service_name'));
22 }
23  
24 sub php_license_uri {
25 return $schema->{php_license_uri} || {
26 'PHP' => 'http://www.php.net/license/',
27 'Apache' => 'http://www.apache.org/licenses/',
28 'LGPL' => 'http://www.gnu.org/copyleft/lesser.html',
29 'BSD style' => 'http://www.opensource.org/licenses/bsd-license.php',
30 'BSD' => 'http://www.opensource.org/licenses/bsd-license.php',
31 'MIT' => 'http://www.opensource.org/licenses/mit-license.html',
32 }->{$tmpl->get('license')};
33 }
34  
35 sub php_channel {
36 return $schema->{php_channel} || "__uri";
37 }
38  
39 sub php_copyright {
40 return $schema->{php_copyright} || "";
41 }
42  
43 sub php_link {
44 return $schema->{php_link} || sprintf("http://localhost/%s", &package_name);
45 }
46  
47 sub php_license_abstract {
48 return $schema->{php_license_abstract} || "";
49 }
50  
51 sub make_params_conf {
52 my ($method, $indent, $indstr) = @_;
53 $indent ||= 0;
54 $indstr ||= " ";
55
56 my $conf = {};
57
58 foreach my $param (@{$method->{params}}) {
59 next if $param->{fixed};
60 push(@{$conf->{'keys'}}, $param->{name});
61 }
62
63 foreach my $param (@{$method->{params}}) {
64 if ($param->{'require'}) {
65 $conf->{'notnull'}->{$param->{name}}++;
66 }
67
68 if ($param->{default}) {
69 $conf->{defaults}->{$param->{name}} = $param->{default};
70 }
71  
72 if ($param->{fixed}) {
73 $conf->{fixed}->{$param->{name}} = $param->{fixed};
74 }
75 }
76
77 if (exists $conf->{'notnull'}) {
78 $conf->{'notnull'} = [sort keys %{$conf->{'notnull'}}];
79 }
80
81 my $result = make_php_array($conf, $indent, $indstr);
82 $result =~ s/^\s+//;
83
84 return $result;
85 }
86  
87 sub make_response_conf {
88 my ($method, $indent, $indstr) = @_;
89 $indent ||= 0;
90 $indstr ||= " ";
91
92 my $conf = {};
93  
94 my $que = [[$method->{'return'}]];
95 while (@$que) {
96 my $node = shift(@$que);
97 foreach my $n (@$node) {
98 if (exists $n->{children}) {
99 push(@$que, $n->{children});
100 }
101 if ($n->{multiple}) {
102 $conf->{'force_array'}->{$n->{name}}++;
103 }
104 }
105 }
106
107 if (exists $conf->{'force_array'}) {
108 $conf->{'force_array'} = [sort keys %{$conf->{'force_array'}}];
109 }
110
111 if (exists $method->{'error_status'}) {
112 $conf->{'error_status'} = [@{$method->{'error_status'}}];
113 }
114
115 my $result = make_php_array($conf, $indent, $indstr);
116 $result =~ s/^\s+//;
117
118 return $result;
119 }
120  
121 sub make_php_array {
122 my ($data, $indent, $indstr, $join) = @_;
123 $indent ||= 0;
124 $indstr ||= " ";
125 $join ||= "\n";
126
127 sub rec_func {
128 my ($dd, $ii, $is) = @_;
129
130 my $pf = $is x $ii;
131
132 my $ref = ref $dd;
133 if ($ref eq 'HASH') {
134 my $res = [];
135 push(@$res, "${pf}array(");
136 foreach my $ddd (sort keys %$dd) {
137 my $dddd = rec_func($dd->{$ddd}, $ii+1, $is);
138 $dddd =~ s/^$pf$is//;
139 push(@$res, "$pf$is'$ddd' => $dddd,");
140 }
141 push(@$res, "${pf})");
142 return join($join, @$res);
143 } elsif ($ref eq 'ARRAY') {
144 my $res = [];
145 push(@$res, "${pf}array(");
146 foreach my $ddd (@$dd) {
147 push(@$res, rec_func($ddd, $ii+1, $is) . ",");
148 }
149 push(@$res, "${pf})");
150 return join($join, @$res);
151 }
152
153 return "$pf'$dd'";
154 }
155
156 return rec_func($data, $indent, $indstr);
157 }
158  
159 sub make_package_contents {
160 my $strs = [];
161
162 my $md5 = Digest::MD5->new();
163
164 foreach my $file (@$result) {
165 my $cfile = $file;
166 $cfile =~ s#^\Q$odir\E/##;
167
168 my $md5sum = "";
169 if (open(IN, $file)) {
170 $md5->addfile(\*IN);
171 $md5sum = sprintf(' md5sum="%s"', $md5->hexdigest);
172 $md5->reset();
173 close(IN);
174 }
175
176 my $role = 'php';
177 $role = 'test' if $cfile =~ /tests/;
178 $role = 'doc' if $cfile =~ /docs/;
179
180 my $bid = "Services/" . $tmpl->get('company_name');
181
182 my $ostr =<<EOS;
183 <file baseinstalldir="$bid"$md5sum name="$cfile" role="$role">
184 <tasks:replace from="\@package_version\@" to="version" type="package-info" />
185 </file>
186 EOS
187 push(@$strs, $ostr);
188 }
189
190 my $str = join("", @$strs);
191 $str =~ s/\s+$//;
192
193 return $str;
194 }
195  
196 sub make_is_error {
197 my ($method, $indent, $indstr) = @_;
198 $indent ||= 0;
199 $indstr ||= " ";
200
201 return "return false;" unless $method->{'error'};
202
203 my $strs = [];
204 push(@$strs, "\$data =& \$this->getData();");
205
206 my $ret_test = [map {['', $_]} @{$method->{'error'}->{children}}];
207 while (my $ret = shift(@$ret_test)) {
208 next if $ret->[1]->{nullable};
209 my $php_var = sprintf('$data%s->%s', $ret->[0], $ret->[1]->{name});
210 push(@$strs, ($indstr x $indent) . "if (!isset($php_var)) {");
211 push(@$strs, ($indstr x ($indent + 1)) . "return false;");
212 if ($ret->[1]->{'values'}) {
213 my $php_array = sprintf('array(%s)', join(", ", map {qq|"$_"|} @{$ret->[1]->{'values'}}));
214 push(@$strs, ($indstr x ($indent)) . "} elseif (!in_array($php_var, $php_array)) {");
215 push(@$strs, ($indstr x ($indent + 1)) . "return false;");
216 }
217 push(@$strs, ($indstr x $indent) . "}");
218 next unless $ret->[1]->{children};
219 my $next_node = $ret->[0].'->'.$ret->[1]->{name};
220 $next_node .= '[0]'
221 if $ret->[1]->{multiple};
222 push(@$ret_test, map {[$next_node, $_]} @{$ret->[1]->{children}});
223 }
224  
225 push(@$strs, ($indstr x $indent) . "return true;");
226
227 return join("\n", @$strs);
228 }
229  
230 sub make_error_message {
231 my ($method, $indent, $indstr) = @_;
232 $indent ||= 0;
233 $indstr ||= " ";
234
235 my $ret_test = [map {['', $_]} @{$method->{'error'}->{children}}];
236 while (my $ret = shift(@$ret_test)) {
237 next if $ret->[1]->{nullable};
238 if ($ret->[1]->{error_message}) {
239 my $strs = [];
240 push(@$strs, "\$data =& \$this->getData();");
241 my $php_var = sprintf('$data%s->%s', $ret->[0], $ret->[1]->{name});
242 if ($ret->[1]->{error_message_map}) {
243 my $msg_map = $ret->[1]->{error_message_map};
244 my $php_array = make_php_array($msg_map, $indent, $indstr);
245 $php_array =~ s/^\s+//;
246 push(@$strs, ($indstr x $indent) . "\$msg_map = $php_array;");
247 push(@$strs, ($indstr x $indent) . "\$val = $php_var;");
248 push(@$strs, ($indstr x $indent) . "if (array_key_exists(\$val, \$msg_map)) {");
249 push(@$strs, ($indstr x ($indent + 1)) . "return \$msg_map[\$val];");
250 push(@$strs, ($indstr x $indent) . "}");
251 push(@$strs, ($indstr x ($indent)) . "return \$val;");
252 } else {
253 push(@$strs, ($indstr x $indent) . "return $php_var;");
254 }
255 return join("\n", @$strs);
256 }
257 next unless $ret->[1]->{children};
258 my $next_node = $ret->[0].'->'.$ret->[1]->{name};
259 $next_node .= '[0]'
260 if $ret->[1]->{multiple};
261 push(@$ret_test, map {[$next_node, $_]} @{$ret->[1]->{children}});
262 }
263
264 return "return 'Unknown error';";
265 }
266  
267 sub make_total_entries {
268 my ($method, $indent, $indstr) = @_;
269 $indent ||= 0;
270 $indstr ||= " ";
271
272 my $ret_test = [map {['', $_]} @{$method->{'return'}->{children}}];
273 while (my $ret = shift(@$ret_test)) {
274 next if $ret->[1]->{nullable};
275 if ($ret->[1]->{page_total_entries}) {
276 my $strs = [];
277 push(@$strs, "\$data =& \$this->getData();");
278 my $php_var = sprintf('$data%s->%s', $ret->[0], $ret->[1]->{name});
279 push(@$strs, ($indstr x $indent) . "return $php_var;");
280 return join("\n", @$strs);
281 }
282 next unless $ret->[1]->{children};
283 my $next_node = $ret->[0].'->'.$ret->[1]->{name};
284 $next_node .= '[0]'
285 if $ret->[1]->{multiple};
286 push(@$ret_test, map {[$next_node, $_]} @{$ret->[1]->{children}});
287 }
288
289 return "return 0;";
290 }
291  
292 sub make_entries_per_page {
293 my ($method, $indent, $indstr) = @_;
294 $indent ||= 0;
295 $indstr ||= " ";
296
297 my $ret_test = [map {['', $_]} @{$method->{'return'}->{children}}];
298 while (my $ret = shift(@$ret_test)) {
299 next if $ret->[1]->{nullable};
300 if ($ret->[1]->{page_entries_per_page}) {
301 my $strs = [];
302 push(@$strs, "\$data =& \$this->getData();");
303 my $php_var = sprintf('$data%s->%s', $ret->[0], $ret->[1]->{name});
304 push(@$strs, ($indstr x $indent) . "return $php_var;");
305 return join("\n", @$strs);
306 }
307 next unless $ret->[1]->{children};
308 my $next_node = $ret->[0].'->'.$ret->[1]->{name};
309 $next_node .= '[0]'
310 if $ret->[1]->{multiple};
311 push(@$ret_test, map {[$next_node, $_]} @{$ret->[1]->{children}});
312 }
313
314 return "return 0;";
315 }
316  
317 sub make_current_page {
318 my ($method, $indent, $indstr) = @_;
319 $indent ||= 0;
320 $indstr ||= " ";
321
322 my $ret_test = [map {['', $_]} @{$method->{'return'}->{children}}];
323 while (my $ret = shift(@$ret_test)) {
324 next if $ret->[1]->{nullable};
325 if ($ret->[1]->{page_current_page}) {
326 my $strs = [];
327 push(@$strs, "\$data =& \$this->getData();");
328 my $php_var = sprintf('$data%s->%s', $ret->[0], $ret->[1]->{name});
329 push(@$strs, ($indstr x $indent) . "return $php_var;");
330 return join("\n", @$strs);
331 } elsif ($ret->[1]->{page_current_offset}) {
332 my $orig = $ret->[1]->{page_current_offset_origin} ? " - $ret->[1]->{page_current_offset_origin}" : "";
333 my $strs = [];
334 push(@$strs, "\$data =& \$this->getData();");
335 push(@$strs, ($indstr x $indent) . "\$epp = \$this->getEntriesPerPage();");
336 push(@$strs, ($indstr x $indent) . "if (\$epp == 0) {");
337 push(@$strs, ($indstr x ($indent + 1)) . "return 0;");
338 push(@$strs, ($indstr x $indent) . "}");
339 my $php_var = sprintf('$data%s->%s', $ret->[0], $ret->[1]->{name});
340 push(@$strs, ($indstr x $indent) . "return (($php_var$orig) / \$epp) + 1;");
341 return join("\n", @$strs);
342 }
343 next unless $ret->[1]->{children};
344 my $next_node = $ret->[0].'->'.$ret->[1]->{name};
345 $next_node .= '[0]'
346 if $ret->[1]->{multiple};
347 push(@$ret_test, map {[$next_node, $_]} @{$ret->[1]->{children}});
348 }
349
350 return "return 0;";
351 }
352  
353 sub make_page_param {
354 my ($method, $indent, $indstr) = @_;
355 $indent ||= 0;
356 $indstr ||= " ";
357
358 my $strs = [];
359
360 foreach my $param (@{$method->{params}}) {
361 if ($param->{page_param_number}) {
362 push(@$strs, ($indstr x $indent) . "\$params['$param->{name}'] = \$page;");
363 } elsif ($param->{page_param_offset}) {
364 my $orig = $param->{page_param_offset_origin} ? " + $param->{page_param_offset_origin}" : "";
365 push(@$strs, ($indstr x $indent) . "\$params['$param->{name}'] = (\$page - 1) * \$size$orig;");
366 } elsif ($param->{page_param_size}) {
367 push(@$strs, ($indstr x $indent) . "\$params['$param->{name}'] = \$size;");
368 }
369 }
370
371 my $str = join("\n", @$strs);
372 $str =~ s/^\s+//;
373
374 return $str;
375 }
376  
377 sub sort_keys {
378 my $hashref = shift;
379 return sort keys %$hashref;
380 }
381  
382 sub env_param {
383 return sub {
384 my $val = shift;
385 unless ($val =~ s/^\$(.*)$/getenv('$1')/) {
386 $val = "'$val'";
387 }
388 return $val;
389 };
390 }
391  
392 sub node_nullable {
393 my $node = shift;
394 my $path = $node->path;
395 foreach my $p (@$path) {
396 return 1 if $p->nullable;
397 }
398 return 0;
399 }
400  
401 sub node_access {
402 my $node = shift;
403 my $with_array = shift;
404 return "" if $node->depth < 2;
405 my $path = $node->path(2);
406 my $access = [""];
407 foreach my $p (@$path) {
408 push(@$access, $p->name);
409 $access->[-1] .= "[0]" if $p->multiple && ($with_array || $p != $node);
410 }
411 return join("->", @$access);
412 }
413  
414 sub author_name {
415 return sub {
416 my $author = shift;
417 my ($name, $email) = ($author =~ /^(.*?)\s+<(.*?)>$/);
418 my $user = (split(/\@/, $email))[0];
419 return $name;
420 };
421 }
422  
423 sub author_user {
424 return sub {
425 my $author = shift;
426 my ($name, $email) = ($author =~ /^(.*?)\s+<(.*?)>$/);
427 my $user = (split(/\@/, $email))[0];
428 return $user;
429 };
430 }
431  
432 sub author_email {
433 return sub {
434 my $author = shift;
435 my ($name, $email) = ($author =~ /^(.*?)\s+<(.*?)>$/);
436 my $user = (split(/\@/, $email))[0];
437 return $email;
438 };
439 }
440  
441 sub now_strftime {
442 my $fmt = shift;
443 return strftime($fmt, localtime());
444 }
445  
446 1;