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