comparison lib/IMPL/Config/ServiceDescriptor.pm @ 421:7798345304bc ref20150831

working on IMPL::Config, removed old stuff
author cin
date Sun, 16 Jul 2017 22:59:39 +0300
parents df591e3afd10
children b0481c071bea
comparison
equal deleted inserted replaced
420:df591e3afd10 421:7798345304bc
2 use strict; 2 use strict;
3 3
4 use IMPL::lang qw(:base); 4 use IMPL::lang qw(:base);
5 use IMPL::Exception(); 5 use IMPL::Exception();
6 use IMPL::declare { 6 use IMPL::declare {
7 require => { 7 require => {
8 Bag => 'IMPL::Config::Bag', 8 Bag => 'IMPL::Config::Bag',
9 ActivationException => 'IMPL::Config::ActivationException' 9 ActivationException => 'IMPL::Config::ActivationException'
10 }, 10 },
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 => 'r', 16 type => 'r',
17 activation => 'r', 17 activation => 'r',
18 args => 'r', 18 args => 'r',
19 services => 'r', 19 services => 'r',
20 norequire => 'r', 20 norequire => 'r',
21 owner => 'r', 21 owner => 'r',
22 _name => 'rw', 22 _name => 'rw',
23 _loaded => 'rw' 23 _loaded => 'rw'
24 ] 24 ]
25 }; 25 };
26 26
27 sub CTOR { 27 sub CTOR {
28 my ( $this, %opts ) = @_; 28 my ( $this, %opts ) = @_;
29 29
30 $this->type( $opts{type} ) 30 $this->type( $opts{type} )
31 or die IMPL::InvalidArgumentException->new('type'); 31 or die IMPL::InvalidArgumentException->new('type');
32 $this->owner( $opts{owner} ) 32 $this->owner( $opts{owner} )
33 or die IMPL::InvalidArgumentException->new('owner'); 33 or die IMPL::InvalidArgumentException->new('owner');
34 34
35 $this->activation( SELF->ParseActivation( $opts{activation} ) ); 35 $this->activation( SELF->ParseActivation( $opts{activation} ) );
36 $this->args( $opts{args} ) if exists $opts{args}; 36 $this->args( $opts{args} ) if exists $opts{args};
37 $this->services( $opts{services} ) if exists $opts{services}; 37 $this->services( $opts{services} ) if exists $opts{services};
38 $this->norequire( $opts{norequire} ) if exists $opts{norequire}; 38 $this->norequire( $opts{norequire} ) if exists $opts{norequire};
39 39
40 $this->_name( 'new {' 40 $this->_name( 'new {'
41 . SELF->ActivationToString( $this->activation ) 41 . SELF->ActivationToString( $this->activation ) . '} '
42 . '} ' 42 . $this->type );
43 . $this->type );
44 } 43 }
45 44
46 sub Activate { 45 sub Activate {
47 my ( $this, $context ) = @_; 46 my ( $this, $context ) = @_;
48 47
49 my $instance; 48 my $instance;
50 $context->EnterScope( $this->_name, $this->services ); 49 $context->EnterScope( $this->_name, $this->services );
51 50
52 my $activation = $this->activation; 51 my $activation = $this->activation;
53 my $cache; 52 my $cache;
54 53
55 if ( $activation == SELF->ACTIVATE_SINGLETON ) { 54 if ( $activation == SELF->ACTIVATE_SINGLETON ) {
56 $cache = $context->container->root->instances; 55 $cache = $context->container->root->instances;
57 } 56 }
58 elsif ( $activation == SELF->ACTIVATE_CONTAINER ) { 57 elsif ( $activation == SELF->ACTIVATE_CONTAINER ) {
59 $cache = $this->owner->instances; 58 $cache = $this->owner->instances;
60 } 59 }
61 elsif ( $activation == SELF->ACTIVATE_HIERARCHY ) { 60 elsif ( $activation == SELF->ACTIVATE_HIERARCHY ) {
62 $cache = $context->container->instances; 61 $cache = $context->container->instances;
63 } 62 }
64 elsif ( $activation == SELF->ACTIVATE_CONTEXT ) { 63 elsif ( $activation == SELF->ACTIVATE_CONTEXT ) {
65 $cache = $context->instances; 64 $cache = $context->instances;
66 } 65 }
67 66
68 $instance = $cache->{ ref($this) } if $cache; 67 $instance = $cache->{ ref($this) } if $cache;
69 unless ($instance) { 68 unless ($instance) {
70 $instance = $this->CreateInstance($context); 69 $instance = $this->CreateInstance($context);
71 } 70 $cache->{ ref($this) } = $instance if $cache;
71 }
72 72
73 $cache->{ ref($this) } = $instance if $cache; 73 $context->LeaveScope();
74 74
75 $context->LeaveScope(); 75 return $instance;
76
77 return $instance;
78 } 76 }
79 77
80 sub CreateInstance { 78 sub CreateInstance {
81 my ( $this, $context ) = @_; 79 my ( $this, $context ) = @_;
82 80
83 my $class = 81 my $class =
84 $this->norequire 82 $this->norequire
85 ? $this->type 83 ? $this->type
86 : $context->container->Require( $this->type ); 84 : $context->container->Require( $this->type );
87 85
88 my $args = $this->args ? $this->args->Activate($context) : undef; 86 my $args = $this->args ? $this->args->Activate($context) : undef;
89 87
90 if ( defined $args ) { 88 if ( defined $args ) {
91 if ( isarray($args) ) { 89 if ( isarray($args) ) {
92 return $class->new(@$args); 90 return $class->new(@$args);
93 } 91 }
94 elsif ( ishash($args) ) { 92 elsif ( ishash($args) ) {
95 return $class->new(%$args); 93 return $class->new(%$args);
96 } 94 }
97 else { 95 else {
98 return $class->new($args); 96 return $class->new($args);
99 } 97 }
100 } 98 }
101 else { 99 else {
102 return $class->new(); 100 return $class->new();
103 } 101 }
104 } 102 }
105 103
106 1; 104 1;