415
|
1 package IMPL::Config::ReferenceDescriptor;
|
|
2 use strict;
|
|
3
|
|
4 use IMPL::Exception();
|
|
5 use IMPL::declare {
|
|
6 base => [
|
|
7 'IMPL::Object' => undef,
|
|
8 'IMPL::Config::Descriptor' => undef
|
|
9 ],
|
|
10 props => [
|
|
11 reference => 'ro',
|
|
12 services => 'ro',
|
|
13 lazy => 'ro',
|
|
14 optional => 'ro',
|
|
15 default => 'ro',
|
|
16 _name => 'rw'
|
|
17 ]
|
|
18 };
|
|
19
|
|
20 sub CTOR {
|
|
21 my ( $this, $ref, %opts ) = @_;
|
|
22
|
|
23 $this->reference($ref)
|
|
24 or die IMPL::InvalidArgumentException->new('ref');
|
|
25
|
416
|
26 $this->lazy( $opts{lazy} ) if $opts{lazy};
|
|
27 $this->optional( $opts{optional} ) if $opts{optional};
|
|
28 $this->default( $opts{default} )
|
|
29 if $opts{optional} and exists $opts{default};
|
|
30
|
415
|
31 $this->_name( 'ref ' . $ref );
|
|
32 }
|
|
33
|
|
34 sub Activate {
|
|
35 my ( $this, $context ) = @_;
|
|
36
|
417
|
37 $context->EnterScope( $this->_name, $this->services );
|
415
|
38
|
|
39 my $ref = $this->reference;
|
|
40 my %opts;
|
|
41 $opts{default} = $this->default
|
|
42 if $this->optional;
|
|
43
|
|
44 if ( $this->lazy ) {
|
|
45 my $clone = $context->Clone();
|
|
46 return sub {
|
|
47 $clone->Resolve( $ref, %opts );
|
|
48 };
|
|
49 }
|
|
50 else {
|
|
51 return $context->Resolve( $ref, %opts );
|
|
52 }
|
|
53
|
417
|
54 $context->LeaveScope();
|
415
|
55 }
|
|
56
|
|
57 1;
|
416
|
58
|
|
59 __END__
|
|
60
|
|
61 =pod
|
|
62
|
|
63 =head1 NAME
|
|
64
|
|
65 C<IMPL::Config::ReferenceDescriptor> - A descriptor which is points to the other service
|
|
66
|
|
67 =head1 MEMBERS
|
|
68
|
|
69 =head2 PROPS
|
|
70
|
|
71 =head3 reference
|
|
72
|
|
73 =head3 lazy
|
|
74
|
|
75 =head3 optional
|
|
76
|
|
77 =head3 default
|
|
78
|
|
79 =head2 METHODS
|
|
80
|
|
81 =head3 CTOR($ref, %opts)
|
|
82
|
|
83 =head3 Activate($context)
|
|
84
|
|
85 Returns a result of the activation of the service specified by the C<reference> property.
|
|
86
|
|
87 If the reference is lazy the method returns a closure.The C<$context> will be cloned and the clone will be used to create the closure.
|
|
88 =cut
|