comparison lib/IMPL/Config/ServiceDescriptor.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents cc2cf8c0edc2
children df591e3afd10
comparison
equal deleted inserted replaced
416:cc2cf8c0edc2 417:3ed0c58e9da3
11 base => [ 11 base => [
12 'IMPL::Object' => undef, 12 'IMPL::Object' => undef,
13 'IMPL::Config::Descriptor' => undef 13 'IMPL::Config::Descriptor' => undef
14 ], 14 ],
15 props => [ 15 props => [
16 type => 'ro', 16 type => 'r',
17 activation => 'ro', 17 activation => 'r',
18 args => 'ro', 18 args => 'r',
19 services => 'ro', 19 services => 'r',
20 norequire => 'r',
20 _name => 'rw', 21 _name => 'rw',
21 _loaded => 'rw' 22 _loaded => 'rw'
22 ] 23 ]
23 }; 24 };
24 25
28 $this->type( $opts{type} ) 29 $this->type( $opts{type} )
29 or die IMPL::InvalidArgumentException->new('type'); 30 or die IMPL::InvalidArgumentException->new('type');
30 31
31 $this->activation( 32 $this->activation(
32 IMPL::Config::Descriptor::ParseActivation( $opts{activation} ) ); 33 IMPL::Config::Descriptor::ParseActivation( $opts{activation} ) );
33 $this->args( $opts{args} ) if exists $opts{args}; 34 $this->args( $opts{args} ) if exists $opts{args};
34 $this->services( $opts{services} ) if exists $opts{services}; 35 $this->services( $opts{services} ) if exists $opts{services};
36 $this->norequire( $opts{norequire} ) if exists $opts{norequire};
35 37
36 $this->_name( 'new {' 38 $this->_name( 'new {'
37 . IMPL::Config::Descriptor::ActivationToString( $this->activation ) 39 . IMPL::Config::Descriptor::ActivationToString( $this->activation )
38 . '} ' 40 . '} '
39 . $this->type ); 41 . $this->type );
41 43
42 sub Activate { 44 sub Activate {
43 my ( $this, $context ) = @_; 45 my ( $this, $context ) = @_;
44 46
45 my $instance; 47 my $instance;
46 $context->GuardScope( 48 $context->EnterScope( $this->_name, $this->services );
47 $this->_name,
48 $this->services,
49 sub {
50 49
51 my $activation = $this->activation; 50 my $activation = $this->activation;
52 my $cache; 51 my $cache;
53 52
54 if ( $activation == IMPL::Config::Descriptor::ACTIVATE_SINGLETON ) { 53 if ( $activation == IMPL::Config::Descriptor::ACTIVATE_SINGLETON ) {
55 $cache = $context->container->root->instances; 54 $cache = $context->container->root->instances;
56 } 55 }
57 elsif ( 56 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTAINER ) {
58 $activation == IMPL::Config::Descriptor::ACTIVATE_CONTAINER ) 57 $cache = $context->container->instances;
59 { 58 }
60 $cache = $context->container->instances; 59 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTEXT ) {
61 } 60 $cache = $context->instances;
62 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTEXT ) 61 }
63 {
64 $cache = $context->instances;
65 }
66 62
67 $instance = $cache->{ ref($this) } if $cache; 63 $instance = $cache->{ ref($this) } if $cache;
68 unless ($instance) { 64 unless ($instance) {
69 $instance = $this->CreateInstance($context); 65 $instance = $this->CreateInstance($context);
70 } 66 }
71 67
72 $cache->{ ref($this) } = $instance if $cache; 68 $cache->{ ref($this) } = $instance if $cache;
73 } 69
74 ); 70 $context->LeaveScope();
75 71
76 return $instance; 72 return $instance;
77 } 73 }
78 74
79 sub CreateInstance { 75 sub CreateInstance {
80 my ( $this, $context ) = @_; 76 my ( $this, $context ) = @_;
81 77
82 my $class = $context > container->Require( $this->type ); 78 my $class =
79 $this->norequire
80 ? $this->type
81 : $context->container->Require( $this->type );
83 82
84 my $args = $this->args ? $this->args->Activate($context) : undef; 83 my $args = $this->args ? $this->args->Activate($context) : undef;
85 84
86 if ( defined $args ) { 85 if ( defined $args ) {
87 if ( isarray($args) ) { 86 if ( isarray($args) ) {