Mercurial > pub > Impl
comparison lib/IMPL/Config/ServiceDescriptor.pm @ 415:3d24b10dd0d5 ref20150831
working on IMPL::Config::Container
author | cin |
---|---|
date | Tue, 20 Oct 2015 07:32:55 +0300 |
parents | c6e90e02dd17 |
children | cc2cf8c0edc2 |
comparison
equal
deleted
inserted
replaced
414:ec6f2d389d1e | 415:3d24b10dd0d5 |
---|---|
1 package IMPL::Config::ServiceDescriptor; | |
2 use strict; | |
3 | |
4 use IMPL::lang qw(:base); | |
5 use IMPL::Exception(); | |
6 use IMPL::declare { | |
7 require => { | |
8 Bag => 'IMPL::Config::Bag' | |
9 }, | |
10 base => [ | |
11 'IMPL::Object' => undef, | |
12 'IMPL::Config::Descriptor' => undef | |
13 ], | |
14 props => [ | |
15 type => 'ro', | |
16 activation => 'ro', | |
17 args => 'ro', | |
18 services => 'ro', | |
19 _name => 'rw', | |
20 _loaded => 'rw' | |
21 ] | |
22 }; | |
23 | |
24 sub CTOR { | |
25 my ( $this, %opts ) = @_; | |
26 | |
27 $this->type( $opts{type} ) | |
28 or die IMPL::InvalidArgumentException->new('type'); | |
29 | |
30 $this->activation( | |
31 IMPL::Config::Descriptor::ParseActivation( $opts{activation} ) ); | |
32 $this->args( $opts{args} ) if exists $opts{args}; | |
33 $this->services( $opts{services} ) if exists $opts{services}; | |
34 | |
35 $this->_name( 'new {' | |
36 . IMPL::Config::Descriptor::ActivationToString( $this->activation ) | |
37 . '} ' | |
38 . $this->type ); | |
39 } | |
40 | |
41 sub Activate { | |
42 my ( $this, $context ) = @_; | |
43 | |
44 $context->EnterScope( $this->_name, $this->services ); | |
45 | |
46 my $activation = $this->activation; | |
47 my $cache; | |
48 | |
49 if ( $activation == IMPL::Config::Descriptor::ACTIVATE_SINGLETON ) { | |
50 $cache = $context->container->root->instances; | |
51 } | |
52 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTAINER ) { | |
53 $cache = $context->container->instances; | |
54 } | |
55 elsif ( $activation == IMPL::Config::Descriptor::ACTIVATE_CONTEXT ) { | |
56 $cache = $context->instances; | |
57 } | |
58 | |
59 my $instance = $cache->{ ref($this) } if $cache; | |
60 | |
61 unless ($instance) { | |
62 $instance = $this->CreateInstance($context); | |
63 } | |
64 | |
65 $cache->{ ref($this) } = $instance if $cache; | |
66 | |
67 $context->LeaveScope(); | |
68 | |
69 return $instance; | |
70 } | |
71 | |
72 sub CreateInstance { | |
73 my ( $this, $context ) = @_; | |
74 | |
75 my $class = $context > container->Require( $this->type ); | |
76 | |
77 my $args = $this->args ? $this->args->Activate($context) : undef; | |
78 | |
79 if ( defined $args ) { | |
80 if ( isarray($args) ) { | |
81 return $class->new(@$args); | |
82 } | |
83 elsif ( ishash($args) ) { | |
84 return $class->new(%$args); | |
85 } | |
86 else { | |
87 return $class->new($args); | |
88 } | |
89 } | |
90 else { | |
91 return $class->new(); | |
92 } | |
93 } | |
94 | |
95 1; |