Wire.pm

filter: Statementstmt 92.8% 440 / 474 Branchbran 82.3% 168 / 204 Conditioncond 70.0% 82 / 117 MC/DCmcdc 53.0% 35 / 66 Subroutinesub 100% 55 / 55 Podpod 85.7% 12 / 14 totaltotal 85.1% 792 / 930 CC 152 SCAR 32.1
CC
152
Cov
87%
validate
122.3
create_service
37.5
parse_args
16.6
CRAP 24.7
↑ Summary Wire/Event/BuildService.pm »
1 package Beam::Wire;
2 29 our $VERSION = '1.031';
3 # ABSTRACT: Lightweight Dependency Injection Container
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # wire.yml
8 #pod captain:
9 #pod class: Person
10 #pod args:
11 #pod name: Malcolm Reynolds
12 #pod rank: Captain
13 #pod first_officer:
14 #pod $class: Person
15 #pod name: Zoƫ Alleyne Washburne
16 #pod rank: Commander
17 #pod
18 #pod # script.pl
19 #pod use Beam::Wire;
20 #pod my $wire = Beam::Wire->new( file => 'wire.yml' );
21 #pod my $captain = $wire->get( 'captain' );
22 #pod print $captain->name; # "Malcolm Reynolds"
23 #pod
24 #pod =head1 DESCRIPTION
25 #pod
26 #pod Beam::Wire is a configuration module and a dependency injection
27 #pod container. In addition to complex data structures, Beam::Wire configures
28 #pod and creates plain old Perl objects.
29 #pod
30 #pod A dependency injection (DI) container creates an inversion of control:
31 #pod Instead of manually creating all the dependent objects (also called
32 #pod "services") before creating the main object that we actually want, a DI
33 #pod container handles that for us: We describe the relationships between
34 #pod objects, and the objects get built as needed.
35 #pod
36 #pod Dependency injection is sometimes called the opposite of garbage
37 #pod collection. Rather than ensure objects are destroyed in the right order,
38 #pod dependency injection makes sure objects are created in the right order.
39 #pod
40 #pod Using Beam::Wire in your application brings great flexibility,
41 #pod allowing users to easily add their own code to customize how your
42 #pod project behaves.
43 #pod
44 #pod For an L<introduction to the Beam::Wire service configuration format,
45 #pod see Beam::Wire::Help::Config|Beam::Wire::Help::Config>.
46 #pod
47 #pod =cut
48  
49 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 29 ▶use constant DEBUG => $ENV{BEAM_WIRE_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 29 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
55 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 29 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 29 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
58 29 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
59 29 ▶use Types::Standard qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
60 29 ▶use if DEBUG, 'Data::Dumper' => qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
61 29 ▶use Beam::Wire::Event::ConfigService;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 29 ▶use Beam::Wire::Event::BuildService;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 29 with 'Beam::Emitter';
64  
65 #pod =attr file
66 #pod
67 #pod The path of the file where services are configured (typically a YAML
68 #pod file). The file's contents should be a single hashref. The keys are
69 #pod service names, and the values are L<service
70 #pod configurations|Beam::Wire::Help::Config>.
71 #pod
72 #pod =cut
73  
74 has file => (
75 is => 'ro',
76 isa => InstanceOf['Path::Tiny'],
77 31 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
78 31 ▶ if ( !blessed $_[0] || !$_[0]->isa('Path::Tiny') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny")) 10 21 31
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny")
!ll&&!rl&&r
10021
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny")
builtin::blessed($_[0]) $_[0]->isa("Path::Tiny")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Path::Tiny")
ABresult
0X0
100
111
79 10 return path( $_[0] );
80 }
81 21 return $_[0];
82 },
83 29 );
84  
85 #pod =attr dir
86 #pod
87 #pod The directory path or paths to use when searching for inner container files.
88 #pod Defaults to using the directory which contains the file specified by the
89 #pod L<file attribute|/file> followed by the C<BEAM_PATH> environment variable
90 #pod (separated by colons C<:>).
91 #pod
92 #pod =cut
93  
94 has dir => (
95 is => 'ro',
96 isa => ArrayRef[InstanceOf['Path::Tiny']],
97 lazy => 1,
98 11 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
99 my $dir = [
100 ($_[0]->file ? ($_[0]->file->parent) : ()),
101 11 ▶ ($ENV{BEAM_PATH} ? (map { path($_) } grep !!$_, split /:/, $ENV{BEAM_PATH}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$_[0]->file ? : 6 5 11
$ENV{'BEAM_PATH'} ? : 1 10 11
102 ];
103 11 ; print 'Using default paths ', Dumper $dir if DEBUG;
104 11 return $dir;
105 },
106 35 ▶ coerce => sub {
Subroutine __ANON__: called CC=5 SCAR=19.6
107 35 ▶ if ( !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 35 35
108 0 return [path( $_[0] )];
109 }
110 35 ▶ if ( ref $_[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "ARRAY") 35 0 35
111 35 ▶ return [map { blessed( $_ ) && $_->isa('Path::Tiny') ? $_ : path($_) } @{$_[0]}];
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('Path::Tiny') ? : 25 0 25
Condition33%1/3
builtin::blessed($_) and $_->isa("Path::Tiny")
!ll&&!rl&&r
0025
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Path::Tiny")
builtin::blessed($_) $_->isa("Path::Tiny")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->isa("Path::Tiny")
ABresult
0X0
100
111
112 }
113 0 return $_[0];
114 },
115 29 );
116  
117 #pod =attr config
118 #pod
119 #pod The raw configuration data. By default, this data is loaded by
120 #pod L<Config::Any|Config::Any> using the file specified by the L<file attribute|/file>.
121 #pod
122 #pod See L<Beam::Wire::Help::Config for details on what the configuration
123 #pod data structure looks like|Beam::Wire::Help::Config>.
124 #pod
125 #pod If you don't want to load a file, you can specify this attribute in the
126 #pod Beam::Wire constructor.
127 #pod
128 #pod =cut
129  
130 29 has config => (
131 is => 'ro',
132 isa => HashRef,
133 lazy => 1,
134 builder => 1
135 );
136  
137 32 ▶sub _build_config {
Subroutine _build_config: called CC=2 SCAR=6.9
138 32 my ( $self ) = @_;
139 32 ▶ return {} if ( !$self->file );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->file 5 27 32
140 27 return $self->_load_config( $self->file );
141 }
142  
143 #pod =attr services
144 #pod
145 #pod A hashref of cached services built from the L<configuration|/config>. If
146 #pod you want to inject a pre-built object for other services to depend on,
147 #pod add it here.
148 #pod
149 #pod =cut
150  
151 29 has services => (
152 is => 'ro',
153 isa => HashRef,
154 lazy => 1,
155 builder => 1,
156 );
157  
158 113 ▶sub _build_services {
Subroutine _build_services: called CC=1 SCAR=0.0
159 113 my ( $self ) = @_;
160 113 my $services = {};
161 113 return $services;
162 }
163  
164 #pod =attr meta_prefix
165 #pod
166 #pod The character that begins a meta-property inside of a service's C<args>. This
167 #pod includes C<$ref>, C<$class>, C<$method>, and etc...
168 #pod
169 #pod The default value is C<$>. The empty string is allowed.
170 #pod
171 #pod =cut
172  
173 has meta_prefix => (
174 is => 'ro',
175 isa => Str,
176 127 ▶ default => sub { q{$} },
Subroutine __ANON__: called CC=1 SCAR=0.0
177 29 );
178  
179 #pod =method get
180 #pod
181 #pod my $service = $wire->get( $name );
182 #pod my $service = $wire->get( $name, %overrides )
183 #pod
184 #pod The get method resolves and returns the service named C<$name>, creating
185 #pod it, if necessary, with L<the create_service method|/create_service>.
186 #pod
187 #pod C<%overrides> is an optional list of name-value pairs. If specified,
188 #pod get() will create an new, anonymous service that extends the named
189 #pod service with the given config overrides. For example:
190 #pod
191 #pod # test.pl
192 #pod use Beam::Wire;
193 #pod my $wire = Beam::Wire->new(
194 #pod config => {
195 #pod foo => {
196 #pod args => {
197 #pod text => 'Hello, World!',
198 #pod },
199 #pod },
200 #pod },
201 #pod );
202 #pod
203 #pod my $foo = $wire->get( 'foo', args => { text => 'Hello, Chicago!' } );
204 #pod print $foo; # prints "Hello, Chicago!"
205 #pod
206 #pod This allows you to create factories out of any service, overriding service
207 #pod configuration at run-time.
208 #pod
209 #pod If C<$name> contains a slash (C</>) character (e.g. C<foo/bar>), the left
210 #pod side (C<foo>) will be used as the name of an inner container, and the
211 #pod right side (C<bar>) is a service inside that container. For example,
212 #pod these two lines are equivalent:
213 #pod
214 #pod $bar = $wire->get( 'foo/bar' );
215 #pod $bar = $wire->get( 'foo' )->get( 'bar' );
216 #pod
217 #pod Inner containers can be nested as deeply as desired (C<foo/bar/baz/fuzz>).
218 #pod
219 #pod =cut
220  
221 323 ▶sub get {
Subroutine get: called CC=12 SCAR=24.9
Pod documented
222 323 my ( $self, $name, %override ) = @_;
223  
224 323 ; print STDERR "Get service: $name\n" if DEBUG;
225  
226 323 ▶ if ( index( $name, q{/} ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($name, "/") != -1) 39 284 323
227 39 my ( $container_name, $service_name ) = split m{/}, $name, 2;
228 39 my $container = $self->get( $container_name );
229  
230 # This could be a Beam::Wire container, or it could be a plain hashref.
231 # If it's a hashref, we can automatically create a container and then use it.
232 39 ▶ if (ref $container eq 'HASH' && !(blessed $container and $container->isa('Beam::Wire'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $container eq "HASH" and not builtin::blessed($container) && $container->isa("Beam::Wire")) 4 35 39
Condition50%3/6
ref $container eq "HASH" and not builtin::blessed($container) && $container->isa("Beam::Wire")
!ll&&!rl&&r
3504
builtin::blessed($container) && $container->isa('Beam::Wire')
!ll&&!rl&&r
400
MC/DC33%1/3
ref $container eq "HASH" and not builtin::blessed($container) && $container->isa("Beam::Wire")
ref $container eq "HASH" builtin::blessed($container) $container->isa('Beam::Wire')
Truth table50%2/4
A and not(B && C)
A= ref $container eq "HASH"B= builtin::blessed($container)C= $container->isa('Beam::Wire')
ABCresult
0XX0
1110
10X1
1101
233 4 my $inner_container = Beam::Wire->new(
234 config => $container,
235 );
236 # Do not cache the inner container for later use, in case someone
237 # also tries to use the parent as a bare hashref.
238 4 $container = $inner_container;
239 }
240 39 return $self->_get_from_inner_container($container, $container_name, $service_name, %override);
241 }
242  
243 284 ▶ if ( keys %override ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %override) 4 280 284
244 4 return $self->create_service(
245 "\$anonymous extends $name",
246 %override,
247 extends => $name,
248 );
249 }
250  
251 280 my $service = $self->services->{$name};
252 280 ▶ if ( !$service ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service) 174 106 280
253 174 ; printf STDERR 'Service "%s" does not exist. Creating.' . "\n", $name if DEBUG;
254  
255 174 my $config_ref = $self->get_config($name);
256 174 ▶ unless ( $config_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config_ref) 2 172 174
257 2 Beam::Wire::Exception::NotFound->throw(
258 name => $name,
259 file => $self->file,
260 );
261 }
262  
263 172 ; print STDERR "Got service config: " . Dumper( $config_ref ) if DEBUG;
264  
265 172 ▶ if ( ref $config_ref eq 'HASH' && $self->is_meta( $config_ref, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_ref eq 'HASH' and $self->is_meta($config_ref, 1)) { } 156 16 172
Condition100%3/3
ref $config_ref eq "HASH" and $self->is_meta($config_ref, 1)
!ll&&!rl&&r
106156
MC/DC100%2/2
ref $config_ref eq "HASH" and $self->is_meta($config_ref, 1)
ref $config_ref eq "HASH" $self->is_meta($config_ref, 1)
Truth table100%3/3
A and B
A= ref $config_ref eq "HASH"B= $self->is_meta($config_ref, 1)
ABresult
0X0
100
111
266 156 my %config = %{ $self->normalize_config( $config_ref ) };
267 156 $service = $self->create_service( $name, %config );
268 146 ▶ if ( !$config{lifecycle} || lc $config{lifecycle} ne 'factory' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config{"lifecycle"} or lc $config{"lifecycle"} ne "factory") 141 5 146
Condition100%3/3
not $config{"lifecycle"} or lc $config{"lifecycle"} ne "factory"
l!l&&r!l&&!r
13925
MC/DC100%2/2
not $config{"lifecycle"} or lc $config{"lifecycle"} ne "factory"
not $config{"lifecycle"} lc $config{"lifecycle"} ne "factory"
Truth table100%3/3
not(A) or B
A= not $config{"lifecycle"}B= lc $config{"lifecycle"} ne "factory"
ABresult
1X1
011
000
269 141 $self->services->{$name} = $service;
270 }
271 }
272 else {
273 16 $self->services->{$name} = $service = $self->find_refs( $name, $config_ref );
274 }
275 }
276  
277 268 ; print STDERR "Returning service: " . Dumper( $service ) if DEBUG;
278  
279 268 return $service;
280 }
281  
282 39 ▶sub _get_from_inner_container {
Subroutine _get_from_inner_container: called CC=1 SCAR=0.0
283 39 my ($self, $container, $container_name, $service_name, %override) = @_;
284  
285 # Bubble up the events from the inner container
286 17 ▶ my $unsub_config = $container->on( configure_service => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
287 17 my ( $event ) = @_;
288 17 $self->emit( configure_service =>
289 class => 'Beam::Wire::Event::ConfigService',
290 service_name => join( '/', $container_name, $event->service_name ),
291 config => $event->config,
292 );
293 39 } );
294 17 ▶ my $unsub_build = $container->on( build_service => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
295 17 my ( $event ) = @_;
296 17 $self->emit( build_service =>
297 class => 'Beam::Wire::Event::BuildService',
298 service_name => join( '/', $container_name, $event->service_name ),
299 service => $event->service,
300 );
301 39 } );
302  
303 39 my $service = $container->get( $service_name, %override );
304  
305 39 $unsub_config->();
306 39 $unsub_build->();
307  
308 39 return $service;
309 }
310  
311 #pod =method set
312 #pod
313 #pod $wire->set( $name => $service );
314 #pod
315 #pod The set method configures and stores the specified C<$service> with the
316 #pod specified C<$name>. Use this to add or replace built services.
317 #pod
318 #pod Like L<the get() method, above|/get>, C<$name> can contain a slash (C</>)
319 #pod character to traverse through nested containers.
320 #pod
321 #pod =cut
322  
323 ## no critic ( ProhibitAmbiguousNames )
324 # This was named set() before I started using Perl::Critic, and will
325 # continue to be named set() now that I no longer use Perl::Critic
326 2 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
327 2 my ( $self, $name, $service ) = @_;
328 2 ▶ if ( $name =~ q{/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[/]) 1 1 2
329 1 my ( $container_name, $service_name ) = split m{/}, $name, 2;
330 1 return $self->get( $container_name )->set( $service_name, $service );
331 }
332 1 $self->services->{$name} = $service;
333 1 return;
334 }
335  
336 #pod =method get_config
337 #pod
338 #pod my $conf = $wire->get_config( $name );
339 #pod
340 #pod Get the config with the given C<$name>. Like L<the get() method,
341 #pod above|/get>, C<$name> can contain slash (C</>) characters to traverse
342 #pod through nested containers.
343 #pod
344 #pod =cut
345  
346 236 ▶sub get_config {
Subroutine get_config: called CC=2 SCAR=6.9
Pod documented
347 236 my ( $self, $name ) = @_;
348 236 ▶ if ( $name =~ q{/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[/]) 4 232 236
349 4 my ( $container_name, $service ) = split m{/}, $name, 2;
350 4 my %inner_config = %{ $self->get( $container_name )->get_config( $service ) };
351 # Fix relative references to prefix the container name
352 4 my ( $fixed_config ) = $self->fix_refs( $container_name, \%inner_config );
353 4 return $fixed_config;
354 }
355 232 return $self->config->{$name};
356 }
357  
358 #pod =method normalize_config
359 #pod
360 #pod my $out_conf = $self->normalize_config( $in_conf );
361 #pod
362 #pod Normalize the given C<$in_conf> into to hash that L<the create_service
363 #pod method|/create_service> expects. This method allows a service to be
364 #pod defined with prefixed meta-names (C<$class> instead of C<class>) and
365 #pod the arguments specified without prefixes.
366 #pod
367 #pod For example, these two services are identical.
368 #pod
369 #pod foo:
370 #pod class: Foo
371 #pod args:
372 #pod fizz: buzz
373 #pod
374 #pod foo:
375 #pod $class: Foo
376 #pod fizz: buzz
377 #pod
378 #pod The C<$in_conf> must be a hash, and must already pass L<an is_meta
379 #pod check|/is_meta>.
380 #pod
381 #pod =cut
382  
383 201 ▶sub normalize_config {
Subroutine normalize_config: called CC=4 SCAR=13.9
Pod documented
384 201 my ( $self, $conf ) = @_;
385  
386 201 ; print STDERR "In conf: " . Dumper( $conf ) if DEBUG;
387  
388 201 my %meta = reverse $self->get_meta_names;
389  
390 # Confs without prefixed keys can be used as-is
391 201 ▶ return $conf if !grep { $meta{ $_ } } keys %$conf;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$meta{$_};} keys %$conf 178 23 201
392  
393 23 my %out_conf;
394 23 for my $key ( keys %$conf ) {
395 39 ▶ if ( $meta{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta{$key}) { } 30 9 39
396 30 $out_conf{ $meta{ $key } } = $conf->{ $key };
397 }
398 else {
399 9 $out_conf{ args }{ $key } = $conf->{ $key };
400 }
401 }
402  
403 23 ; print STDERR "Out conf: " . Dumper( \%out_conf ) if DEBUG;
404  
405 23 return \%out_conf;
406 }
407  
408 #pod =method create_service
409 #pod
410 #pod my $service = $wire->create_service( $name, %config );
411 #pod
412 #pod Create the service with the given C<$name> and C<%config>. Config can
413 #pod contain the following keys:
414 #pod
415 #pod =over 4
416 #pod
417 #pod =item class
418 #pod
419 #pod The class name of an object to create. Can be combined with C<version>,
420 #pod C<method>, and C<args>. An object of any class can be created with Beam::Wire.
421 #pod
422 #pod =item version
423 #pod
424 #pod The minimum version required for a class based service. Has to be combined with
425 #pod C<class>.
426 #pod
427 #pod =item args
428 #pod
429 #pod The arguments to the constructor method. Used with C<class> and
430 #pod C<method>. Can be a simple value, or a reference to an array or
431 #pod hash which will be dereferenced and passed in to the constructor
432 #pod as a list.
433 #pod
434 #pod If the C<class> consumes the L<Beam::Service role|Beam::Service>,
435 #pod the service's C<name> and C<container> will be added to the C<args>.
436 #pod
437 #pod =item method
438 #pod
439 #pod The method to call to create the object. Only used with C<class>.
440 #pod Defaults to C<"new">.
441 #pod
442 #pod This can also be an array of hashes which describe a list of methods
443 #pod that will be called on the object. The first method should create the
444 #pod object, and each subsequent method can be used to modify the object. The
445 #pod hashes should contain a C<method> key, which is a string containing the
446 #pod method to call, and optionally C<args> and C<return> keys. The C<args>
447 #pod key works like the top-level C<args> key, above. The optional C<return>
448 #pod key can have the special value C<"chain">, which will use the return
449 #pod value from the method as the value for the service (L<The tutorial shows
450 #pod examples of this|Beam::Wire::Help::Config/Multiple Constructor
451 #pod Methods>).
452 #pod
453 #pod If an array is used, the top-level C<args> key is not used.
454 #pod
455 #pod =item value
456 #pod
457 #pod The value of this service. Can be a simple value, or a reference to an
458 #pod array or hash. This value will be simply returned by this method, and is
459 #pod mostly useful when using container files.
460 #pod
461 #pod C<value> can not be used with C<class> or C<extends>.
462 #pod
463 #pod =item ref
464 #pod
465 #pod A reference to another service. This may be paired with C<call> or C<path>.
466 #pod
467 #pod =item config
468 #pod
469 #pod The path to a configuration file, relative to L<the dir attribute|/dir>.
470 #pod The file will be read with L<Config::Any>, and the resulting data
471 #pod structure returned. If the config file does not exist, will return the
472 #pod data in the C<$default> attribute. If the C<$default> attribute does not
473 #pod exist, will return C<undef>.
474 #pod
475 #pod C<config> can not be used with C<class> or C<extends>.
476 #pod
477 #pod =item env
478 #pod
479 #pod Get the value from an environment variable. If the environment variable does not exist, will return the
480 #pod data in the C<$default> attribute. If the C<$default> attribute does not exist, will return C<undef>.
481 #pod
482 #pod C<env> can not be used with C<class> or C<extends>.
483 #pod
484 #pod =item extends
485 #pod
486 #pod The name of a service to extend. The named service's configuration will
487 #pod be merged with this configuration (via L<the merge_config
488 #pod method|/merge_config>).
489 #pod
490 #pod This can be used in place of the C<class> key if the extended configuration
491 #pod contains a class.
492 #pod
493 #pod =item with
494 #pod
495 #pod Compose a role into the object's class before creating the object. This
496 #pod can be a single string, or an array reference of strings which are roles
497 #pod to combine.
498 #pod
499 #pod This uses L<Moo::Role|Moo::Role> and L<the create_class_with_roles
500 #pod method|Role::Tiny/create_class_with_roles>, which should work with any
501 #pod class (as it uses L<the Role::Tiny module|Role::Tiny> under the hood).
502 #pod
503 #pod This can be used with the C<class> key.
504 #pod
505 #pod =item on
506 #pod
507 #pod Attach an event handler to a L<Beam::Emitter subclass|Beam::Emitter>. This
508 #pod is an array of hashes of event names and handlers. A handler is made from
509 #pod a service reference (C<$ref> or an anonymous service), and a subroutine to
510 #pod call on that service (C<$sub>).
511 #pod
512 #pod For example:
513 #pod
514 #pod emitter:
515 #pod class: My::Emitter
516 #pod on:
517 #pod - my_event:
518 #pod $ref: my_handler
519 #pod $sub: on_my_event
520 #pod
521 #pod This can be used with the C<class> key.
522 #pod
523 #pod =back
524 #pod
525 #pod This method uses L<the parse_args method|/parse_args> to parse the C<args> key,
526 #pod L<resolving references|resolve_ref> as needed.
527 #pod
528 #pod =cut
529  
530 175 ▶sub create_service {
Subroutine create_service: called CC=37 SCAR=36.3
Pod documented
531 175 my ( $self, $name, %service_info ) = @_;
532  
533 175 ; print STDERR "Creating service: " . Dumper( \%service_info ) if DEBUG;
534  
535 # Compose the parent ref into the copy, in case the parent changes
536 175 %service_info = $self->merge_config( %service_info );
537  
538 # value | ref | config and class/extends are mutually exclusive
539 # must check after merge_config in case parent config has class/value
540  
541 173 my @classy = grep { exists $service_info{$_} } qw( class extends );
542 173 my @other = grep { exists $service_info{$_} } qw( value ref config env );
543  
544 173 ▶ if ( @other > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other > 1) 0 173 173
545 0 Beam::Wire::Exception::InvalidConfig->throw(
546 name => $name,
547 file => $self->file,
548 error => 'use only one of "value", "ref", "env", or "config"',
549 );
550 }
551  
552 173 ▶ if ( @classy && @other ) { # @other == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@classy and @other) 6 167 173
Condition100%3/3
@classy and @other
!ll&&!rl&&r
151526
MC/DC100%2/2
@classy and @other
@classy @other
Truth table100%3/3
A and B
A= @classyB= @other
ABresult
0X0
100
111
553 6 Beam::Wire::Exception::InvalidConfig->throw(
554 name => $name,
555 file => $self->file,
556 error => qq{"$other[0]" cannot be used with "class" or "extends"},
557 );
558 }
559  
560 167 ▶ if ( exists $service_info{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $service_info{"value"}) 2 165 167
561 2 return $service_info{value};
562 }
563  
564 165 ▶ if ( exists $service_info{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $service_info{"env"}) 2 163 165
565 2 ▶ return exists $ENV{$service_info{env}} ? $ENV{$service_info{env}} : ($service_info{default} // undef);
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{$service_info{'env'}} ? : 1 1 2
Condition50%1/2
$service_info{"default"} // undef
l!l
10
MC/DC0%0/1
$service_info{"default"} // undef
$service_info{"default"}
Truth table50%1/2
A
A= $service_info{"default"}
Aresult
11
00
566 }
567  
568 163 ▶ if ( exists $service_info{ref} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $service_info{"ref"}) 1 162 163
569 # at this point the service info is normalized, so none of the
570 # meta keys have a prefix. this will cause resolve_ref some angst,
571 # so de-normalize them
572 1 my %meta = $self->get_meta_names;
573 1 ▶ my %de_normalized = map { $meta{$_} // $_ => $service_info{$_} } keys %service_info;
Condition33%1/3
$meta{$_} // $_
l!l&&r!l&&!r
200
MC/DC0%0/2
$meta{$_} // $_
$meta{$_} $_
Truth table33%1/3
A // B
A= $meta{$_}B= $_
ABresult
1X1
011
000
574 1 return ( $self->resolve_ref( $name, \%de_normalized ) )[0];
575 }
576  
577 162 ▶ if ( $service_info{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_info{"config"}) 9 153 162
578 9 my $conf_path = path( $service_info{config} );
579 9 ▶ if ( !$conf_path->is_absolute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf_path->is_absolute) 5 4 9
580 5 $conf_path = $self->_resolve_relative_path($conf_path);
581 }
582 9 ▶ if ($service_info{default} && (!$conf_path || !$conf_path->is_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_info{"default"} and not $conf_path && $conf_path->is_file) 1 8 9
Condition50%3/6
$service_info{"default"} and not $conf_path && $conf_path->is_file
!ll&&!rl&&r
801
$conf_path && $conf_path->is_file
!ll&&!rl&&r
010
MC/DC33%1/3
$service_info{"default"} and not $conf_path && $conf_path->is_file
$service_info{"default"} $conf_path $conf_path->is_file
Truth table50%2/4
A and not(B && C)
A= $service_info{"default"}B= $conf_pathC= $conf_path->is_file
ABCresult
0XX0
1110
10X1
1101
583 1 return $service_info{default};
584 }
585 8 return $self->_load_config( "$conf_path" );
586 }
587  
588 153 ▶ if ( !$service_info{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service_info{"class"}) 2 151 153
589 2 Beam::Wire::Exception::InvalidConfig->throw(
590 name => $name,
591 file => $self->file,
592 error => 'Service configuration incomplete. Missing one of "class", "value", "config", "ref"',
593 );
594 }
595  
596 151 $self->emit( configure_service =>
597 class => 'Beam::Wire::Event::ConfigService',
598 service_name => $name,
599 config => \%service_info,
600 );
601  
602 151 ▶ $service_info{version} ? use_module( $service_info{class}, $service_info{version} ) : use_module( $service_info{class} );
Branch: 1 branch
BranchTrueFalseTotal
$service_info{'version'} ? : 2 149 151
603  
604 150 ▶ if ( my $with = $service_info{with} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $with = $service_info{"with"}) 4 146 150
605 4 ▶ my @roles = ref $with ? @{ $with } : ( $with );
Branch: 1 branch
BranchTrueFalseTotal
ref $with ? : 2 2 4
606 4 my $class = Moo::Role->create_class_with_roles( $service_info{class}, @roles );
607 4 $service_info{class} = $class;
608 }
609  
610 150 ▶ my $method = $service_info{method} || "new";
Condition100%2/2
$service_info{'method'} || 'new'
l!l
6144
MC/DC100%1/1
$service_info{'method'} || 'new'
$service_info{'method'}
Truth table100%2/2
A
A= $service_info{'method'}
Aresult
11
00
611 150 my $service;
612 150 ▶ if ( ref $method eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method eq 'ARRAY') { } 2 148 150
613 2 for my $m ( @{$method} ) {
614 4 my $method_name = $m->{method};
615 4 ▶ my $return = $m->{return} || q{};
Condition100%2/2
$m->{'return'} || ''
l!l
13
MC/DC100%1/1
$m->{'return'} || ''
$m->{'return'}
Truth table100%2/2
A
A= $m->{'return'}
Aresult
11
00
616 4 delete $service_info{args};
617 4 my @args = $self->parse_args( $name, $service_info{class}, $m->{args} );
618 4 ▶ my $invocant = defined $service ? $service : $service_info{class};
Branch: 1 branch
BranchTrueFalseTotal
defined $service ? : 2 2 4
619 4 my $output = $invocant->$method_name( @args );
620 4 ▶ $service = !defined $service || $return eq 'chain' ? $output
Branch: 1 branch
BranchTrueFalseTotal
!defined($service) || $return eq 'chain' ? : 3 1 4
Condition100%3/3
not defined $service or $return eq "chain"
l!l&&r!l&&!r
211
MC/DC100%2/2
not defined $service or $return eq "chain"
not defined $service $return eq "chain"
Truth table100%3/3
not(A) or B
A= not defined $serviceB= $return eq "chain"
ABresult
1X1
011
000
621 : $service;
622 }
623 }
624 else {
625 148 my @args = $self->parse_args( $name, @service_info{"class","args","default"} );
626 148 ▶ if ( $service_info{class}->can( 'DOES' ) && $service_info{class}->DOES( 'Beam::Service' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_info{"class"}->can("DOES") and $service_info{"class"}->DOES("Beam::Service")) 4 144 148
Condition66%2/3
$service_info{"class"}->can("DOES") and $service_info{"class"}->DOES("Beam::Service")
!ll&&!rl&&r
01444
MC/DC50%1/2
$service_info{"class"}->can("DOES") and $service_info{"class"}->DOES("Beam::Service")
$service_info{"class"}->can("DOES") $service_info{"class"}->DOES("Beam::Service")
Truth table66%2/3
A and B
A= $service_info{"class"}->can("DOES")B= $service_info{"class"}->DOES("Beam::Service")
ABresult
0X0
100
111
627 4 push @args, name => $name, container => $self;
628 }
629 148 $service = $service_info{class}->$method( @args );
630 }
631  
632 147 ▶ if ( $service_info{on} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_info{"on"}) 7 140 147
633 7 my %meta = $self->get_meta_names;
634 7 my @listeners;
635  
636 7 ▶ if ( ref $service_info{on} eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $service_info{'on'} eq 'ARRAY') { } 1 6 7
elsif (ref $service_info{'on'} eq 'HASH') { } 6 0 6
637 1 @listeners = map { [ %$_ ] } @{ $service_info{on} };
638 }
639 elsif ( ref $service_info{on} eq 'HASH' ) {
640 6 for my $event ( keys %{ $service_info{on} } ) {
641 6 ▶ if ( ref $service_info{on}{$event} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $service_info{'on'}{$event} eq 'ARRAY') { } 1 5 6
642 push @listeners,
643 2 map {; [ $event => $_ ] }
644 1 @{ $service_info{on}{$event} };
645 }
646 else {
647 5 push @listeners, [ $event => $service_info{on}{$event} ];
648 }
649 }
650 }
651  
652 7 for my $listener ( @listeners ) {
653 9 my ( $event, $conf ) = @$listener;
654 9 ▶ if ( $conf->{ $meta{method} } && !$conf->{ $meta{sub} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$conf{$meta{"method"}} and not $$conf{$meta{"sub"}}) 1 8 9
Condition66%2/3
$$conf{$meta{"method"}} and not $$conf{$meta{"sub"}}
!ll&&!rl&&r
801
MC/DC50%1/2
$$conf{$meta{"method"}} and not $$conf{$meta{"sub"}}
$$conf{$meta{"method"}} not $$conf{$meta{"sub"}}
Truth table66%2/3
A and not(B)
A= $$conf{$meta{"method"}}B= not $$conf{$meta{"sub"}}
ABresult
0X0
100
111
655 1 _deprecated( 'warning: (deprecated) "$method" in event handlers is now "$sub" in service "' . $name . '"' );
656 }
657 9 ▶ my $sub_name = delete $conf->{ $meta{sub} } || delete $conf->{ $meta{method} };
Condition100%3/3
delete $$conf{$meta{'sub'}} || delete $$conf{$meta{'method'}}
l!l&&r!l&&!r
711
MC/DC100%2/2
delete $$conf{$meta{'sub'}} || delete $$conf{$meta{'method'}}
delete $$conf{$meta{'sub'}} delete $$conf{$meta{'method'}}
Truth table100%3/3
A || B
A= delete $$conf{$meta{'sub'}}B= delete $$conf{$meta{'method'}}
ABresult
1X1
011
000
658 9 my ( $listen_svc ) = $self->find_refs( $name, $conf );
659 9 ▶ $service->on( $event => sub { $listen_svc->$sub_name( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
660 }
661 }
662  
663 147 $self->emit( build_service =>
664 class => 'Beam::Wire::Event::BuildService',
665 service_name => $name,
666 service => $service,
667 );
668  
669 147 return $service;
670 }
671  
672 #pod =method merge_config
673 #pod
674 #pod my %merged = $wire->merge_config( %config );
675 #pod
676 #pod If C<%config> contains an C<extends> key, merge the extended config together
677 #pod with this one, returning the merged service configuration. This works recursively,
678 #pod so a service can extend a service that extends another service just fine.
679 #pod
680 #pod When merging, hashes are combined, with the child configuration taking
681 #pod precedence. The C<args> key is handled specially to allow a hash of
682 #pod args to be merged. A single element array of args is merged too, if the
683 #pod element is a hash.
684 #pod
685 #pod The configuration returned is a safe copy and can be modified without
686 #pod effecting the original config.
687 #pod
688 #pod =cut
689  
690 215 ▶sub merge_config {
Subroutine merge_config: called CC=12 SCAR=24.9
Pod documented
691 215 my ( $self, %service_info ) = @_;
692 215 ▶ if ( $service_info{ extends } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_info{"extends"}) 38 177 215
693 38 my $base_config_ref = $self->get_config( $service_info{extends} );
694 38 ▶ unless ( $base_config_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_config_ref) 3 35 38
695 Beam::Wire::Exception::NotFound->throw(
696 name => $service_info{extends},
697 3 file => $self->file,
698 );
699 }
700 35 my %base_config = %{ $self->normalize_config( $base_config_ref ) };
701 # Merge the args separately, to be a bit nicer about hashes of arguments
702 35 my $args;
703 35 ▶ if ( ref $service_info{args} eq 'HASH' && ref $base_config{args} eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $service_info{'args'} eq 'HASH' and ref $base_config{'args'} eq 'HASH') { } 8 27 35
elsif (ref $service_info{'args'} eq 'ARRAY' and @{$service_info{'args'};} == 1 and ref $service_info{'args'}[0] eq 'HASH' and ref $base_config{'args'} eq 'ARRAY' and @{$base_config{'args'};} == 1 and ref $base_config{'args'}[0] eq 'HASH') { } 1 45 46
Condition100%3/3
ref $service_info{"args"} eq "HASH" and ref $base_config{"args"} eq "HASH"
!ll&&!rl&&r
2528
MC/DC100%2/2
ref $service_info{"args"} eq "HASH" and ref $base_config{"args"} eq "HASH"
ref $service_info{"args"} eq "HASH" ref $base_config{"args"} eq "HASH"
Truth table100%3/3
A and B
A= ref $service_info{"args"} eq "HASH"B= ref $base_config{"args"} eq "HASH"
ABresult
0X0
100
111
704 8 $args = { %{ delete $base_config{args} }, %{ delete $service_info{args} } };
705 10 ▶ } elsif ( ref $service_info{args} eq 'ARRAY' && @{ $service_info{args} } == 1 && ref $service_info{args}->[0] eq 'HASH' &&
Condition80%12/15
ref $service_info{"args"} eq "ARRAY" and @{$service_info{"args"};} == 1 and ref $service_info{"args"}[0] eq "HASH" and ref $base_config{"args"} eq "ARRAY" and @{$base_config{"args"};} == 1 and ref $base_config{"args"}[0] eq "HASH"
!ll&&!rl&&r
1971
ref $service_info{"args"} eq "ARRAY" and @{$service_info{"args"};} == 1 and ref $service_info{"args"}[0] eq "HASH" and ref $base_config{"args"} eq "ARRAY" and @{$base_config{"args"};} == 1
!ll&&!rl&&r
1908
ref $service_info{"args"} eq "ARRAY" and @{$service_info{"args"};} == 1 and ref $service_info{"args"}[0] eq "HASH" and ref $base_config{"args"} eq "ARRAY"
!ll&&!rl&&r
1908
ref $service_info{"args"} eq "ARRAY" and @{$service_info{"args"};} == 1 and ref $service_info{"args"}[0] eq "HASH"
!ll&&!rl&&r
1728
ref $service_info{"args"} eq "ARRAY" and @{$service_info{"args"};} == 1
!ll&&!rl&&r
17010
MC/DC50%3/6
ref $service_info{"args"} eq "ARRAY" and @{$service_info{"args"};} == 1 and ref $service_info{"args"}[0] eq "HASH" and ref $base_config{"args"} eq "ARRAY" and @{$base_config{"args"};} == 1 and ref $base_config{"args"}[0] eq "HASH"
ref $service_info{"args"} eq "ARRAY" @{$service_info{"args"};} == 1 ref $service_info{"args"}[0] eq "HASH" ref $base_config{"args"} eq "ARRAY" @{$base_config{"args"};} == 1 ref $base_config{"args"}[0] eq "HASH"
Truth table57%4/7
A and B and C and D and E and F
A= ref $service_info{"args"} eq "ARRAY"B= @{$service_info{"args"};} == 1C= ref $service_info{"args"}[0] eq "HASH"D= ref $base_config{"args"} eq "ARRAY"E= @{$base_config{"args"};} == 1F= ref $base_config{"args"}[0] eq "HASH"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
706 8 ref $base_config{args} eq 'ARRAY' && @{ $base_config{args} } == 1 && ref $base_config{args}->[0] eq 'HASH' ) {
707 1 $args = [ { %{ delete($base_config{args})->[0] }, %{ delete($service_info{args})->[0] } } ];
708 }
709 35 %service_info = ( $self->merge_config( %base_config ), %service_info );
710 35 ▶ if ( $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 9 26 35
711 9 $service_info{args} = $args;
712 }
713 }
714 212 return %service_info;
715 }
716  
717 #pod =method parse_args
718 #pod
719 #pod my @args = $wire->parse_args( $for_name, $class, $args );
720 #pod
721 #pod Parse the arguments (C<$args>) for the given service (C<$for_name>) with
722 #pod the given class (C<$class>).
723 #pod
724 #pod C<$args> can be an array reference, a hash reference, or a simple
725 #pod scalar. The arguments will be searched for references using L<the
726 #pod find_refs method|/find_refs>, and then a list of arguments will be
727 #pod returned, ready to pass to the object's constructor.
728 #pod
729 #pod Nested containers are handled specially by this method:
730 #pod
731 #pod =over
732 #pod
733 #pod =item * Inner references are not resolved by the parent container.
734 #pod This ensures that references are always relative to the container they're in.
735 #pod
736 #pod =item * If a file is specified but cannot be found, a C<default> can be provided
737 #pod as a fallback.
738 #pod
739 #pod =back
740 #pod
741 #pod =cut
742  
743 # NOTE: Fallback only works on nested Beam::Wire containers right now.
744 # I don't know what one could use to detect one should fall back for
745 # any other kind of service...
746 152 ▶sub parse_args {
Subroutine parse_args: called CC=16 SCAR=28.1
Pod documented
747 152 my ( $self, $for, $class, $args, $fallback ) = @_;
748 152 ▶ return if not $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 30 122 152
749 122 my @args;
750 122 ▶ if ( ref $args eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'ARRAY') { } 22 100 122
elsif (ref $args eq 'HASH') { } 95 5 100
751 22 @args = $self->find_refs( $for, @{$args} );
752 }
753 elsif ( ref $args eq 'HASH' ) {
754 # Hash args could be a ref
755 # Subcontainers cannot scan for refs in their configs
756 95 ▶ if ( $class->isa( 'Beam::Wire' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa('Beam::Wire')) { } 16 79 95
757 16 my %args = %{$args};
758 16 my $config = delete $args{config};
759 # Subcontainer files should inherit the lookup paths of the
760 # current container, unless overridden.
761 16 ▶ $args{dir} //= $self->dir;
Condition33%1/3
$args{"dir"} //= $self->dir
l!l&&r!l&&!r
0160
MC/DC0%0/2
$args{"dir"} //= $self->dir
$args{"dir"} $self->dir
Truth table33%1/3
A //= B
A= $args{"dir"}B= $self->dir
ABresult
1X1
011
000
762 # Relative subcontainer files should be looked up from the list of dirs.
763 16 ▶ if ( exists $args{file} && !path( $args{file} )->is_absolute ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"file"} and not path($args{"file"})->is_absolute) 9 7 16
Condition100%3/3
exists $args{"file"} and not path($args{"file"})->is_absolute
!ll&&!rl&&r
349
MC/DC100%2/2
exists $args{"file"} and not path($args{"file"})->is_absolute
exists $args{"file"} not path($args{"file"})->is_absolute
Truth table100%3/3
A and not(B)
A= exists $args{"file"}B= not path($args{"file"})->is_absolute
ABresult
0X0
100
111
764 9 $args{file} = $self->_resolve_relative_path($args{file}, $args{dir});
765 }
766 # If the file doesn't exist, try to fall back to a default
767 16 ▶ if ( exists $args{file} && !($args{file} && path( $args{file} )->is_file) && $fallback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"file"} and not $args{"file"} && path($args{"file"})->is_file and $fallback) 2 14 16
Condition88%8/9
exists $args{"file"} and not $args{"file"} && path($args{"file"})->is_file and $fallback
!ll&&!rl&&r
1222
exists $args{"file"} and not $args{"file"} && path($args{"file"})->is_file
!ll&&!rl&&r
394
$args{'file'} && path($args{'file'})->is_file
!ll&&!rl&&r
049
MC/DC75%3/4
exists $args{"file"} and not $args{"file"} && path($args{"file"})->is_file and $fallback
exists $args{"file"} $args{'file'} path($args{'file'})->is_file $fallback
Truth table66%4/6
A and not(B && C) and D
A= exists $args{"file"}B= $args{'file'}C= path($args{'file'})->is_fileD= $fallback
ABCDresult
0XXX0
111X0
10X00
11000
10X11
11011
768 2 delete $args{file};
769 2 %args = (%args, %$fallback);
770 }
771 16 @args = $self->find_refs( $for, %args );
772 16 ▶ if ( $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config) 2 14 16
773 2 push @args, config => $config;
774 }
775 }
776 else {
777 79 my ( $maybe_ref ) = $self->find_refs( $for, $args );
778 79 ▶ if ( blessed $maybe_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($maybe_ref)) { } 0 79 79
779 0 @args = ( $maybe_ref );
780 }
781 else {
782 79 ▶ @args = ref $maybe_ref eq 'HASH' ? %$maybe_ref
Branch: 2 branches
BranchTrueFalseTotal
ref $maybe_ref eq 'HASH' ? : 79 0 79
ref $maybe_ref eq 'ARRAY' ? : 0 0 0
783 : ref $maybe_ref eq 'ARRAY' ? @$maybe_ref
784 : ( $maybe_ref );
785 }
786 }
787 }
788 else {
789 # Try anyway?
790 5 @args = $args;
791 }
792  
793 122 return @args;
794 }
795  
796 #pod =method find_refs
797 #pod
798 #pod my @resolved = $wire->find_refs( $for_name, @args );
799 #pod
800 #pod Go through the C<@args> and recursively resolve any references and
801 #pod services found inside, returning the resolved result. References are
802 #pod identified with L<the is_meta method|/is_meta>.
803 #pod
804 #pod If a reference contains a C<$ref> key, it will be resolved by L<the
805 #pod resolve_ref method|/resolve_ref>. Otherwise, the reference will be
806 #pod treated as an anonymous service, and passed directly to L<the
807 #pod create_service method|/create_service>.
808 #pod
809 #pod This is used when L<creating a service|create_service> to ensure all
810 #pod dependencies are created first.
811 #pod
812 #pod =cut
813  
814 284 ▶sub find_refs {
Subroutine find_refs: called CC=7 SCAR=19.5
Pod documented
815 284 my ( $self, $for, @args ) = @_;
816  
817 284 ; printf STDERR qq{Searching for refs for "%s": %s}, $for, Dumper( \@args ) if DEBUG;
818  
819 284 my @out;
820 284 my %meta = $self->get_meta_names;
821 284 for my $arg ( @args ) {
822 456 ▶ if ( ref $arg eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 146 310 456
elsif (ref $arg eq 'ARRAY') { } 29 281 310
823 146 ▶ if ( $self->is_meta( $arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_meta($arg)) { } 46 100 146
824 46 ▶ if ( $arg->{ $meta{ ref } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$arg{$meta{'ref'}}) { } 36 10 46
825 36 push @out, $self->resolve_ref( $for, $arg );
826 }
827 else { # Try to treat it as a service to create
828 10 ; print STDERR "Creating anonymous service: " . Dumper( $arg ) if DEBUG;
829  
830 10 my %service_info = %{ $self->normalize_config( $arg ) };
831 10 push @out, $self->create_service( '$anonymous', %service_info );
832 }
833 }
834 else {
835 100 push @out, { $self->find_refs( $for, %{$arg} ) };
836 }
837 }
838 elsif ( ref $arg eq 'ARRAY' ) {
839 29 push @out, [ map { $self->find_refs( $for, $_ ) } @{$arg} ];
840 }
841 else {
842 281 push @out, $arg; # simple scalars
843 }
844 }
845  
846 # In case we only pass in one argument and want one return value
847 284 ▶ return wantarray ? @out : $out[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 268 16 284
848 }
849  
850 #pod =method is_meta
851 #pod
852 #pod my $is_meta = $wire->is_meta( $ref_hash, $root );
853 #pod
854 #pod Returns true if the given hash reference describes some kind of
855 #pod Beam::Wire service. This is used to identify service configuration
856 #pod hashes inside of larger data structures.
857 #pod
858 #pod A service hash reference must contain at least one key, and must either
859 #pod contain a L<prefixed|/meta_prefix> key that could create or reference an
860 #pod object (one of C<class>, C<extends>, C<config>, C<value>, C<env>, or C<ref>) or,
861 #pod if the C<$root> flag exists, be made completely of unprefixed meta keys
862 #pod (as returned by L<the get_meta_names method|/get_meta_names>).
863 #pod
864 #pod The C<$root> flag is used by L<the get method|/get> to allow unprefixed
865 #pod meta keys in the top-level hash values.
866 #pod
867 #pod =cut
868  
869 334 ▶sub is_meta {
Subroutine is_meta: called CC=6 SCAR=17.9
Pod documented
870 334 my ( $self, $arg, $root ) = @_;
871  
872 # Only a hashref can be meta
873 334 ▶ return unless ref $arg eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg eq "HASH" 0 334 334
874  
875 334 my @keys = keys %$arg;
876 334 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 3 331 334
877  
878 331 my %meta = $self->get_meta_names;
879  
880 # A regular service does not need the prefix, but must consist
881 # only of meta keys
882 331 ▶ return 1 if $root && scalar @keys eq grep { $meta{ $_ } } @keys;
Branch: 1 branch
BranchTrueFalseTotal
if $root and scalar @keys eq grep({$meta{$_};} @keys) 153 178 331
Condition100%3/3
$root and scalar @keys eq grep({$meta{$_};} @keys)
!ll&&!rl&&r
15424153
MC/DC100%2/2
$root and scalar @keys eq grep({$meta{$_};} @keys)
$root scalar @keys eq grep({$meta{$_};} @keys)
Truth table100%3/3
A and B
A= $rootB= scalar @keys eq grep({$meta{$_};} @keys)
ABresult
0X0
100
111
883  
884 # A meta service contains at least one of these keys, as these are
885 # the keys that can create a service. All other keys are
886 # modifiers
887 return 1
888 1068 if grep { exists $arg->{ $_ } }
889 178 ▶ map { $meta{ $_ } }
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $arg->{$_};} map({$meta{$_};} "ref", "class", "extends", "config", "value", "env") 66 112 178
890 qw( ref class extends config value env );
891  
892 # Must not be meta
893 112 return;
894 }
895  
896 #pod =method get_meta_names
897 #pod
898 #pod my %meta_keys = $wire->get_meta_names;
899 #pod
900 #pod Get all the possible service keys with the L<meta prefix|/meta_prefix> already
901 #pod attached.
902 #pod
903 #pod =cut
904  
905 1004 ▶sub get_meta_names {
Subroutine get_meta_names: called CC=2 SCAR=7.0
Pod documented
906 1004 my ( $self ) = @_;
907 1004 my $prefix = $self->meta_prefix;
908 1004 my %meta = (
909 ref => "${prefix}ref",
910 path => "${prefix}path",
911 method => "${prefix}method",
912 args => "${prefix}args",
913 class => "${prefix}class",
914 version => "${prefix}version",
915 extends => "${prefix}extends",
916 sub => "${prefix}sub",
917 call => "${prefix}call",
918 lifecycle => "${prefix}lifecycle",
919 on => "${prefix}on",
920 with => "${prefix}with",
921 value => "${prefix}value",
922 config => "${prefix}config",
923 env => "${prefix}env",
924 default => "${prefix}default",
925 );
926 1004 ▶ return wantarray ? %meta : \%meta;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1004 0 1004
927 }
928  
929 #pod =method resolve_ref
930 #pod
931 #pod my @value = $wire->resolve_ref( $for_name, $ref_hash );
932 #pod
933 #pod Resolves the given dependency from the configuration hash (C<$ref_hash>)
934 #pod for the named service (C<$for_name>). Reference hashes contain the
935 #pod following keys:
936 #pod
937 #pod =over 4
938 #pod
939 #pod =item $ref
940 #pod
941 #pod The name of a service in the container. Required.
942 #pod
943 #pod =item $path (deprecated)
944 #pod
945 #pod B<Deprecated>: Bare hashrefs can be treated as child containers with C<$ref>
946 #pod now. If there are other uses of C<$path> that cannot be updated to C<$ref>,
947 #pod please L<open a GitHub issue to discuss it|https://github.com/preaction/Beam-Wire/issues>.
948 #pod
949 #pod A data path to pick some data out of the reference. Useful with C<value>
950 #pod and C<config> services.
951 #pod
952 #pod # container.yml
953 #pod bounties:
954 #pod value:
955 #pod malcolm: 50000
956 #pod zoe: 35000
957 #pod simon: 100000
958 #pod
959 #pod captain:
960 #pod class: Person
961 #pod args:
962 #pod name: Malcolm Reynolds
963 #pod bounty:
964 #pod $ref: bounties
965 #pod $path: /malcolm
966 #pod
967 #pod =item $call
968 #pod
969 #pod Call a method on the referenced object and use the resulting value. This
970 #pod may be a string, which will be the method name to call, or a hash with
971 #pod C<$method> and C<$args>, which are the method name to call and the
972 #pod arguments to that method, respectively.
973 #pod
974 #pod captain:
975 #pod class: Person
976 #pod args:
977 #pod name: Malcolm Reynolds
978 #pod location:
979 #pod $ref: beacon
980 #pod $call: get_location
981 #pod bounty:
982 #pod $ref: news
983 #pod $call:
984 #pod $method: get_bounty
985 #pod $args:
986 #pod name: mreynolds
987 #pod
988 #pod =back
989 #pod
990 #pod =cut
991  
992 37 ▶sub resolve_ref {
Subroutine resolve_ref: called CC=9 SCAR=22.1
Pod documented
993 37 my ( $self, $for, $arg ) = @_;
994  
995 37 my %meta = $self->get_meta_names;
996  
997 37 my @ref;
998 37 my $name = $arg->{ $meta{ref} };
999 37 my $service = $self->get( $name );
1000 # resolve service ref w/path
1001 37 ▶ if ( my $path = $arg->{ $meta{path} } ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $path = $$arg{$meta{'path'}}) { } 0 37 37
elsif (my $call = $$arg{$meta{'call'}}) { } 4 33 37
elsif (my $method = $$arg{$meta{'method'}}) { } 3 30 33
1002 # This was not nearly as useful as I had hoped... It would be better,
1003 # much better, to have $ref handle these kind of things instead.
1004 0 _deprecated('warning: (deprecated) Using $path is deprecated to remove the dependency on Data::DPath in service "' . $for . '"' );
1005 # locate foreign service data
1006 0 use_module( 'Data::DPath' )->import('dpath');
1007 0 @ref = dpath( $path )->match($service);
1008 }
1009 elsif ( my $call = $arg->{ $meta{call} } ) {
1010 4 my ( $method, @args );
1011  
1012 4 ▶ if ( ref $call eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $call eq 'HASH') { } 2 2 4
1013 2 $method = $call->{ $meta{method} };
1014 2 my $args = $call->{ $meta{args} };
1015 @args = !$args ? ()
1016 2 ▶ : ref $args eq 'ARRAY' ? @{ $args }
Branch: 2 branches
BranchTrueFalseTotal
!$args ? : 0 2 2
ref $args eq 'ARRAY' ? : 1 1 2
1017 : $args;
1018 }
1019 else {
1020 2 $method = $call;
1021 }
1022  
1023 4 @ref = $service->$method( @args );
1024 }
1025 elsif ( my $method = $arg->{ $meta{method} } ) {
1026 3 _deprecated( 'warning: (deprecated) Using "$method" to get a value in a dependency is now "$call" in service "' . $for . '"' );
1027 3 my $args = $arg->{ $meta{args} };
1028 my @args = !$args ? ()
1029 3 ▶ : ref $args eq 'ARRAY' ? @{ $args }
Branch: 2 branches
BranchTrueFalseTotal
!$args ? : 1 2 3
ref $args eq 'ARRAY' ? : 1 1 2
1030 : $args;
1031 3 @ref = $service->$method( @args );
1032 }
1033 else {
1034 30 @ref = $service;
1035 }
1036  
1037 37 return @ref;
1038 }
1039  
1040 #pod =method fix_refs
1041 #pod
1042 #pod my @fixed = $wire->fix_refs( $for_container_name, @args );
1043 #pod
1044 #pod Similar to L<the find_refs method|/find_refs>. This method searches
1045 #pod through the C<@args> and recursively fixes any reference paths to be
1046 #pod absolute. References are identified with L<the is_meta
1047 #pod method|/is_meta>.
1048 #pod
1049 #pod This is used by L<the get_config method|/get_config> to ensure that the
1050 #pod configuration can be passed directly in to L<the create_service
1051 #pod method|create_service>.
1052 #pod
1053 #pod =cut
1054  
1055 19 ▶sub fix_refs {
Subroutine fix_refs: called CC=7 SCAR=19.5
Pod documented
1056 19 my ( $self, $container_name, @args ) = @_;
1057 19 my @out;
1058 19 my %meta = $self->get_meta_names;
1059 19 for my $arg ( @args ) {
1060 22 ▶ if ( ref $arg eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 9 13 22
elsif (ref $arg eq 'ARRAY') { } 3 10 13
1061 9 ▶ if ( $self->is_meta( $arg, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_meta($arg, 1)) { } 6 3 9
1062 #; print STDERR 'Fixing refs for arg: ' . Dumper( $arg );
1063 6 my %new = %$arg;
1064 6 for my $key ( keys %new ) {
1065 10 ▶ if ( $key =~ /(?:ref|extends)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(?:ref|extends)$/) { } 3 7 10
1066 3 $new{ $key } = join( q{/}, $container_name, $new{$key} );
1067 }
1068 else {
1069 7 ( $new{ $key } ) = $self->fix_refs( $container_name, $new{ $key } );
1070 }
1071 }
1072 #; print STDERR 'Fixed refs for arg: ' . Dumper( \%new );
1073 6 push @out, \%new;
1074 }
1075 else {
1076 3 push @out, { $self->fix_refs( $container_name, %{$arg} ) };
1077 }
1078 }
1079 elsif ( ref $arg eq 'ARRAY' ) {
1080 3 push @out, [ map { $self->fix_refs( $container_name, $_ ) } @{$arg} ];
1081 }
1082 else {
1083 10 push @out, $arg; # simple scalars
1084 }
1085 }
1086 19 return @out;
1087 }
1088  
1089  
1090 #pod =method new
1091 #pod
1092 #pod my $wire = Beam::Wire->new( %attributes );
1093 #pod
1094 #pod Create a new container.
1095 #pod
1096 #pod =cut
1097  
1098 127 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=20.8
Pod undocumented
1099 127 my ( $self ) = @_;
1100  
1101 127 ▶ if ( $self->file && !path( $self->file )->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file and not path($self->file)->exists) 3 124 127
Condition100%3/3
$self->file and not path($self->file)->exists
!ll&&!rl&&r
96283
MC/DC100%2/2
$self->file and not path($self->file)->exists
$self->file not path($self->file)->exists
Truth table100%3/3
A and not(B)
A= $self->fileB= not path($self->file)->exists
ABresult
0X0
100
111
1102 3 my $file = $self->file;
1103 3 Beam::Wire::Exception::Constructor->throw(
1104 attr => 'file',
1105 error => qq{Container file '$file' does not exist},
1106 );
1107 }
1108  
1109 # Create all the eager services
1110 124 my %meta = $self->get_meta_names;
1111 124 for my $key ( keys %{ $self->config } ) {
1112 213 my $config = $self->config->{$key};
1113 213 ▶ if ( ref $config eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq "HASH") 203 10 213
1114 203 ▶ my $lifecycle = $config->{lifecycle} || $config->{ $meta{lifecycle} };
Condition66%2/3
$config->{'lifecycle'} || $$config{$meta{'lifecycle'}}
l!l&&r!l&&!r
40199
MC/DC50%1/2
$config->{'lifecycle'} || $$config{$meta{'lifecycle'}}
$config->{'lifecycle'} $$config{$meta{'lifecycle'}}
Truth table66%2/3
A || B
A= $config->{'lifecycle'}B= $$config{$meta{'lifecycle'}}
ABresult
1X1
011
000
1115 203 ▶ if ( $lifecycle && $lifecycle eq 'eager' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lifecycle and $lifecycle eq "eager") 1 202 203
Condition100%3/3
$lifecycle and $lifecycle eq "eager"
!ll&&!rl&&r
19931
MC/DC100%2/2
$lifecycle and $lifecycle eq "eager"
$lifecycle $lifecycle eq "eager"
Truth table100%3/3
A and B
A= $lifecycleB= $lifecycle eq "eager"
ABresult
0X0
100
111
1116 1 $self->get($key);
1117 }
1118 }
1119 }
1120 123 return;
1121 }
1122  
1123 29 my %deprecated_warnings;
1124 4 ▶sub _deprecated {
Subroutine _deprecated: called CC=2 SCAR=7.1
1125 4 my ( $warning ) = @_;
1126 4 ▶ return if $deprecated_warnings{ $warning };
Branch: 1 branch
BranchTrueFalseTotal
if $deprecated_warnings{$warning} 0 4 4
1127 4 warn $deprecated_warnings{ $warning } = $warning . "\n";
1128 }
1129  
1130 # Load a config file
1131 35 ▶sub _load_config {
Subroutine _load_config: called CC=3 SCAR=11.0
1132 35 my ( $self, $path ) = @_;
1133 35 local $Config::Any::YAML::NO_YAML_XS_WARNING = 1;
1134  
1135 35 my $loader;
1136 35 eval {
1137 35 $loader = Config::Any->load_files( {
1138 files => [$path], use_ext => 1, flatten_to_hash => 1
1139 } );
1140 };
1141 35 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 34 35
1142 1 Beam::Wire::Exception::Config->throw(
1143 file => $self->file,
1144 config_error => $@,
1145 );
1146 }
1147  
1148 34 ▶ return "HASH" eq ref $loader ? (values(%{$loader}))[0] : {};
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $loader ? : 34 0 34
1149 }
1150  
1151 14 ▶sub _resolve_relative_path {
Subroutine _resolve_relative_path: called CC=6 SCAR=18.5
1152 14 my ( $self, $file, $dirs ) = @_;
1153 14 ▶ $dirs //= $self->dir;
Condition66%2/3
$dirs //= $self->dir
l!l&&r!l&&!r
950
MC/DC0%0/2
$dirs //= $self->dir
$dirs $self->dir
Truth table66%2/3
A //= B
A= $dirsB= $self->dir
ABresult
1X1
011
000
1154 14 ; printf STDERR qq{Searching for path '%s' in %s}, $file, Dumper( $dirs ) if DEBUG;
1155 14 for my $dir ( @{ $dirs } ) {
1156 11 ▶ $dir = !(blessed $dir && $dir->isa('Path::Tiny')) ? path($dir) : $dir;
Branch: 1 branch
BranchTrueFalseTotal
!(builtin::blessed($dir) && $dir->isa('Path::Tiny')) ? : 0 11 11
Condition33%1/3
builtin::blessed($dir) && $dir->isa('Path::Tiny')
!ll&&!rl&&r
0011
MC/DC0%0/2
builtin::blessed($dir) && $dir->isa('Path::Tiny')
builtin::blessed($dir) $dir->isa('Path::Tiny')
Truth table33%1/3
A && B
A= builtin::blessed($dir)B= $dir->isa('Path::Tiny')
ABresult
0X0
100
111
1157 11 ▶ if ($dir->child($file)->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->child($file)->exists) 9 2 11
1158 9 return $dir->child( $file );
1159 }
1160 }
1161 # Allow the file to fall through so we get an error message with
1162 # the relative filename that we tried looking up.
1163 5 return $file;
1164 }
1165  
1166 # Check config file for known issues and report
1167 # Optionally attempt to get all configured items for complete test
1168 # Intended for use with beam-wire script
1169 4 ▶sub validate {
Subroutine validate: called CC=22 SCAR=48.1
Pod undocumented
1170 4 my $error_count = 0;
1171 4 my @valid_dependency_nodes = qw( class method args extends lifecycle on config );
1172 4 my ( $self, $instantiate, $show_all_errors ) = @_;
1173  
1174 4 ▶ while ( my ( $name, $v ) = each %{ $self->{config} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"config"};}) 5 0 5
1175  
1176 5 ▶ if ($instantiate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instantiate) 0 5 5
1177 0 ▶ if ($show_all_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_all_errors) { } 0 0 0
1178 0 eval {
1179 0 $self->get($name);
1180 };
1181 0 ▶ print $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1182 }
1183 else {
1184 0 $self->get($name);
1185 }
1186 0 next;
1187 };
1188  
1189 5 my %config = %{ $self->get_config($name) };
1190 5 %config = $self->merge_config(%config);
1191  
1192 4 my @classy = grep { exists $config{$_} } qw( class extends );
1193 4 my @other = grep { exists $config{$_} } qw( value ref config );
1194  
1195 4 ▶ if ( @other > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other > 1) 0 4 4
1196 0 $error_count++;
1197 0 my $error = 'use only one of "value", "ref", or "config"';
1198  
1199 0 ▶ if ($show_all_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_all_errors) 0 0 0
1200 0 print qq(Invalid config for service '$name': $error\n);
1201 0 next;
1202 }
1203  
1204 Beam::Wire::Exception::InvalidConfig->throw(
1205 0 name => $name,
1206 file => $self->file,
1207 error => $error,
1208 );
1209 }
1210  
1211 4 ▶ if ( @classy && @other ) { # @other == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@classy and @other) 3 1 4
Condition66%2/3
@classy and @other
!ll&&!rl&&r
103
MC/DC50%1/2
@classy and @other
@classy @other
Truth table66%2/3
A and B
A= @classyB= @other
ABresult
0X0
100
111
1212 3 $error_count++;
1213 3 my $error = qq{"$other[0]" cannot be used with "class" or "extends"};
1214  
1215 3 ▶ if ($show_all_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_all_errors) 0 3 3
1216 0 print qq(Invalid config for service '$name': $error\n);
1217 0 next;
1218 }
1219  
1220 Beam::Wire::Exception::InvalidConfig->throw(
1221 3 name => $name,
1222 file => $self->file,
1223 error => $error,
1224 );
1225 }
1226  
1227 1 ▶ if ( exists $config{value} && ( exists $config{class} || exists $config{extends})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{"value"} and exists $config{"class"} || exists $config{"extends"}) 0 1 1
Condition33%2/6
exists $config{"value"} and exists $config{"class"} || exists $config{"extends"}
!ll&&!rl&&r
010
exists $config{"class"} or exists $config{"extends"}
l!l&&r!l&&!r
001
MC/DC0%0/3
exists $config{"value"} and exists $config{"class"} || exists $config{"extends"}
exists $config{"value"} exists $config{"class"} exists $config{"extends"}
Truth table25%1/4
A and B or C
A= exists $config{"value"}B= exists $config{"class"}C= exists $config{"extends"}
ABCresult
0XX0
1000
11X1
1011
1228 0 $error_count++;
1229 0 ▶ if ($show_all_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_all_errors) 0 0 0
1230 0 print qq(Invalid config for service '$name': "value" cannot be used with "class" or "extends"\n);
1231 0 next;
1232 }
1233  
1234 Beam::Wire::Exception::InvalidConfig->throw(
1235 0 name => $name,
1236 file => $self->file,
1237 error => '"value" cannot be used with "class" or "extends"',
1238 );
1239 }
1240  
1241 1 ▶ if ( $config{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"config"}) 0 1 1
1242 0 my $conf_path = path( $config{config} );
1243 0 ▶ if ( $self->file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) 0 0 0
1244 0 $conf_path = path( $self->file )->parent->child($conf_path);
1245 }
1246 0 %config = %{ $self->_load_config("$conf_path") };
1247 }
1248  
1249 1 ▶ unless ( $config{value} || $config{class} || $config{extends} || $config{ref} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config{"value"} or $config{"class"} or $config{"extends"} or $config{"ref"}) 0 1 1
Condition33%3/9
$config{"value"} or $config{"class"} or $config{"extends"} or $config{"ref"}
l!l&&r!l&&!r
100
$config{"value"} or $config{"class"} or $config{"extends"}
l!l&&r!l&&!r
100
$config{"value"} or $config{"class"}
l!l&&r!l&&!r
100
MC/DC0%0/4
$config{"value"} or $config{"class"} or $config{"extends"} or $config{"ref"}
$config{"value"} $config{"class"} $config{"extends"} $config{"ref"}
Truth table20%1/5
A or B or C or D
A= $config{"value"}B= $config{"class"}C= $config{"extends"}D= $config{"ref"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1250 0 next;
1251 }
1252  
1253 1 ▶ if ($config{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"class"}) 0 1 1
1254 0 ▶ eval "require " . $config{class} if $config{class};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"class"} 0 0 0
1255 }
1256 #TODO: check method chain & serial
1257 }
1258 0 return $error_count;
1259 }
1260  
1261 #pod =head1 EXCEPTIONS
1262 #pod
1263 #pod If there is an error internal to Beam::Wire, an exception will be thrown. If there is an
1264 #pod error with creating a service or calling a method, the exception thrown will be passed-
1265 #pod through unaltered.
1266 #pod
1267 #pod =head2 Beam::Wire::Exception
1268 #pod
1269 #pod The base exception class
1270 #pod
1271 #pod =cut
1272  
1273 package Beam::Wire::Exception;
1274 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1275 29 with 'Throwable';
1276 29 ▶use Types::Standard qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
1277 29 ▶use overload q{""} => sub { $_[0]->error };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1278  
1279 29 has error => (
1280 is => 'ro',
1281 isa => Str,
1282 );
1283  
1284 #pod =head2 Beam::Wire::Exception::Constructor
1285 #pod
1286 #pod An exception creating a Beam::Wire object
1287 #pod
1288 #pod =cut
1289  
1290 package Beam::Wire::Exception::Constructor;
1291 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1292 29 ▶use Types::Standard qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
1293 29 extends 'Beam::Wire::Exception';
1294  
1295 29 has attr => (
1296 is => 'ro',
1297 isa => Str,
1298 required => 1,
1299 );
1300  
1301 #pod =head2 Beam::Wire::Exception::Config
1302 #pod
1303 #pod An exception loading the configuration file.
1304 #pod
1305 #pod =cut
1306  
1307 package Beam::Wire::Exception::Config;
1308 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1309 29 ▶use Types::Standard qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
1310 29 extends 'Beam::Wire::Exception';
1311  
1312 29 has file => (
1313 is => 'ro',
1314 isa => Maybe[InstanceOf['Path::Tiny']],
1315 );
1316  
1317 29 has config_error => (
1318 is => 'ro',
1319 isa => Str,
1320 required => 1,
1321 );
1322  
1323 has '+error' => (
1324 lazy => 1,
1325 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1326 1 my ( $self ) = @_;
1327 1 return sprintf 'Could not load container file "%s": Error from config parser: %s',
1328 $self->file,
1329 $self->config_error;
1330 },
1331 29 );
1332  
1333 #pod =head2 Beam::Wire::Exception::Service
1334 #pod
1335 #pod An exception with service information inside
1336 #pod
1337 #pod =cut
1338  
1339 package Beam::Wire::Exception::Service;
1340 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1341 29 ▶use Types::Standard qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
1342 29 extends 'Beam::Wire::Exception';
1343  
1344 29 has name => (
1345 is => 'ro',
1346 isa => Str,
1347 required => 1,
1348 );
1349  
1350 29 has file => (
1351 is => 'ro',
1352 isa => Maybe[InstanceOf['Path::Tiny']],
1353 );
1354  
1355 #pod =head2 Beam::Wire::Exception::NotFound
1356 #pod
1357 #pod The requested service or configuration was not found.
1358 #pod
1359 #pod =cut
1360  
1361 package Beam::Wire::Exception::NotFound;
1362 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1363 29 extends 'Beam::Wire::Exception::Service';
1364  
1365 has '+error' => (
1366 lazy => 1,
1367 4 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1368 4 my ( $self ) = @_;
1369 4 my $name = $self->name;
1370 4 my $file = $self->file;
1371 4 ▶ return "Service '$name' not found" . ( $file ? " in file '$file'" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 1 3 4
1372 },
1373 29 );
1374  
1375 #pod =head2 Beam::Wire::Exception::InvalidConfig
1376 #pod
1377 #pod The configuration is invalid:
1378 #pod
1379 #pod =over 4
1380 #pod
1381 #pod =item *
1382 #pod
1383 #pod Both "value" and "class" or "extends" are defined. These are mutually-exclusive.
1384 #pod
1385 #pod =back
1386 #pod
1387 #pod =cut
1388  
1389 package Beam::Wire::Exception::InvalidConfig;
1390 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1391 29 extends 'Beam::Wire::Exception::Service';
1392 29 ▶use overload q{""} => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
1393 7 my ( $self ) = @_;
1394 7 my $file = $self->file;
1395  
1396 7 ▶ sprintf "Invalid config for service '%s': %s%s",
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 1 6 7
1397 $self->name,
1398 $self->error,
1399 ( $file ? " in file '$file'" : "" ),
1400 ;
1401 29 };
1402  
1403 #pod =head1 EVENTS
1404 #pod
1405 #pod The container emits the following events.
1406 #pod
1407 #pod =head2 configure_service
1408 #pod
1409 #pod This event is emitted when a new service is configured, but before it is
1410 #pod instantiated or any classes loaded. This allows altering of the
1411 #pod configuration before the service is built. Already-built services will
1412 #pod not fire this event.
1413 #pod
1414 #pod Event handlers get a L<Beam::Wire::Event::ConfigService> object as their
1415 #pod only argument.
1416 #pod
1417 #pod This event will bubble up from child containers.
1418 #pod
1419 #pod =head2 build_service
1420 #pod
1421 #pod This event is emitted when a new service is built. Cached services will
1422 #pod not fire this event.
1423 #pod
1424 #pod Event handlers get a L<Beam::Wire::Event::BuildService> object as their
1425 #pod only argument.
1426 #pod
1427 #pod This event will bubble up from child containers.
1428 #pod
1429 #pod =cut
1430  
1431 29 1;
1432  
1433 __END__
↑ Summary Wire/Event/BuildService.pm »