remove duff croak added by mistake
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Component.pm
index 38e87aa..07202d3 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
 use NEXT;
 
-__PACKAGE__->mk_classdata($_) for qw/_config/;
+__PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
 
 =head1 NAME
 
@@ -47,6 +47,20 @@ This is the universal base class for Catalyst components
 It provides you with a generic new() for instantiation through Catalyst's
 component loader with config() support and a process() method placeholder.
 
+=head1 ACCEPT_CONTEXT
+
+Catalyst components are normally initalized during server startup, either
+as a Class or a Instance. However, some compoents require information about
+the current request. To do so, they can implement an ACCEPT_CONTEXT method.
+
+The ACCEPT_CONTEXT method is called on the component as initalized at startup,
+with the current $c object, and should return itself. It can do whatever it 
+likes with $c, such as extracting a path to use in the component or something
+similar. 
+
+This call happens for every $c->comp/controller/model/view call.
+
+
 =head1 METHODS
 
 =head2 new($c)
@@ -62,6 +76,31 @@ sub new {
     return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
 }
 
+=head2 COMPONENT($c)
+
+=cut
+
+sub COMPONENT {
+    my ( $self, $c ) = @_;
+
+    # Temporary fix, some components does not pass context to constructor
+    my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
+
+    if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
+        return $new;
+    }
+    else {
+        if ( my $new = $self->new( $c, $arguments ) ) {
+            return $new;
+        }
+        else {
+            my $class = ref $self || $self;
+            my $new = { %{ $self->config }, %{$arguments} };
+            return bless $new, $class;
+        }
+    }
+}
+
 # remember to leave blank lines between the consecutive =head2's
 # otherwise the pod tools don't recognize the subsequent =head2s
 
@@ -75,14 +114,15 @@ sub new {
 
 sub config {
     my $self = shift;
-    $self->_config( {} ) unless $self->_config;
+    my $config = $self->_config;
+    unless ($config) {
+        $self->_config( $config = {} );
+    }
     if (@_) {
-        my $config = @_ > 1 ? {@_} : $_[0];
-        while ( my ( $key, $val ) = each %$config ) {
-            $self->_config->{$key} = $val;
-        }
+        $config = { %{$config}, %{@_ > 1 ? {@_} : $_[0]} };
+        $self->_config($config);
     }
-    return $self->_config;
+    return $config;
 }
 
 =head2 $c->process()