Catalyst-Plugin-InjectionHelpers
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/InjectionHelpers.pm view on Meta::CPAN
MyApp->config(
'Model::SingletonA' => {
-inject => {
from_class=>'MyApp::Singleton',
adaptor=>'Application',
roles=>['MyApp::Role::Foo'],
method=>'new',
},
aaa => 100,
},
'Model::SingletonB' => {
-inject => {
from_class=>'MyApp::Singleton',
adaptor=>'Application',
method=>sub {
my ($adaptor_instance, $from_class, $app, %args) = @_;
return $from_class->new(aaa=>$args{arg});
},
arg => 300,
},
);
MyApp->setup;
Alternatively you can use the 'inject_components' class method:
package MyApp;
use Catalyst 'InjectionHelpers';
MyApp->inject_components(
'Model::SingletonA' => {
from_class=>'MyApp::Singleton',
adaptor=>'Application',
roles=>['MyApp::Role::Foo'],
method=>'new',
},
'Model::SingletonB' => {
from_class=>'MyApp::Singleton',
adaptor=>'Application',
method=>sub {
my ($adaptor_instance, $from_class, $app, %args) = @_;
return $class->new(aaa=>$args{arg});
},
},
);
MyApp->config(
'Model::SingletonA' => { aaa=>100 },
'Model::SingletonB' => { arg=>300 },
);
MyApp->setup;
The first method is a better choice if you need to alter how your injections work
based on configuration that is controlled per environment.
=head1 DESCRIPTION
B<NOTE> Starting with C<VERSION> 0.012 there is a breaking change in the number
of arguments that the C<method> and C<from_code> callbacks get. If you need to
keep backwards compatibility you should set the version flag to 1:
MyApp->config(
'Plugin::InjectionHelpers' => { version => 1 },
## Additional configuration as needed
);
This plugin enhances the build in component injection features of L<Catalyst>
(since v5.90090) to make it easy to bring non L<Catalyst::Component> classes
into your application. You may consider using this for what you often used
L<Catalyst::Model::Adaptor> in the past for (although there is no reason to
stop using that if you are doing so, its not a 'broken' approach, but for the
very simple cases this might suffice and allow you to reduce the number of nearly
empty 'boilerplate' classes in your application.)
You should be familiar with how component injection works in newer versions of
L<Catalyst> (v5.90090+).
It also experimentally supports a mechanism for dependency injection (that is
the ability to set other componements as initialization arguments, similar to
how you might see this work with inversion of control frameworks such as
L<Bread::Board>.) Author has no plan to move this past experimental status; he
is merely publishing code that he's used on jobs where the code worked for the
exact cases he was using it for the purposes of easing long term maintainance
on those projects. If you like this feature and would like to see it stablized
it will be on you to help the author validate it; its not impossible more changes
and pontentially breaking changes will be needed to make that happen, and its
also not impossible that changes to core L<Catalyst> would be needed as well.
Reports from users in the wild greatly appreciated.
=head1 USAGE
MyApp->config(
$model_name => +{
-inject => +{ %injection_args },
\%configuration_args;
or
MyApp->inject_components($model_name => \%injection_args);
MyApp->config($model_name => \%configuration_args);
Where C<$model_name> is the name of the component as it is in your L<Catalyst>
application (ie 'Model::User', 'View::HTML', 'Controller::Static') and C<%injection_args>
are key /values as described below:
=head2 from_class
This is the full namespace of the class you are adapting to use as a L<Catalyst>
component. Example 'MyApp::Class'.
=head2 from_code
This is a codereference that generates your component instance. Used when you
don't have a class you wish to adapt (handy for prototyping or small components).
MyApp->inject_components(
'Model::Foo' => {
from_code => sub {
my ($app_ctx, %args) = @_;
( run in 2.617 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )