lib/HTML/Mason/Component.pm

filter: Statementstmt 91.0% 143 / 157 Branchbran 74.2% 52 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 36 / 42 Podpod 66.6% 20 / 30 totaltotal 83.9% 251 / 299 SCAR 37.1
CC
37
Cov
86%
flag
12.0
parent
6.0
_determine_inheritance
6.0
CRAP 40.8
« lib/HTML/Mason/CGIHandler.pm ↑ Summary lib/HTML/Mason/Exceptions.pm »
1 # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved.
2 # This program is free software; you can redistribute it and/or modify it
3 # under the same terms as Perl itself.
4  
5 package HTML::Mason::Component;
6 31 $HTML::Mason::Component::VERSION = '1.59';
7 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use HTML::Mason::Exceptions( abbr => [qw(param_error)] );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use HTML::Mason::Tools qw(absolute_comp_path can_weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶Params::Validate::validation_options( on_fail => sub { param_error join '', @_ } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 31 ▶use HTML::Mason::Exceptions( abbr => ['error'] );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 use HTML::Mason::MethodMaker
17 31 ( read_only => [ qw( code
18 comp_id
19 compiler_id
20 declared_args
21 inherit_path
22 inherit_start_path
23 has_filter
24 load_time
25 ) ],
26  
27 read_write => [ [ dynamic_subs_request => { isa => 'HTML::Mason::Request' } ],
28 [ mfu_count => { type => SCALAR } ],
29 [ filter => { type => CODEREF } ],
30 ]
31 31 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # for reference later
34 #
35 # __PACKAGE__->valid_params
36 # (
37 # attr => {type => HASHREF, default => {}, public => 0},
38 # code => {type => CODEREF, public => 0, public => 0},
39 # load_time => {type => SCALAR, optional => 1, public => 0},
40 # declared_args => {type => HASHREF, default => {}, public => 0},
41 # dynamic_subs_init => {type => CODEREF, default => sub {}, public => 0},
42 # flags => {type => HASHREF, default => {}, public => 0},
43 # comp_id => {type => SCALAR, optional => 1, public => 0},
44 # methods => {type => HASHREF, default => {}, public => 0},
45 # mfu_count => {type => SCALAR, default => 0, public => 0},
46 # parser_version => {type => SCALAR, optional => 1, public => 0}, # allows older components to be instantied
47 # compiler_id => {type => SCALAR, optional => 1, public => 0},
48 # subcomps => {type => HASHREF, default => {}, public => 0},
49 # );
50 #
51  
52 my %defaults = ( attr => {},
53 declared_args => {},
54 ▶ dynamic_subs_init => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
55 31 flags => {},
56 methods => {},
57 mfu_count => 0,
58 subcomps => {},
59 );
60 sub new
61 {
62 784 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
63 784 my $self = bless { %defaults, @_ }, $class;
64  
65 # Initialize subcomponent and method properties: owner, name, and
66 # is_method flag.
67 784 ▶ while (my ($name,$c) = each(%{$self->{subcomps}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"subcomps"};}) 57 784 841
68 57 $c->assign_subcomponent_properties($self,$name,0);
69 57 ▶ Scalar::Util::weaken($c->{owner}) if can_weaken;
Branch: 1 branch
BranchTrueFalseTotal
if &can_weaken() 57 0 57
70 }
71 784 ▶ while (my ($name,$c) = each(%{$self->{methods}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"methods"};}) 64 784 848
72 64 $c->assign_subcomponent_properties($self,$name,1);
73 64 ▶ Scalar::Util::weaken($c->{owner}) if can_weaken;
Branch: 1 branch
BranchTrueFalseTotal
if &can_weaken() 64 0 64
74 }
75  
76 784 return $self;
77 }
78  
79 31 my $comp_count = 0;
80 sub assign_runtime_properties {
81 784 ▶ my ($self, $interp, $source) = @_;
Subroutine assign_runtime_properties: called CC=4 SCAR=13.9
Pod undocumented
82 784 $self->interp($interp);
83 784 ▶ $self->{comp_id} = defined $source->comp_id ? $source->comp_id : "[anon ". ++$comp_count . "]";
Branch: 1 branch
BranchTrueFalseTotal
defined $source->comp_id ? : 771 13 784
84  
85 784 $self->{path} = $source->comp_path;
86  
87 784 $self->_determine_inheritance;
88  
89 784 foreach my $c (values(%{$self->{subcomps}}), values(%{$self->{methods}})) {
90 121 $c->assign_runtime_properties($interp, $source);
91 }
92  
93 # Cache of uncanonicalized call paths appearing in the
94 # component. Used in $m->fetch_comp.
95 #
96 784 ▶ if ($interp->use_internal_component_caches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interp->use_internal_component_caches) 17 767 784
97 17 $self->{fetch_comp_cache} = {};
98 }
99 }
100  
101 sub flush_internal_caches
102 {
103 11 ▶ my ($self) = @_;
Subroutine flush_internal_caches: called CC=1 SCAR=0.0
Pod undocumented
104  
105 11 $self->{fetch_comp_cache} = {};
106 11 delete($self->{parent_cache});
107 }
108  
109 sub _determine_inheritance {
110 784 ▶ my $self = shift;
Subroutine _determine_inheritance: called CC=6 SCAR=17.9
111  
112 784 my $interp = $self->interp;
113  
114 # Assign inheritance properties
115 784 ▶ if (exists($self->{flags}->{inherit})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'flags'}{'inherit'}) { } 21 763 784
elsif ($interp->use_autohandlers) { } 762 1 763
116 21 ▶ if (defined($self->{flags}->{inherit})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"flags"}{"inherit"}) 2 19 21
117 2 $self->{inherit_path} = absolute_comp_path($self->{flags}->{inherit}, $self->dir_path);
118 }
119 } elsif ( $interp->use_autohandlers ) {
120 762 ▶ if ($self->name eq $interp->autohandler_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name eq $interp->autohandler_name) { } 15 747 762
121 15 ▶ unless ($self->dir_path eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dir_path eq "/") 15 0 15
122 15 ($self->{inherit_start_path}) = $self->dir_path =~ m,^(.*/)?.*,s
123 }
124 } else {
125 747 $self->{inherit_start_path} = $self->dir_path;
126 }
127 }
128 }
129  
130 sub run {
131 1278 ▶ my $self = shift;
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
132  
133 1278 $self->{mfu_count}++;
134  
135 1278 $self->{code}->(@_);
136 }
137  
138 sub dynamic_subs_init {
139 13 ▶ my $self = shift;
Subroutine dynamic_subs_init: called CC=3 SCAR=11.0
Pod undocumented
140  
141 error "cannot call a method or subcomponent from a <%shared> block"
142 13 ▶ if $self->{in_dynamic_subs_init};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_dynamic_subs_init"} 2 11 13
143  
144 11 local $self->{in_dynamic_subs_init} = 1;
145  
146 11 $self->{dynamic_subs_hash} = $self->{dynamic_subs_init}->();
147 error "could not process <%shared> section (does it contain a return()?)"
148 8 ▶ unless ref($self->{dynamic_subs_hash}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"dynamic_subs_hash"} eq "HASH" 0 8 8
149 }
150  
151 sub run_dynamic_sub {
152 17 ▶ my ($self, $key, @args) = @_;
Subroutine run_dynamic_sub: called CC=2 SCAR=7.1
Pod undocumented
153  
154 error "call_dynamic: assert error - could not find code for key $key in component " . $self->title
155 17 ▶ unless exists $self->{dynamic_subs_hash}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"dynamic_subs_hash"}{$key} 0 17 17
156  
157 17 return $self->{dynamic_subs_hash}->{$key}->(@args);
158 }
159  
160 # Legacy, left in for pre-0.8 obj files
161 ▶sub assign_subcomponent_properties {}
Subroutine assign_subcomponent_properties: not called CC=1 SCAR=0.0
Pod undocumented
162  
163 #
164 # By default components are not persistent.
165 #
166 0 ▶sub persistent { 0 }
Subroutine persistent: not called CC=1 SCAR=6.9
Pod undocumented
167  
168 #
169 # Only true in Subcomponent subclass.
170 #
171 427 ▶sub is_subcomp { 0 }
Subroutine is_subcomp: called CC=1 SCAR=0.0
Pod documented
172  
173 5 ▶sub is_method { 0 }
Subroutine is_method: called CC=1 SCAR=0.0
Pod documented
174  
175 #
176 # Only true in FileBased subclass.
177 #
178 3 ▶sub is_file_based { 0 }
Subroutine is_file_based: called CC=1 SCAR=0.0
Pod documented
179  
180 #
181 # Basic defaults for component designators: title, path, name, dir_path
182 #
183 1 ▶sub title { return $_[0]->{comp_id} }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
184 15 ▶sub name { return $_[0]->{comp_id} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
185 1 ▶sub path { return undef }
Subroutine path: called CC=1 SCAR=0.0
Pod documented
186 16 ▶sub dir_path { return undef }
Subroutine dir_path: called CC=1 SCAR=0.0
Pod documented
187  
188 #
189 # Get all subcomps or particular subcomp by name
190 #
191 sub subcomps {
192 481 ▶ my ($self,$key) = @_;
Subroutine subcomps: called CC=2 SCAR=6.9
Pod documented
193 481 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 473 8 481
194 473 return $self->{subcomps}->{$key};
195 } else {
196 8 return $self->{subcomps};
197 }
198 }
199  
200 #
201 # Get all methods or particular method by name
202 #
203 sub methods {
204 4 ▶ my ($self,$key) = @_;
Subroutine methods: called CC=2 SCAR=6.9
Pod documented
205 4 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 3 1 4
206 3 return $self->{methods}->{$key};
207 } else {
208 1 return $self->{methods};
209 }
210 }
211  
212 #
213 # Get all attributes
214 #
215 1 ▶sub attributes { $_[0]->{attr} }
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
216  
217 #
218 # Get attribute by name
219 #
220 sub attr {
221 49 ▶ my ($self,$name) = @_;
Subroutine attr: called CC=2 SCAR=7.4
Pod documented
222 49 my $value;
223 49 ▶ if ($self->_locate_inherited('attr',$name,\$value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_locate_inherited('attr', $name, \$value)) { } 49 0 49
224 49 return $value;
225 } else {
226 0 error "no attribute '$name' for component " . $self->title;
227 }
228 }
229  
230 sub attr_if_exists {
231 2 ▶ my ($self,$name) = @_;
Subroutine attr_if_exists: called CC=2 SCAR=6.9
Pod documented
232 2 my $value;
233 2 ▶ if ($self->_locate_inherited('attr',$name,\$value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_locate_inherited('attr', $name, \$value)) { } 1 1 2
234 1 return $value;
235 } else {
236 1 return undef;
237 }
238 }
239  
240 #
241 # Determine if particular attribute exists
242 #
243 sub attr_exists {
244 43 ▶ my ($self,$name) = @_;
Subroutine attr_exists: called CC=1 SCAR=0.0
Pod documented
245 43 return $self->_locate_inherited('attr',$name);
246 }
247  
248 #
249 # Call method by name
250 #
251 sub call_method {
252 5 ▶ my ($self,$name,@args) = @_;
Subroutine call_method: called CC=2 SCAR=7.4
Pod documented
253 5 my $method;
254 5 ▶ if ($self->_locate_inherited('methods',$name,\$method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_locate_inherited('methods', $name, \$method)) { } 5 0 5
255 5 HTML::Mason::Request->instance->comp({base_comp=>$self},$method,@args);
256 } else {
257 0 error "no method '$name' for component " . $self->title;
258 }
259 }
260  
261 #
262 # Like call method, but return component output.
263 #
264 sub scall_method {
265 1 ▶ my ($self,$name,@args) = @_;
Subroutine scall_method: called CC=2 SCAR=7.4
Pod documented
266 1 my $method;
267 1 ▶ if ($self->_locate_inherited('methods',$name,\$method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_locate_inherited('methods', $name, \$method)) { } 1 0 1
268 1 HTML::Mason::Request->instance->scomp({base_comp=>$self},$method,@args);
269 } else {
270 0 error "no method '$name' for component " . $self->title;
271 }
272 }
273  
274 #
275 # Determine if particular method exists
276 #
277 sub method_exists {
278 42 ▶ my ($self,$name) = @_;
Subroutine method_exists: called CC=1 SCAR=0.0
Pod documented
279 42 return $self->_locate_inherited('methods',$name);
280 }
281  
282 #
283 # Locate a component slot element following inheritance path
284 #
285 sub _locate_inherited {
286 215 ▶ my ($self,$field,$key,$ref) = @_;
Subroutine _locate_inherited: called CC=5 SCAR=16.1
287 215 my $count = 0;
288 215 ▶ for (my $comp = $self; $comp; $comp = $comp->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp) 285 16 301
289 285 ▶ if (exists($comp->{$field}->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $comp->{$field}{$key}) 199 86 285
290 199 ▶ $$ref = $comp->{$field}->{$key} if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 124 75 199
291 199 return 1;
292 }
293 86 ▶ error "inheritance chain length > 32 (infinite inheritance loop?)"
Branch: 1 branch
BranchTrueFalseTotal
if ++$count > 32 0 86 86
294 if ++$count > 32;
295 }
296 16 return 0;
297 }
298  
299 #
300 # Get particular flag by name
301 #
302 sub flag {
303 0 ▶ my ($self,$name) = @_;
Subroutine flag: not called CC=3 SCAR=24.8
Pod documented
304 0 my %flag_defaults =
305 (
306 );
307 0 ▶ if (exists($self->{flags}->{$name})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'flags'}{$name}) { } 0 0 0
elsif (exists $flag_defaults{$name}) { } 0 0 0
308 0 return $self->{flags}->{$name};
309 } elsif (exists($flag_defaults{$name})) {
310 0 return $flag_defaults{$name};
311 } else {
312 0 error "invalid flag: $name";
313 }
314 }
315  
316 #
317 # Return parent component according to inherit flag.
318 #
319 sub parent {
320 592 ▶ my ($self) = @_;
Subroutine parent: called CC=6 SCAR=18.0
Pod documented
321  
322 # Return cached value for parent, if any (may be undef)
323 #
324 592 ▶ return $self->{parent_cache} if exists($self->{parent_cache});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"parent_cache"} 0 592 592
325  
326 592 my $interp = $self->interp;
327 592 my $parent;
328 592 ▶ if ($self->inherit_path) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->inherit_path) { } 24 568 592
elsif ($self->inherit_start_path) { } 536 32 568
329 24 ▶ $parent = $interp->load($self->inherit_path)
Branch: 1 branch
BranchTrueFalseTotal
$parent = $interp->load($self->inherit_path) or error(sprintf("cannot find inherit path '%s' for component '%s'", $self->inherit_path, $self->title)) 0 24 24
330 or error(sprintf("cannot find inherit path '%s' for component '%s'",
331 $self->inherit_path, $self->title));
332 } elsif ($self->inherit_start_path) {
333 536 $parent = $interp->find_comp_upwards($self->inherit_start_path, $interp->autohandler_name);
334 }
335  
336 # Can only cache parent value if interp->{use_internal_component_caches} is on -
337 # see definition in Interp::_initialize.
338 #
339 592 ▶ if ($interp->use_internal_component_caches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interp->use_internal_component_caches) 7 585 592
340 7 $self->{parent_cache} = $parent;
341 }
342  
343 592 return $parent;
344 }
345  
346 sub interp {
347 2163 ▶ my $self = shift;
Subroutine interp: called CC=4 SCAR=14.3
Pod undocumented
348  
349 2163 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 784 1379 2163
elsif (not defined $self->{'interp'}) { } 0 1379 1379
350 784 validate_pos( @_, { isa => 'HTML::Mason::Interp' } );
351  
352 784 $self->{interp} = $_[0];
353  
354 784 ▶ Scalar::Util::weaken( $self->{interp} ) if can_weaken;
Branch: 1 branch
BranchTrueFalseTotal
if &can_weaken() 784 0 784
355 } elsif ( ! defined $self->{interp} ) {
356 0 die "The Interp object that this object contains has gone out of scope.\n";
357 }
358  
359 2163 return $self->{interp};
360 }
361  
362 #
363 # Accessors for various files associated with component
364 #
365 sub object_file {
366 3 ▶ my $self = shift;
Subroutine object_file: called CC=1 SCAR=0.0
Pod documented
367 3 return $self->interp->object_file($self);
368 }
369  
370 # For backwards compatibility with 1.0x
371 sub create_time {
372 0 ▶ my $self = shift;
Subroutine create_time: not called CC=1 SCAR=6.9
Pod documented
373 0 return $self->load_time(@_);
374 }
375  
376 # Create logger on demand - generally called from $m->log
377 sub logger {
378 2 ▶ my ($self) = @_;
Subroutine logger: called CC=2 SCAR=7.0
Pod undocumented
379  
380 2 ▶ if (!$self->{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"logger"}) 2 0 2
381 2 my $log_category = "HTML::Mason::Component" . $self->path();
382 2 $log_category =~ s/\//::/g;
383 2 $self->{logger} = Log::Any->get_logger(category => $log_category);
384 }
385 2 return $self->{logger};
386 }
387  
388 31 1;
389  
390 __END__
« lib/HTML/Mason/CGIHandler.pm ↑ Summary lib/HTML/Mason/Exceptions.pm »