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