Context for TestAppPluginWithConstructor
[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
41 __END__
42
43 =head1 NAME
44
45 Catalyst::Config - Catalyst config role
46
47 =head1 METHODS
48
49 =head2 $app->config
50
51 =head2 $app->merge_config_hashes
52
53 =head1 SEE ALSO
54
55 L<Catalyst>, 
56
57 =head1 AUTHORS
58
59 Catalyst Contributors, see Catalyst.pm
60
61 =head1 COPYRIGHT
62
63 This library is free software. You can redistribute it and/or modify it under
64 the same terms as Perl itself.
65
66 =cut
67
68