X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FComponent.pm;h=b38361e2daa9c4951070fa454a4ca0e1bde95cf2;hp=4b12fdab40932773a3a4addc2d8c79888862bec8;hb=baf6a3dbf99be93d8e8bd9b986a95ad1d81a61ca;hpb=9488795ca5244087fc99828f92d70769d9f80b22 diff --git a/lib/Catalyst/Component.pm b/lib/Catalyst/Component.pm index 4b12fda..b38361e 100644 --- a/lib/Catalyst/Component.pm +++ b/lib/Catalyst/Component.pm @@ -62,7 +62,7 @@ sub new { # Temporary fix, some components does not pass context to constructor my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {}; - return $self->NEXT::new( { %{ $self->config }, %{$arguments} } ); + return $self->NEXT::new( $self->merge_config_hashes( $self->config, $arguments ) ); } =head2 COMPONENT($c, $arguments) @@ -92,7 +92,7 @@ sub COMPONENT { } else { my $class = ref $self || $self; - my $new = { %{ $self->config }, %{$arguments} }; + my $new = $self->merge_config_hashes( $self->config, $arguments ); return bless $new, $class; } } @@ -116,8 +116,10 @@ sub config { $self->_config( $config = {} ); } if (@_) { - $config = { %{$config}, %{@_ > 1 ? {@_} : $_[0]} }; - $self->_config($config); + my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} }; + $self->_config( + $self->merge_config_hashes( $config, $newconfig ) + ); } return $config; } @@ -132,6 +134,32 @@ sub process { . " did not override Catalyst::Component::process" ); } +=head2 $c->merge_hash_config( $hashref, $hashref ) + +Merges two hashes together recursively, giving right-hand precedence. + +=cut + +sub merge_config_hashes { + my ( $self, $lefthash, $righthash ) = @_; + + my %merged = %$lefthash; + for my $key ( keys %$righthash ) { + my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH'; + my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH'; + if( $right_ref and $left_ref ) { + $merged{ $key } = $self->merge_config_hashes( + $lefthash->{ $key }, $righthash->{ $key } + ); + } + else { + $merged{ $key } = $righthash->{ $key }; + } + } + + return \%merged; +} + =head1 OPTIONAL METHODS =head2 ACCEPT_CONTEXT($c, @args)