Filename | /home/ocbnet/domain/ocbnet.ch/vhost/webmerge/htdocs/webmerge/scripts/modules/RTP/Webmerge/Embedder/JS.pm |
Statements | Executed 15 statements in 5.95ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 10.8ms | 99.6ms | BEGIN@20 | RTP::Webmerge::Embedder::JS::
1 | 1 | 1 | 95µs | 368µs | BEGIN@8 | RTP::Webmerge::Embedder::JS::
1 | 1 | 1 | 64µs | 262µs | BEGIN@23 | RTP::Webmerge::Embedder::JS::
1 | 1 | 1 | 63µs | 99µs | BEGIN@10 | RTP::Webmerge::Embedder::JS::
1 | 1 | 1 | 54µs | 143µs | BEGIN@9 | RTP::Webmerge::Embedder::JS::
1 | 1 | 1 | 50µs | 271µs | BEGIN@26 | RTP::Webmerge::Embedder::JS::
1 | 1 | 1 | 24µs | 24µs | BEGIN@15 | RTP::Webmerge::Embedder::JS::
0 | 0 | 0 | 0s | 0s | embedder | RTP::Webmerge::Embedder::JS::
0 | 0 | 0 | 0s | 0s | is_enabled | RTP::Webmerge::Embedder::JS::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | ################################################################################################### | ||||
2 | # Copyright 2013 by Marcel Greter | ||||
3 | # This file is part of Webmerge (GPL3) | ||||
4 | ################################################################################################### | ||||
5 | package RTP::Webmerge::Embedder::JS; | ||||
6 | ################################################################################################### | ||||
7 | |||||
8 | 2 | 163µs | 2 | 640µs | # spent 368µs (95+273) within RTP::Webmerge::Embedder::JS::BEGIN@8 which was called:
# once (95µs+273µs) by RTP::Webmerge::Embedder::BEGIN@252 at line 8 # spent 368µs making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@8
# spent 273µs making 1 call to Exporter::import |
9 | 2 | 373µs | 2 | 233µs | # spent 143µs (54+90) within RTP::Webmerge::Embedder::JS::BEGIN@9 which was called:
# once (54µs+90µs) by RTP::Webmerge::Embedder::BEGIN@252 at line 9 # spent 143µs making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@9
# spent 90µs making 1 call to strict::import |
10 | 2 | 203µs | 2 | 136µs | # spent 99µs (63+36) within RTP::Webmerge::Embedder::JS::BEGIN@10 which was called:
# once (63µs+36µs) by RTP::Webmerge::Embedder::BEGIN@252 at line 10 # spent 99µs making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@10
# spent 36µs making 1 call to warnings::import |
11 | |||||
12 | ################################################################################################### | ||||
13 | |||||
14 | # define our version string | ||||
15 | 1 | 142µs | 1 | 24µs | # spent 24µs within RTP::Webmerge::Embedder::JS::BEGIN@15 which was called:
# once (24µs+0s) by RTP::Webmerge::Embedder::BEGIN@252 at line 15 # spent 24µs making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@15 |
16 | |||||
17 | ################################################################################################### | ||||
18 | |||||
19 | # use module to dump js data | ||||
20 | 2 | 768µs | 2 | 100ms | # spent 99.6ms (10.8+88.8) within RTP::Webmerge::Embedder::JS::BEGIN@20 which was called:
# once (10.8ms+88.8ms) by RTP::Webmerge::Embedder::BEGIN@252 at line 20 # spent 99.6ms making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@20
# spent 886µs making 1 call to JSON::import |
21 | |||||
22 | # load our local modules | ||||
23 | 2 | 190µs | 2 | 460µs | # spent 262µs (64+198) within RTP::Webmerge::Embedder::JS::BEGIN@23 which was called:
# once (64µs+198µs) by RTP::Webmerge::Embedder::BEGIN@252 at line 23 # spent 262µs making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@23
# spent 198µs making 1 call to Exporter::import |
24 | |||||
25 | # end of line in code | ||||
26 | 2 | 4.08ms | 2 | 492µs | # spent 271µs (50+221) within RTP::Webmerge::Embedder::JS::BEGIN@26 which was called:
# once (50µs+221µs) by RTP::Webmerge::Embedder::BEGIN@252 at line 26 # spent 271µs making 1 call to RTP::Webmerge::Embedder::JS::BEGIN@26
# spent 221µs making 1 call to constant::import |
27 | |||||
28 | ################################################################################################### | ||||
29 | |||||
30 | sub is_enabled ($) | ||||
31 | { | ||||
32 | return 0 unless (defined $_[0]); | ||||
33 | return 1 if (lc $_[0] eq 'true'); | ||||
34 | return 1 if (lc $_[0] eq 'enabled'); | ||||
35 | return 0; | ||||
36 | } | ||||
37 | |||||
38 | sub embedder | ||||
39 | { | ||||
40 | |||||
41 | # get variables and collections from parent | ||||
42 | my ($domains, $contents, $features, $detects, $config) = @_; | ||||
43 | |||||
44 | # create switcher code | ||||
45 | my $switcher = ''; | ||||
46 | |||||
47 | # process all header detect entries | ||||
48 | foreach my $detect (@{$detects || [] }) | ||||
49 | { | ||||
50 | |||||
51 | # get options for this detection | ||||
52 | my $target = $detect->{'target'}; | ||||
53 | my $enabled = $detect->{'enabled'}; | ||||
54 | my $disabled = $detect->{'disabled'}; | ||||
55 | |||||
56 | # class is a reserved keyword in js | ||||
57 | $target = 'klass' if $target eq 'class'; | ||||
58 | |||||
59 | unless ( defined $detect->{'feature'} ) | ||||
60 | { die 'detect without feature id found'; } | ||||
61 | |||||
62 | my $id = $detect->{'feature'}; | ||||
63 | my $feature = $features->{$id}; | ||||
64 | |||||
65 | unless ($feature || is_enabled($detect->{'optional'})) | ||||
66 | { die "feature detection <$id> not available"; } | ||||
67 | |||||
68 | $switcher .= ' ' . '// default feature setting' . EOL; | ||||
69 | $switcher .= ' ' . 'var enabled = null;' . EOL; | ||||
70 | |||||
71 | # process all header detect entries | ||||
72 | foreach my $test (@{$feature->{'test'} || [] }) | ||||
73 | { | ||||
74 | |||||
75 | my $types = lc $test->{'type'}; | ||||
76 | |||||
77 | foreach my $type (split(/\s*,\s*/, $types)) | ||||
78 | { | ||||
79 | |||||
80 | my $var; | ||||
81 | |||||
82 | unless (defined $type) { $var = 'webmerge.SERVER'; } | ||||
83 | elsif ($type eq 'env') { $var = 'webmerge.ENV'; } | ||||
84 | elsif ($type eq 'get') { $var = 'webmerge.GET'; } | ||||
85 | elsif ($type eq 'post') { $var = 'webmerge.POST'; } | ||||
86 | elsif ($type eq 'server') { $var = 'webmerge.SERVER'; } | ||||
87 | elsif ($type eq 'cookie') { $var = 'webmerge.COOKIE'; } | ||||
88 | elsif ($type eq 'session') { $var = 'webmerge.SESSION'; } | ||||
89 | elsif ($type eq 'request') { $var = 'webmerge.REQUEST'; } | ||||
90 | else { die "Fatal: unknown test type <$type>"; } | ||||
91 | |||||
92 | my ($enabled, $disabled) = ('true', 'false'); | ||||
93 | |||||
94 | $switcher .= EOL; | ||||
95 | $switcher .= " // make sure the variable exists" . EOL; | ||||
96 | $switcher .= " if (typeof $var == 'undefined') $var = {};" . EOL; | ||||
97 | |||||
98 | if (exists $test->{'enable'} && scalar(@{$test->{'enable'}})) | ||||
99 | { | ||||
100 | $enabled = '(' . $var . '[\'' . $test->{'key'} . '\'] || \'\').' . | ||||
101 | 'match(/^(?:' . join( '|', @{$test->{'enable'}} ) . ')$/)'; | ||||
102 | } | ||||
103 | |||||
104 | if (exists $test->{'disable'} && scalar(@{$test->{'disable'}})) | ||||
105 | { | ||||
106 | $disabled = '(' . $var . '[\'' . $test->{'key'} . '\'] || \'\').' . | ||||
107 | 'match(/^(?:' . join( '|', @{$test->{'disable'}} ) . ')$/)'; | ||||
108 | } | ||||
109 | |||||
110 | $switcher .= EOL; | ||||
111 | $switcher .= ' ' . ' // sniff for useragent' . EOL; | ||||
112 | $switcher .= ' ' . 'if (\'' . $test->{'key'} . '\' in ' . $var . ') {' . EOL; | ||||
113 | $switcher .= ' ' . 'enabled = ( enabled || ( ' . $enabled . ' )) && ! ' . $disabled . ';' . EOL; | ||||
114 | $switcher .= ' ' . '}' . EOL; | ||||
115 | |||||
116 | } | ||||
117 | |||||
118 | } | ||||
119 | |||||
120 | $switcher .= EOL; | ||||
121 | $switcher .= ' ' . ' // change ' . $target . ' if feature is enabled' . EOL; | ||||
122 | $switcher .= ' ' . 'if (enabled === true) { ' . $target .' = \'' . $enabled . '\'; }'. EOL if (defined $enabled); | ||||
123 | $switcher .= ' ' . 'if (enabled === false) { ' . $target .' = \'' . $disabled . '\'; }'. EOL if (defined $disabled); | ||||
124 | |||||
125 | }; | ||||
126 | # EO each detect | ||||
127 | |||||
128 | # load the template (readfile will resolve path) | ||||
129 | my $tmpl = readfile($config->{'tmpl-embed-js'}); | ||||
130 | |||||
131 | # create data for template | ||||
132 | my %data = | ||||
133 | ( | ||||
134 | 'switcher' => $switcher, | ||||
135 | 'includes' => to_json( | ||||
136 | $contents, | ||||
137 | { pretty => 1 } | ||||
138 | ), | ||||
139 | ); | ||||
140 | |||||
141 | # insert data into loaded template | ||||
142 | ${$tmpl} =~ s/%%([a-z]+)%%/$data{$1}/eg; | ||||
143 | |||||
144 | # return tmpl pointer | ||||
145 | return $tmpl; | ||||
146 | |||||
147 | } | ||||
148 | # EO sub embedder | ||||
149 | |||||
150 | ################################################################################################### | ||||
151 | |||||
152 | # register this embedder type to parent module | ||||
153 | 1 | 20µs | 1 | 37µs | RTP::Webmerge::Embedder::register('js', \&embedder); # spent 37µs making 1 call to RTP::Webmerge::Embedder::register |
154 | |||||
155 | ################################################################################################### | ||||
156 | ################################################################################################### | ||||
157 | 1 | 16µs | 1; |