annotate lib/IMPL/Config/ServiceDescriptor.pm @ 427:09e0086a82a7 ref20150831 tip

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