annotate lib/IMPL/Config/ServiceDescriptor.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents cc2cf8c0edc2
children df591e3afd10
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',
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
21 _name => 'rw',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
22 _loaded => 'rw'
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
26 sub CTOR {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
27 my ( $this, %opts ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
28
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
29 $this->type( $opts{type} )
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
30 or die IMPL::InvalidArgumentException->new('type');
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
31
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
32 $this->activation(
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
33 IMPL::Config::Descriptor::ParseActivation( $opts{activation} ) );
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
34 $this->args( $opts{args} ) if exists $opts{args};
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
35 $this->services( $opts{services} ) if exists $opts{services};
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
36 $this->norequire( $opts{norequire} ) if exists $opts{norequire};
415
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->_name( 'new {'
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
39 . IMPL::Config::Descriptor::ActivationToString( $this->activation )
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
40 . '} '
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
41 . $this->type );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
42 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
43
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
44 sub Activate {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
45 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
46
416
cin
parents: 415
diff changeset
47 my $instance;
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
48 $context->EnterScope( $this->_name, $this->services );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
49
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
50 my $activation = $this->activation;
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
51 my $cache;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
52
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
53 if ( $activation == IMPL::Config::Descriptor::ACTIVATE_SINGLETON ) {
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
54 $cache = $context->container->root->instances;
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
55 }
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
56 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTAINER ) {
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
57 $cache = $context->container->instances;
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
58 }
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
59 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTEXT ) {
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
60 $cache = $context->instances;
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
61 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
62
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
63 $instance = $cache->{ ref($this) } if $cache;
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
64 unless ($instance) {
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
65 $instance = $this->CreateInstance($context);
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 $cache->{ ref($this) } = $instance if $cache;
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
69
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
70 $context->LeaveScope();
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
71
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
72 return $instance;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
73 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
74
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
75 sub CreateInstance {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
76 my ( $this, $context ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
77
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
78 my $class =
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
79 $this->norequire
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
80 ? $this->type
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
81 : $context->container->Require( $this->type );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
82
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
83 my $args = $this->args ? $this->args->Activate($context) : undef;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
84
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
85 if ( defined $args ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
86 if ( isarray($args) ) {
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 elsif ( ishash($args) ) {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
90 return $class->new(%$args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
91 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
92 else {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
93 return $class->new($args);
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
94 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
95 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
96 else {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
97 return $class->new();
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
98 }
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 1;