From: John Napiorkowski Date: Mon, 27 Apr 2015 23:36:10 +0000 (-0500) Subject: add config_for X-Git-Tag: 5.90089_004~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=ea66d7ea834e3d8db9dd2c6fd2b6facbac9ceb3a add config_for --- diff --git a/Changes b/Changes index 2655f99..63a406c 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,11 @@ # This file documents the revision history for Perl extension Catalyst. +5.90089_004 - TDB + - New application setup hook 'config_for' which allows one to get the + canonical application configuration for a controller, view or model, or + a plugin. Can also be used to override and adapt what configuration is + retrieved. + 5.90089_003 - 2015-04-27 - Fixed an issue where a delayed controller that did ACCEPT_CONTEXT would raise an error when registering its actions. diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index fc278fb..a3608be 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -3022,8 +3022,7 @@ sub setup_component { return $component; } - my $suffix = Catalyst::Utils::class2classsuffix( $component ); - my $config = $class->config->{ $suffix } || {}; + my $config = $class->config_for($component); # Stash catalyst_component_name in the config here, so that custom COMPONENT # methods also pass it. local to avoid pointlessly shitting in config # for the debug screen, as $component is already the key name. @@ -3061,6 +3060,33 @@ sub setup_component { return $instance; } +=head2 $app->config_for( $component_name ) + +Return the application level configuration (which is not yet merged with any +local component configuration, via $component_class->config) for the named +component or component object. Example: + + MyApp->config( + 'Model::Foo' => { a => 1, b => 2}, + ); + + my $config = MyApp->config_for('MyApp::Model::Foo'); + +In this case $config is the hashref C< {a=>1, b=>2} >. + +This is also handy for looking up configuration for a plugin, to make sure you follow +existing L standards for where a plugin should put its configuration. + +=cut + +sub config_for { + my ($class, $component_name) = @_; + my $component_suffix = Catalyst::Utils::class2classsuffix($component_name); + my $config = $class->config->{ $component_suffix } || {}; + + return $config; +} + =head2 $c->setup_dispatcher Sets up dispatcher.