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