File Coverage

lib/MooseX/DIC/ServiceFactory/Moose.pm
Criterion Covered Total %
statement 44 47 93.6
branch 9 14 64.2
condition n/a
subroutine 12 13 92.3
pod 0 2 0.0
total 65 76 85.5


line stmt bran cond sub pod time code
1             package MooseX::DIC::ServiceFactory::Moose;
2              
3 5     5   2642 use Moose;
  5         13  
  5         39  
4             with 'MooseX::DIC::ServiceFactory';
5 5     5   34405 use namespace::autoclean;
  5         11  
  5         53  
6              
7             with 'MooseX::DIC::Loggable';
8              
9 5     5   461 use aliased 'MooseX::DIC::UnregisteredServiceException';
  5         11  
  5         38  
10 5     5   987 use aliased 'MooseX::DIC::ContainerConfigurationException';
  5         12  
  5         21  
11 5     5   748 use aliased 'MooseX::DIC::ServiceCreationException';
  5         11  
  5         18  
12 5     5   555 use Try::Tiny;
  5         15  
  5         2493  
13              
14             has container => ( is => 'ro', does => 'MooseX::DIC::Container', required => 1 );
15              
16             sub build_service {
17 22     22 0 54 my ( $self, $service_meta ) = @_;
18              
19 22         91 my %dependencies = $self->build_dependencies_for($service_meta);
20              
21 21         37 my $service;
22             try {
23 21     21   1513 $service = $service_meta->class_name->new(%dependencies);
24             } catch {
25 0     0   0 MooseX::DIC::ServiceCreationException->throw(
26             message => "Error while building an injected service: $_" );
27 21         151 };
28              
29 21         7856 return $service;
30             }
31              
32             sub build_dependencies_for {
33 22     22 0 39 my ($self,$service_meta) = @_;
34              
35 22         760 my $class_meta = $service_meta->class_name->meta;
36              
37             # Build the to-be-injected dependencies of
38             # the object
39 22         429 my %dependencies = ();
40              
41 22         38 while(my ($name,$dependency) = each(%{$service_meta->dependencies})) {
  27         886  
42 6         216 my $attribute = $class_meta->get_attribute($dependency->name);
43 6         336 my $service_type = $attribute->type_constraint->name;
44              
45 6 50       423 if($self->container->has_service($service_type)) {
46              
47 6 100       214 if( $dependency->scope eq 'object' ) {
    50          
48 3         92 $dependencies{ $dependency->name } = $self->container->get_service($service_type);
49             } elsif( $dependency->scope eq 'request') {
50             # It is a configuration error to ask for a request-injection of
51             # a singleton object. It may indicate a misconception or a config
52             # typo.
53 3         75 my $attribute_service_meta = $self->container->get_service_metadata($service_type);
54 3 100       82 ContainerConfigurationException->throw( message =>
55             "A singleton-scoped service cannot be injected into a request-injected attribute"
56             ) if $attribute_service_meta->scope eq 'singleton';
57              
58             # Replace the getter with a custom proxy function
59 2         13 $attribute->remove_accessors;
60             $class_meta->add_method(
61             $dependency->name,
62             sub {
63 4     4   18 my ( $object, $value ) = @_;
        4      
        4      
64              
65             # This is only a setter. Trying to write is an error
66 4 50       10 ContainerException->throw( message =>
67             "A request-injected service accessor is read-only, it cannot be used as a setter"
68             ) if $value;
69              
70 4         102 return $self->container->get_service($service_type);
71             }
72 2         363 );
73              
74             # We must pass a valid attribute value in case the attribute is required. It will never
75             # get used, though.
76 2 100       154 if( $attribute->is_required) {
77 1         31 $dependencies{ $dependency->name } = $self->container->get_service($service_type);
78             }
79             } else {
80 0         0 ContainerConfigurationException->throw( message => "Injection scope of dependencies can only
81             be of type 'request' or 'object'" );
82             }
83             } else {
84 0 0       0 UnregisteredServiceException->throw(service => $service_type )
85             if ( $attribute->is_required );
86             }
87             }
88              
89 21         71 return %dependencies;
90              
91             }
92              
93             __PACKAGE__->meta->make_immutable;
94              
95             1;