Mercurial > pub > Impl
annotate lib/IMPL/Config/ServiceDescriptor.pm @ 424:87af445663d7 ref20150831
IMPL::Object::_Base
| author | cin |
|---|---|
| date | Tue, 03 Apr 2018 10:54:09 +0300 |
| parents | b0481c071bea |
| children |
| rev | line source |
|---|---|
| 415 | 1 package IMPL::Config::ServiceDescriptor; |
| 2 use strict; | |
| 3 | |
| 4 use IMPL::lang qw(:base); | |
| 5 use IMPL::Exception(); | |
| 6 use IMPL::declare { | |
| 421 | 7 require => { |
| 8 Bag => 'IMPL::Config::Bag', | |
| 9 ActivationException => 'IMPL::Config::ActivationException' | |
| 10 }, | |
| 11 base => [ | |
| 12 'IMPL::Object' => undef, | |
| 13 'IMPL::Config::Descriptor' => undef | |
| 14 ], | |
| 15 props => [ | |
| 16 type => 'r', | |
| 17 activation => 'r', | |
| 18 args => 'r', | |
| 19 services => 'r', | |
| 20 norequire => 'r', | |
| 21 _name => 'rw', | |
| 22 _loaded => 'rw' | |
| 23 ] | |
| 415 | 24 }; |
| 25 | |
| 26 sub CTOR { | |
| 421 | 27 my ( $this, %opts ) = @_; |
| 415 | 28 |
| 421 | 29 $this->type( $opts{type} ) |
| 30 or die IMPL::InvalidArgumentException->new('type'); | |
| 415 | 31 |
| 421 | 32 $this->activation( SELF->ParseActivation( $opts{activation} ) ); |
| 33 $this->args( $opts{args} ) if exists $opts{args}; | |
| 34 $this->services( $opts{services} ) if exists $opts{services}; | |
| 35 $this->norequire( $opts{norequire} ) if exists $opts{norequire}; | |
| 415 | 36 |
| 421 | 37 $this->_name( 'new {' |
| 38 . SELF->ActivationToString( $this->activation ) . '} ' | |
| 39 . $this->type ); | |
| 415 | 40 } |
| 41 | |
| 42 sub Activate { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
43 my ( $this, $context) = @_; |
| 415 | 44 |
| 421 | 45 my $instance; |
| 46 my $activation = $this->activation; | |
| 47 my $cache; | |
| 415 | 48 |
| 421 | 49 if ( $activation == SELF->ACTIVATE_SINGLETON ) { |
| 50 $cache = $context->container->root->instances; | |
| 51 } | |
| 52 elsif ( $activation == SELF->ACTIVATE_CONTAINER ) { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
53 $cache = $context->container->instances; |
| 421 | 54 } |
| 55 elsif ( $activation == SELF->ACTIVATE_HIERARCHY ) { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
56 $cache = $context->owner->instances; |
| 421 | 57 } |
| 58 elsif ( $activation == SELF->ACTIVATE_CONTEXT ) { | |
| 59 $cache = $context->instances; | |
| 60 } | |
| 415 | 61 |
| 421 | 62 $instance = $cache->{ ref($this) } if $cache; |
| 63 unless ($instance) { | |
| 64 $instance = $this->CreateInstance($context); | |
| 65 $cache->{ ref($this) } = $instance if $cache; | |
| 66 } | |
| 415 | 67 |
| 421 | 68 return $instance; |
| 415 | 69 } |
| 70 | |
| 71 sub CreateInstance { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
72 my ( $this, $context) = @_; |
| 415 | 73 |
| 421 | 74 my $class = |
| 75 $this->norequire | |
| 76 ? $this->type | |
| 77 : $context->container->Require( $this->type ); | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
78 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
79 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
80 # determine how to pass arguments |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
81 if (isarray($this->args)) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
82 # if args is an array ref, pass it as list |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
83 return $class->new(map $context->Activate($_), @{$this->args}); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
84 } elsif (ishash($this->args)) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
85 # if args is a hash ref, pass it as list |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
86 my %args; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
87 while(my ($k,$v) = each %{$this->args}) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
88 $args{$k} = $context->Activate($v); |
| 421 | 89 } |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
90 return $class->new(%args); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
91 } elsif(defined $this->args) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
92 # otherwise pass it as a single argument |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
93 return $class->new($context->Activate($this->args)); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
94 } else { |
| 421 | 95 return $class->new(); |
| 96 } | |
| 415 | 97 } |
| 98 | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
99 sub GetName { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
100 shift->_name; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
101 } |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
102 |
| 415 | 103 1; |
