Removed WithCOMPONENT, renamed My::External::Class
André Walker [Sat, 6 Aug 2011 05:33:41 +0000 (02:33 -0300)]
lib/Catalyst/IOC/ConstructorInjection.pm
lib/Catalyst/IOC/Service/WithCOMPONENT.pm [deleted file]
t/lib/TestAppCustomContainer/External/Class.pm [moved from t/lib/My/External/Class.pm with 100% similarity]

index 711f2f2..0f112a0 100644 (file)
@@ -5,8 +5,7 @@ extends 'Bread::Board::ConstructorInjection';
 
 with 'Bread::Board::Service::WithClass',
      'Bread::Board::Service::WithParameters',
-     'Bread::Board::Service::WithDependencies',
-     'Catalyst::IOC::Service::WithCOMPONENT';
+     'Bread::Board::Service::WithDependencies';
 
 has config_key => (
     is         => 'ro',
@@ -14,31 +13,53 @@ has config_key => (
     lazy_build => 1,
 );
 
-sub _build_config_key {
-    Catalyst::Utils::class2classsuffix( shift->class );
-}
+sub _build_config_key { Catalyst::Utils::class2classsuffix( shift->class ) }
+
+sub _build_constructor_name { 'COMPONENT' }
 
-# FIXME - how much of this should move to ::WithCOMPONENT?
 sub get {
     my $self = shift;
 
     my $constructor = $self->constructor_name;
     my $component   = $self->class;
-    my $params      = $self->params;
-    my $config      = $params->{config}->{ $self->config_key } || {};
+    my $config      = $self->param('config')->{ $self->config_key } || {};
     # FIXME - Is depending on the application name to pass into constructors here a good idea?
     #         This makes app/ctx split harder I think.. Need to think more here, but I think
     #         we want to pass the application in as a parameter when building the service
     #         rather than depending on the app name, so that later, when the app becomes an instance
     #         then it'll get passed in, and components can stash themselves 'per app instance'
-    my $app_name    = $params->{application_name};
+    my $app_name    = $self->param('application_name');
 
     # Stash catalyst_component_name in the config here, so that custom COMPONENT
     # methods also pass it. local to avoid pointlessly shitting in config
     # for the debug screen, as $component is already the key name.
     local $config->{catalyst_component_name} = $component;
 
-    return $component->$constructor( $app_name, $config );
+    unless ( $component->can( $constructor ) ) {
+        # FIXME - make some deprecation warnings
+        return $component;
+    }
+
+    my $instance = eval { $component->$constructor( $app_name, $config ) };
+
+    if ( my $error = $@ ) {
+        chomp $error;
+        Catalyst::Exception->throw(
+            message => qq/Couldn't instantiate component "$component", "$error"/
+        );
+    }
+    elsif (!blessed $instance) {
+        my $metaclass = Moose::Util::find_meta($component);
+        my $method_meta = $metaclass->find_method_by_name($constructor);
+        my $component_method_from = $method_meta->associated_metaclass->name;
+        my $value = defined($instance) ? $instance : 'undef';
+        Catalyst::Exception->throw(
+            message =>
+            qq/Couldn't instantiate component "$component", $constructor() method (from $component_method_from) didn't return an object-like value (value was $value)./
+        );
+    }
+
+    return $instance;
 }
 
 __PACKAGE__->meta->make_immutable;
diff --git a/lib/Catalyst/IOC/Service/WithCOMPONENT.pm b/lib/Catalyst/IOC/Service/WithCOMPONENT.pm
deleted file mode 100644 (file)
index 97a08ea..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-package Catalyst::IOC::Service::WithCOMPONENT;
-use Moose::Role;
-
-with 'Bread::Board::Service';
-
-sub _build_constructor_name { 'COMPONENT' }
-
-around 'get' => sub {
-    my ( $orig, $self ) = @_;
-
-    my $constructor = $self->constructor_name;
-    my $component   = $self->class;
-
-    unless ( $component->can( $constructor ) ) {
-        # FIXME - make some deprecation warnings
-        return $component;
-    }
-
-    my $instance = eval { $self->$orig() };
-
-    if ( my $error = $@ ) {
-        chomp $error;
-        Catalyst::Exception->throw(
-            message => qq/Couldn't instantiate component "$component", "$error"/
-        );
-    }
-    elsif (!blessed $instance) {
-        my $metaclass = Moose::Util::find_meta($component);
-        my $method_meta = $metaclass->find_method_by_name($constructor);
-        my $component_method_from = $method_meta->associated_metaclass->name;
-        my $value = defined($instance) ? $instance : 'undef';
-        Catalyst::Exception->throw(
-            message =>
-            qq/Couldn't instantiate component "$component", $constructor() method (from $component_method_from) didn't return an object-like value (value was $value)./
-        );
-    }
-
-    return $instance;
-};
-
-no Moose::Role;
-1;
-
-__END__
-
-=pod
-
-=head1 NAME
-
-Catalyst::IOC::Service::WithCOMPONENT
-
-=head1 DESCRIPTION
-
-=head1 METHODS
-
-=head1 AUTHORS
-
-Catalyst Contributors, see Catalyst.pm
-
-=head1 COPYRIGHT
-
-This library is free software. You can redistribute it and/or modify it under
-the same terms as Perl itself.
-
-=cut