view lib/IMPL/Config/ReferenceDescriptor.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents cc2cf8c0edc2
children 7798345304bc
line wrap: on
line source

package IMPL::Config::ReferenceDescriptor;
use strict;

use IMPL::Exception();
use IMPL::declare {
	base => [
		'IMPL::Object'             => undef,
		'IMPL::Config::Descriptor' => undef
	],
	props => [
		reference => 'ro',
		services  => 'ro',
		lazy      => 'ro',
		optional  => 'ro',
		default   => 'ro',
		_name     => 'rw'
	]
};

sub CTOR {
	my ( $this, $ref, %opts ) = @_;

	$this->reference($ref)
	  or die IMPL::InvalidArgumentException->new('ref');

	$this->lazy( $opts{lazy} )         if $opts{lazy};
	$this->optional( $opts{optional} ) if $opts{optional};
	$this->default( $opts{default} )
	  if $opts{optional} and exists $opts{default};

	$this->_name( 'ref ' . $ref );
}

sub Activate {
	my ( $this, $context ) = @_;

	$context->EnterScope( $this->_name, $this->services );

	my $ref = $this->reference;
	my %opts;
	$opts{default} = $this->default
	  if $this->optional;

	if ( $this->lazy ) {
		my $clone = $context->Clone();
		return sub {
			$clone->Resolve( $ref, %opts );
		};
	}
	else {
		return $context->Resolve( $ref, %opts );
	}

	$context->LeaveScope();
}

1;

__END__

=pod

=head1 NAME

C<IMPL::Config::ReferenceDescriptor> - A descriptor which is points to the other service

=head1 MEMBERS

=head2 PROPS

=head3 reference

=head3 lazy

=head3 optional

=head3 default

=head2 METHODS

=head3 CTOR($ref, %opts)

=head3 Activate($context)

Returns a result of the activation of the service specified by the C<reference> property.

If the reference is lazy the method returns a closure.The C<$context> will be cloned and the clone will be used to create the closure. 
=cut