annotate lib/IMPL/Config/ServiceDescriptor.pm @ 420:df591e3afd10 ref20150831

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