X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FComponent.pm;h=07202d3210230780204c1607750f35a89b4bb7d0;hb=f8ad6ea56eeb4d3a3d5d77cf502651712dfb5e62;hp=339cab8994a43c350dc3f31231f62952a7b622d4;hpb=e7f1cf73b4e0e5863e901aaa0e6bda2e39bd0edc;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Component.pm b/lib/Catalyst/Component.pm index 339cab8..07202d3 100644 --- a/lib/Catalyst/Component.pm +++ b/lib/Catalyst/Component.pm @@ -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,11 +47,23 @@ 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 METHODS +=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. -=over 4 +This call happens for every $c->comp/controller/model/view call. + + +=head1 METHODS -=item new($c) +=head2 new($c) =cut @@ -64,30 +76,56 @@ sub new { return $self->NEXT::new( { %{ $self->config }, %{$arguments} } ); } -# remember to leave blank lines between the consecutive =item's -# otherwise the pod tools don't recognize the subsequent =items +=head2 COMPONENT($c) -=item $c->config +=cut -=item $c->config($hashref) +sub COMPONENT { + my ( $self, $c ) = @_; -=item $c->config($key, $value, ...) + # 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 + +=head2 $c->config + +=head2 $c->config($hashref) + +=head2 $c->config($key, $value, ...) =cut 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; } -=item $c->process() +=head2 $c->process() =cut @@ -97,8 +135,6 @@ sub process { . " did not override Catalyst::Component::process" ); } -=back - =head1 SEE ALSO L, L, L, L.