Removed references to BlockInjection, and created validation for parameters in accept...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / Container.pm
index 7d40bf5..4409816 100644 (file)
@@ -8,7 +8,7 @@ use Devel::InnerPackage ();
 use Hash::Util qw/lock_hash/;
 use MooseX::Types::LoadableClass qw/ LoadableClass /;
 use Moose::Util;
-use Catalyst::IOC::BlockInjection;
+use Catalyst::IOC::ConstructorInjection;
 use Module::Pluggable::Object ();
 use namespace::autoclean;
 
@@ -76,7 +76,6 @@ sub BUILD {
         config_local_suffix
         config_path
         locate_components
-        setup_components
     /;
 
     $self->add_sub_container(
@@ -396,70 +395,52 @@ sub build_locate_components_service {
                 %$config
             );
 
-            # XXX think about ditching this sort entirely
-            my @comps = sort { length $a <=> length $b } $locator->plugins;
-
-            return \@comps;
+            return [ $locator->plugins ];
         },
         dependencies => [ depends_on('application_name'), depends_on('config') ],
     );
 }
 
-sub build_setup_components_service {
+sub setup_components {
     my $self = shift;
+    my $class = $self->resolve( service => 'application_name' );
+    my @comps = @{ $self->resolve( service => 'locate_components' ) };
+    my %comps = map { $_ => 1 } @comps;
+    my $deprecatedcatalyst_component_names = 0;
 
-    return Bread::Board::BlockInjection->new(
-        lifecycle => 'Singleton',
-        name      => 'setup_components',
-        block     => sub {
-            my $s     = shift;
-            my $class = $s->param('application_name');
-            my @comps = @{ $s->param( 'locate_components' ) };
-            my %comps = map { $_ => 1 } @comps;
-            my $deprecatedcatalyst_component_names = 0;
-
-            for my $component ( @comps ) {
+    for my $component ( @comps ) {
 
-                # We pass ignore_loaded here so that overlay files for (e.g.)
-                # Model::DBI::Schema sub-classes are loaded - if it's in @comps
-                # we know M::P::O found a file on disk so this is safe
+        # We pass ignore_loaded here so that overlay files for (e.g.)
+        # Model::DBI::Schema sub-classes are loaded - if it's in @comps
+        # we know M::P::O found a file on disk so this is safe
 
-                Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded => 1 } );
-            }
+        Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded => 1 } );
+    }
 
-            for my $component (@comps) {
-                $self->add_component( $component, $class );
-                # FIXME - $instance->expand_modules() is broken
-                my @expanded_components = $self->expand_component_module( $component );
-
-                if (
-                    !$deprecatedcatalyst_component_names &&
-                    ($deprecatedcatalyst_component_names = $component =~ m/::[CMV]::/) ||
-                    ($deprecatedcatalyst_component_names = grep { /::[CMV]::/ } @expanded_components)
-                ) {
-                    # FIXME - should I be calling warn here?
-                    $class->log->warn(qq{Your application is using the deprecated ::[MVC]:: type naming scheme.\n}.
-                        qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
-                    );
-                }
+    for my $component (@comps) {
+        $self->add_component( $component, $class );
+        # FIXME - $instance->expand_modules() is broken
+        my @expanded_components = $self->expand_component_module( $component );
+
+        if (
+            !$deprecatedcatalyst_component_names &&
+            ($deprecatedcatalyst_component_names = $component =~ m/::[CMV]::/) ||
+            ($deprecatedcatalyst_component_names = grep { /::[CMV]::/ } @expanded_components)
+        ) {
+            # FIXME - should I be calling warn here?
+            $class->log->warn(qq{Your application is using the deprecated ::[MVC]:: type naming scheme.\n}.
+                qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
+            );
+        }
 
-                for my $component (@expanded_components) {
-                    $self->add_component( $component, $class )
-                        unless $comps{$component};
-                }
-            }
+        for my $component (@expanded_components) {
+            $self->add_component( $component, $class )
+                unless $comps{$component};
+        }
+    }
 
-            # FIXME - how can this be done?
-            #$s->param('/model')->make_single_default;
-            #$s->param('/view')->make_single_default;
-        },
-        dependencies => [
-            depends_on('application_name'),
-            depends_on('locate_components'),
-            #depends_on('/model'),
-            #depends_on('/view'),
-        ],
-    );
+    $self->get_sub_container('model')->make_single_default;
+    $self->get_sub_container('view')->make_single_default;
 }
 
 sub _fix_syntax {
@@ -635,10 +616,23 @@ sub add_component {
     return unless $type;
 
     $self->get_sub_container($type)->add_service(
-        Catalyst::IOC::BlockInjection->new(
-            lifecycle => 'Singleton', # FIXME?
+        Catalyst::IOC::ConstructorInjection->new(
             name      => $name,
-            block     => sub { $self->setup_component( $component, $class ) },
+            class     => $component,
+            dependencies => [
+                depends_on( '/application_name' ),
+                depends_on( '/config' ),
+            ],
+            parameters => {
+                suffix => {
+                    isa => 'Str',
+                    default => Catalyst::Utils::class2classsuffix( $component ),
+                },
+                accept_context_args => {
+                    isa => 'ArrayRef',
+                    default => sub { [] },
+                },
+            },
         )
     );
 }
@@ -665,47 +659,6 @@ sub _get_component_type_name {
     return (undef, $component);
 }
 
-# FIXME ugly and temporary
-# Just moved it here the way it was, so we can work on it here in the container
-sub setup_component {
-    my ( $self, $component, $class ) = @_;
-
-    unless ( $component->can( 'COMPONENT' ) ) {
-        return $component;
-    }
-
-    # FIXME I know this isn't the "Dependency Injection" way of doing things,
-    # its just temporary
-    my $suffix = Catalyst::Utils::class2classsuffix( $component );
-    my $config = $self->resolve(service => 'config')->{ $suffix } || {};
-
-    # 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;
-
-    my $instance = eval { $component->COMPONENT( $class, $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('COMPONENT');
-        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", COMPONENT() method (from $component_method_from) didn't return an object-like value (value was $value)./
-        );
-    }
-
-    return $instance;
-}
-
 sub expand_component_module {
     my ( $class, $module ) = @_;
     return Devel::InnerPackage::list_packages( $module );
@@ -822,8 +775,6 @@ Searches for components in all containers. If $component is the full class name,
 
 Finds components that match a given regexp. Used internally, by find_component.
 
-=head2 setup_component
-
 =head2 _fix_syntax
 
 =head2 _config_substitutions
@@ -868,7 +819,7 @@ setup for the application.  By default, it will use L<Module::Pluggable>.
 Specify a C<setup_components> config option to pass additional options directly
 to L<Module::Pluggable>.
 
-=head2 build_setup_components_service
+=head2 setup_components
 
 =head1 AUTHORS