Forgot to commit - ripped the config bits of ::Component out into a role so that...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Config.pm
1 package Catalyst::Config;
2 use Moose::Role;
3 use Class::MOP ();
4 use Catalyst::Utils ();
5 use namespace::autoclean;
6
7 sub config {
8     my $self = shift;
9     # Uncomment once sane to do so
10     #Carp::cluck("config method called on instance") if ref $self;
11     my $config = $self->_config || {};
12     if (@_) {
13         my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
14         $self->_config(
15             $self->merge_config_hashes( $config, $newconfig )
16         );
17     } else {
18         # this is a bit of a kludge, required to make
19         # __PACKAGE__->config->{foo} = 'bar';
20         # work in a subclass.
21         # TODO maybe this should be a ClassData option?
22         my $class = blessed($self) || $self;
23         my $meta = Class::MOP::get_metaclass_by_name($class);
24         unless ($meta->has_package_symbol('$_config')) {
25             # Call merge_hashes to ensure we deep copy the parent
26             # config onto the subclass
27             $self->_config( Catalyst::Utils::merge_hashes($config, {}) );
28         }
29     }
30     return $self->_config;
31 }
32
33 sub merge_config_hashes {
34     my ( $self, $lefthash, $righthash ) = @_;
35
36     return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
37 }
38
39 1;
40