[Catalyst] External plugins (continued)

Jason Kohles email at jasonkohles.com
Sun Feb 19 19:23:03 CET 2006


On 2/17/06, Ovid <publiustemp-catalyst at yahoo.com> wrote:
> Hi all,
>
> So it sounds like there's a bit of consensus to be able to specify a
> MyApp::Plugin directory just like we have
> MyApp::(?:Controller|Model|View) directories.  Until (and unless)
> someone wants to write the code for this, is the unary plus model
> acceptable?
>
I wish I had jumped on this earlier, as there seems to have been a lot
of discussion on the subject, but I wanted to put in my two bits and
share the code I've been using to do this.  Mostly I'm curious if
people have already tried this solution and run into problems with it,
as I haven't used it that much yet, but it seems to work for me...

I handled it by overriding setup_plugins in my application base class,
to allow a "module prefix search path" rather than having
"Catalyst::Plugin::" hardcoded in...

MyApp->config(
    plugin_search_path => [qw(MyApp::Plugin MyGlobal::Plugin Catalyst::Plugin)]
);

sub setup_plugins {
    my ( $class, $plugins ) = @_;

    my $search = $class->config->{plugin_search_path} || [qw(Catalyst::Plugin)];
    foreach my $plugin ( reverse @{ $plugins || [] } ) {
        foreach my $path (@{ $search }) {
            my $module = "$path\::$plugin";
            $module->require;
            if ($@) {
                next if $@ =~ /^Can't locate /; # skip it if it just
doesn't exist
                Catalyst::Exception->throw(
                    message => qq/Couldn't load plugin "$module", "$@"/
                );
            }
            { no strict 'refs'; unshift @{"$class\::ISA"}, $module; }
        }
    }
}

--
Jason Kohles
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire



More information about the Catalyst mailing list