annotate lib/IMPL/Config/ServiceDescriptor.pm @ 415:3d24b10dd0d5 ref20150831

working on IMPL::Config::Container
author cin
date Tue, 20 Oct 2015 07:32:55 +0300
parents c6e90e02dd17
children cc2cf8c0edc2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
1 package IMPL::Config::ServiceDescriptor;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
2 use strict;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
3
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
4 use IMPL::lang qw(:base);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
5 use IMPL::Exception();
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
6 use IMPL::declare {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
7 require => {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
8 Bag => 'IMPL::Config::Bag'
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
9 },
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
10 base => [
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
11 'IMPL::Object' => undef,
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
12 'IMPL::Config::Descriptor' => undef
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
13 ],
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
14 props => [
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
15 type => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
16 activation => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
17 args => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
18 services => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
19 _name => 'rw',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
20 _loaded => 'rw'
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
21 ]
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
22 };
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
23
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
24 sub CTOR {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
25 my ( $this, %opts ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
26
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
27 $this->type( $opts{type} )
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
28 or die IMPL::InvalidArgumentException->new('type');
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
29
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
30 $this->activation(
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
31 IMPL::Config::Descriptor::ParseActivation( $opts{activation} ) );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
32 $this->args( $opts{args} ) if exists $opts{args};
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
33 $this->services( $opts{services} ) if exists $opts{services};
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
34
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
35 $this->_name( 'new {'
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
36 . IMPL::Config::Descriptor::ActivationToString( $this->activation )
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
37 . '} '
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
38 . $this->type );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
39 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
40
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
41 sub Activate {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
42 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
43
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
44 $context->EnterScope( $this->_name, $this->services );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
45
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
46 my $activation = $this->activation;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
47 my $cache;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
48
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
49 if ( $activation == IMPL::Config::Descriptor::ACTIVATE_SINGLETON ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
50 $cache = $context->container->root->instances;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
51 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
52 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTAINER ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
53 $cache = $context->container->instances;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
54 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
55 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTEXT ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
56 $cache = $context->instances;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
57 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
58
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
59 my $instance = $cache->{ ref($this) } if $cache;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
60
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
61 unless ($instance) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
62 $instance = $this->CreateInstance($context);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
63 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
64
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
65 $cache->{ ref($this) } = $instance if $cache;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
66
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
67 $context->LeaveScope();
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
68
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
69 return $instance;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
70 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
71
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
72 sub CreateInstance {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
73 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
74
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
75 my $class = $context > container->Require( $this->type );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
76
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
77 my $args = $this->args ? $this->args->Activate($context) : undef;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
78
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
79 if ( defined $args ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
80 if ( isarray($args) ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
81 return $class->new(@$args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
82 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
83 elsif ( ishash($args) ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
84 return $class->new(%$args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
85 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
86 else {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
87 return $class->new($args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
88 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
89 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
90 else {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
91 return $class->new();
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
92 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
93 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
94
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
95 1;