annotate lib/IMPL/Config/ServiceDescriptor.pm @ 416:cc2cf8c0edc2 ref20150831

sync
author cin
date Thu, 29 Oct 2015 03:50:25 +0300
parents 3d24b10dd0d5
children 3ed0c58e9da3
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 => [
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
16 type => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
17 activation => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
18 args => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
19 services => 'ro',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
20 _name => 'rw',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
21 _loaded => 'rw'
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
25 sub CTOR {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
26 my ( $this, %opts ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
27
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
28 $this->type( $opts{type} )
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
29 or die IMPL::InvalidArgumentException->new('type');
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
30
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
31 $this->activation(
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
32 IMPL::Config::Descriptor::ParseActivation( $opts{activation} ) );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
33 $this->args( $opts{args} ) if exists $opts{args};
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
34 $this->services( $opts{services} ) if exists $opts{services};
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
35
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
36 $this->_name( 'new {'
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
37 . IMPL::Config::Descriptor::ActivationToString( $this->activation )
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
38 . '} '
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
39 . $this->type );
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 {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
43 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
44
416
cin
parents: 415
diff changeset
45 my $instance;
cin
parents: 415
diff changeset
46 $context->GuardScope(
cin
parents: 415
diff changeset
47 $this->_name,
cin
parents: 415
diff changeset
48 $this->services,
cin
parents: 415
diff changeset
49 sub {
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
50
416
cin
parents: 415
diff changeset
51 my $activation = $this->activation;
cin
parents: 415
diff changeset
52 my $cache;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
53
416
cin
parents: 415
diff changeset
54 if ( $activation == IMPL::Config::Descriptor::ACTIVATE_SINGLETON ) {
cin
parents: 415
diff changeset
55 $cache = $context->container->root->instances;
cin
parents: 415
diff changeset
56 }
cin
parents: 415
diff changeset
57 elsif (
cin
parents: 415
diff changeset
58 $activation == IMPL::Config::Descriptor::ACTIVATE_CONTAINER )
cin
parents: 415
diff changeset
59 {
cin
parents: 415
diff changeset
60 $cache = $context->container->instances;
cin
parents: 415
diff changeset
61 }
cin
parents: 415
diff changeset
62 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTEXT )
cin
parents: 415
diff changeset
63 {
cin
parents: 415
diff changeset
64 $cache = $context->instances;
cin
parents: 415
diff changeset
65 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
66
416
cin
parents: 415
diff changeset
67 $instance = $cache->{ ref($this) } if $cache;
cin
parents: 415
diff changeset
68 unless ($instance) {
cin
parents: 415
diff changeset
69 $instance = $this->CreateInstance($context);
cin
parents: 415
diff changeset
70 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
71
416
cin
parents: 415
diff changeset
72 $cache->{ ref($this) } = $instance if $cache;
cin
parents: 415
diff changeset
73 }
cin
parents: 415
diff changeset
74 );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
75
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
76 return $instance;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
77 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
78
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
79 sub CreateInstance {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
80 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
81
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
82 my $class = $context > container->Require( $this->type );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
83
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
84 my $args = $this->args ? $this->args->Activate($context) : undef;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
85
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
86 if ( defined $args ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
87 if ( isarray($args) ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
88 return $class->new(@$args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
89 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
90 elsif ( ishash($args) ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
91 return $class->new(%$args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
92 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
93 else {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
94 return $class->new($args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
95 }
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();
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
102 1;