annotate lib/IMPL/Config/ServiceDescriptor.pm @ 421:7798345304bc ref20150831

working on IMPL::Config, removed old stuff
author cin
date Sun, 16 Jul 2017 22:59:39 +0300
parents df591e3afd10
children b0481c071bea
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 owner => 'r',
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
22 _name => 'rw',
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
23 _loaded => 'rw'
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
24 ]
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
25 };
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
26
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
27 sub CTOR {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
28 my ( $this, %opts ) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
29
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
30 $this->type( $opts{type} )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
31 or die IMPL::InvalidArgumentException->new('type');
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
32 $this->owner( $opts{owner} )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
33 or die IMPL::InvalidArgumentException->new('owner');
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
34
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
35 $this->activation( SELF->ParseActivation( $opts{activation} ) );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
36 $this->args( $opts{args} ) if exists $opts{args};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
37 $this->services( $opts{services} ) if exists $opts{services};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
38 $this->norequire( $opts{norequire} ) if exists $opts{norequire};
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
39
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
40 $this->_name( 'new {'
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
41 . SELF->ActivationToString( $this->activation ) . '} '
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
42 . $this->type );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
43 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
44
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
45 sub Activate {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
46 my ( $this, $context ) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
47
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
48 my $instance;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
49 $context->EnterScope( $this->_name, $this->services );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
50
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
51 my $activation = $this->activation;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
52 my $cache;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
53
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
54 if ( $activation == SELF->ACTIVATE_SINGLETON ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
55 $cache = $context->container->root->instances;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
56 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
57 elsif ( $activation == SELF->ACTIVATE_CONTAINER ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
58 $cache = $this->owner->instances;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
59 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
60 elsif ( $activation == SELF->ACTIVATE_HIERARCHY ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
61 $cache = $context->container->instances;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
62 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
63 elsif ( $activation == SELF->ACTIVATE_CONTEXT ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
64 $cache = $context->instances;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
65 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
66
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
67 $instance = $cache->{ ref($this) } if $cache;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
68 unless ($instance) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
69 $instance = $this->CreateInstance($context);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
70 $cache->{ ref($this) } = $instance if $cache;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
71 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
72
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
73 $context->LeaveScope();
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
74
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
75 return $instance;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
76 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
77
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
78 sub CreateInstance {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
79 my ( $this, $context ) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
80
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
81 my $class =
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
82 $this->norequire
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
83 ? $this->type
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
84 : $context->container->Require( $this->type );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
85
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
86 my $args = $this->args ? $this->args->Activate($context) : undef;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
87
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
88 if ( defined $args ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
89 if ( isarray($args) ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
90 return $class->new(@$args);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
91 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
92 elsif ( ishash($args) ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
93 return $class->new(%$args);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
94 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
95 else {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
96 return $class->new($args);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
97 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
98 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
99 else {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
100 return $class->new();
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
101 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
102 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
103
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
104 1;