annotate lib/IMPL/Config/ReferenceDescriptor.pm @ 419:bbc4739c4d48 ref20150831

working on IMPL::Config::Container
author cin
date Sun, 29 Jan 2017 10:30:20 +0300
parents 3ed0c58e9da3
children 7798345304bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
1 package IMPL::Config::ReferenceDescriptor;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
2 use strict;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
3
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
4 use IMPL::Exception();
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
5 use IMPL::declare {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
6 base => [
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
7 'IMPL::Object' => undef,
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
8 'IMPL::Config::Descriptor' => undef
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
9 ],
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
10 props => [
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
11 reference => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
12 services => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
13 lazy => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
14 optional => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
15 default => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
16 _name => 'rw'
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
17 ]
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
18 };
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
19
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
20 sub CTOR {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
21 my ( $this, $ref, %opts ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
22
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
23 $this->reference($ref)
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
24 or die IMPL::InvalidArgumentException->new('ref');
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
25
416
cin
parents: 415
diff changeset
26 $this->lazy( $opts{lazy} ) if $opts{lazy};
cin
parents: 415
diff changeset
27 $this->optional( $opts{optional} ) if $opts{optional};
cin
parents: 415
diff changeset
28 $this->default( $opts{default} )
cin
parents: 415
diff changeset
29 if $opts{optional} and exists $opts{default};
cin
parents: 415
diff changeset
30
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
31 $this->_name( 'ref ' . $ref );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
32 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
33
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
34 sub Activate {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
35 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
36
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
37 $context->EnterScope( $this->_name, $this->services );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
38
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
39 my $ref = $this->reference;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
40 my %opts;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
41 $opts{default} = $this->default
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
42 if $this->optional;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
43
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
44 if ( $this->lazy ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
45 my $clone = $context->Clone();
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
46 return sub {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
47 $clone->Resolve( $ref, %opts );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
48 };
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
49 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
50 else {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
51 return $context->Resolve( $ref, %opts );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
52 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
53
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
54 $context->LeaveScope();
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
55 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
56
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
57 1;
416
cin
parents: 415
diff changeset
58
cin
parents: 415
diff changeset
59 __END__
cin
parents: 415
diff changeset
60
cin
parents: 415
diff changeset
61 =pod
cin
parents: 415
diff changeset
62
cin
parents: 415
diff changeset
63 =head1 NAME
cin
parents: 415
diff changeset
64
cin
parents: 415
diff changeset
65 C<IMPL::Config::ReferenceDescriptor> - A descriptor which is points to the other service
cin
parents: 415
diff changeset
66
cin
parents: 415
diff changeset
67 =head1 MEMBERS
cin
parents: 415
diff changeset
68
cin
parents: 415
diff changeset
69 =head2 PROPS
cin
parents: 415
diff changeset
70
cin
parents: 415
diff changeset
71 =head3 reference
cin
parents: 415
diff changeset
72
cin
parents: 415
diff changeset
73 =head3 lazy
cin
parents: 415
diff changeset
74
cin
parents: 415
diff changeset
75 =head3 optional
cin
parents: 415
diff changeset
76
cin
parents: 415
diff changeset
77 =head3 default
cin
parents: 415
diff changeset
78
cin
parents: 415
diff changeset
79 =head2 METHODS
cin
parents: 415
diff changeset
80
cin
parents: 415
diff changeset
81 =head3 CTOR($ref, %opts)
cin
parents: 415
diff changeset
82
cin
parents: 415
diff changeset
83 =head3 Activate($context)
cin
parents: 415
diff changeset
84
cin
parents: 415
diff changeset
85 Returns a result of the activation of the service specified by the C<reference> property.
cin
parents: 415
diff changeset
86
cin
parents: 415
diff changeset
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.
cin
parents: 415
diff changeset
88 =cut