comparison lib/IMPL/Config/ServiceDescriptor.pm @ 422:b0481c071bea ref20150831

IMPL::Config::Container tests, YAMLConfiguration now works and tested
author cin
date Sun, 20 Aug 2017 00:20:41 +0300
parents 7798345304bc
children
comparison
equal deleted inserted replaced
421:7798345304bc 422:b0481c071bea
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',
22 _name => 'rw', 21 _name => 'rw',
23 _loaded => 'rw' 22 _loaded => 'rw'
24 ] 23 ]
25 }; 24 };
26 25
27 sub CTOR { 26 sub CTOR {
28 my ( $this, %opts ) = @_; 27 my ( $this, %opts ) = @_;
29 28
30 $this->type( $opts{type} ) 29 $this->type( $opts{type} )
31 or die IMPL::InvalidArgumentException->new('type'); 30 or die IMPL::InvalidArgumentException->new('type');
32 $this->owner( $opts{owner} )
33 or die IMPL::InvalidArgumentException->new('owner');
34 31
35 $this->activation( SELF->ParseActivation( $opts{activation} ) ); 32 $this->activation( SELF->ParseActivation( $opts{activation} ) );
36 $this->args( $opts{args} ) if exists $opts{args}; 33 $this->args( $opts{args} ) if exists $opts{args};
37 $this->services( $opts{services} ) if exists $opts{services}; 34 $this->services( $opts{services} ) if exists $opts{services};
38 $this->norequire( $opts{norequire} ) if exists $opts{norequire}; 35 $this->norequire( $opts{norequire} ) if exists $opts{norequire};
41 . SELF->ActivationToString( $this->activation ) . '} ' 38 . SELF->ActivationToString( $this->activation ) . '} '
42 . $this->type ); 39 . $this->type );
43 } 40 }
44 41
45 sub Activate { 42 sub Activate {
46 my ( $this, $context ) = @_; 43 my ( $this, $context) = @_;
47 44
48 my $instance; 45 my $instance;
49 $context->EnterScope( $this->_name, $this->services );
50
51 my $activation = $this->activation; 46 my $activation = $this->activation;
52 my $cache; 47 my $cache;
53 48
54 if ( $activation == SELF->ACTIVATE_SINGLETON ) { 49 if ( $activation == SELF->ACTIVATE_SINGLETON ) {
55 $cache = $context->container->root->instances; 50 $cache = $context->container->root->instances;
56 } 51 }
57 elsif ( $activation == SELF->ACTIVATE_CONTAINER ) { 52 elsif ( $activation == SELF->ACTIVATE_CONTAINER ) {
58 $cache = $this->owner->instances; 53 $cache = $context->container->instances;
59 } 54 }
60 elsif ( $activation == SELF->ACTIVATE_HIERARCHY ) { 55 elsif ( $activation == SELF->ACTIVATE_HIERARCHY ) {
61 $cache = $context->container->instances; 56 $cache = $context->owner->instances;
62 } 57 }
63 elsif ( $activation == SELF->ACTIVATE_CONTEXT ) { 58 elsif ( $activation == SELF->ACTIVATE_CONTEXT ) {
64 $cache = $context->instances; 59 $cache = $context->instances;
65 } 60 }
66 61
68 unless ($instance) { 63 unless ($instance) {
69 $instance = $this->CreateInstance($context); 64 $instance = $this->CreateInstance($context);
70 $cache->{ ref($this) } = $instance if $cache; 65 $cache->{ ref($this) } = $instance if $cache;
71 } 66 }
72 67
73 $context->LeaveScope();
74
75 return $instance; 68 return $instance;
76 } 69 }
77 70
78 sub CreateInstance { 71 sub CreateInstance {
79 my ( $this, $context ) = @_; 72 my ( $this, $context) = @_;
80 73
81 my $class = 74 my $class =
82 $this->norequire 75 $this->norequire
83 ? $this->type 76 ? $this->type
84 : $context->container->Require( $this->type ); 77 : $context->container->Require( $this->type );
85 78
86 my $args = $this->args ? $this->args->Activate($context) : undef; 79
87 80 # determine how to pass arguments
88 if ( defined $args ) { 81 if (isarray($this->args)) {
89 if ( isarray($args) ) { 82 # if args is an array ref, pass it as list
90 return $class->new(@$args); 83 return $class->new(map $context->Activate($_), @{$this->args});
84 } elsif (ishash($this->args)) {
85 # if args is a hash ref, pass it as list
86 my %args;
87 while(my ($k,$v) = each %{$this->args}) {
88 $args{$k} = $context->Activate($v);
91 } 89 }
92 elsif ( ishash($args) ) { 90 return $class->new(%args);
93 return $class->new(%$args); 91 } elsif(defined $this->args) {
94 } 92 # otherwise pass it as a single argument
95 else { 93 return $class->new($context->Activate($this->args));
96 return $class->new($args); 94 } else {
97 }
98 }
99 else {
100 return $class->new(); 95 return $class->new();
101 } 96 }
102 } 97 }
103 98
99 sub GetName {
100 shift->_name;
101 }
102
104 1; 103 1;