X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst.pm;h=a0c4d4068ee5f88413d7b19760d34ff33c0db7e1;hp=d93707fcd26f0c6c52b191687f125f150d1fedb0;hb=836e1134dfc70e064464c366a44ebb6aabfa1648;hpb=8327e2e21fc9ea72c357e876f868001ad9712474 diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index d93707f..a0c4d40 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -178,6 +178,14 @@ C. use Catalyst qw/My::Module/; +If your plugin starts with a name other than C, you can +fully qualify the name by using a unary plus: + + use Catalyst qw/ + My::Module + +Fully::Qualified::Plugin::Name + /; + Special flags like C<-Debug> and C<-Engine> can also be specified as arguments when Catalyst is loaded: @@ -534,12 +542,7 @@ loads and instantiates the given class. sub plugin { my ( $class, $name, $plugin, @args ) = @_; - $plugin->require; - - if ( my $error = $UNIVERSAL::require::ERROR ) { - Catalyst::Exception->throw( - message => qq/Couldn't load instant plugin "$plugin", "$error"/ ); - } + $class->_register_plugin($plugin, 1); eval { $plugin->import }; $class->mk_classdata($name); @@ -1841,25 +1844,63 @@ Sets up plugins. =cut -sub setup_plugins { - my ( $class, $plugins ) = @_; +=head2 $c->registered_plugins + +Returns a sorted list of the plugins which have either been stated in the +import list or which have been added via C<< MyApp->plugin(@args); >>. + +If passed a given plugin name, it will report a boolean value indicating +whether or not that plugin is loaded. A fully qualified name is required if +the plugin name does not begin with C. + + if ($c->registered_plugins('Some::Plugin')) { + ... + } + +=cut - $plugins ||= []; - for my $plugin ( reverse @$plugins ) { +{ + my %PLUGINS; + sub registered_plugins { + my $proto = shift; + return sort keys %PLUGINS unless @_; + my $plugin = shift; + return 1 if exists $PLUGINS{$plugin}; + return exists $PLUGINS{"Catalyst::Plugin::$plugin"}; + } - $plugin = "Catalyst::Plugin::$plugin"; + sub _register_plugin { + my ( $proto, $plugin, $instant ) = @_; + my $class = ref $proto || $proto; $plugin->require; - if ($@) { + if ( my $error = $@ ) { + my $type = $instant ? "instant " : ''; Catalyst::Exception->throw( - message => qq/Couldn't load plugin "$plugin", "$@"/ ); + message => qq/Couldn't load ${type}plugin "$plugin", $error/ ); } - { + $PLUGINS{$plugin} = 1; + unless ($instant) { no strict 'refs'; unshift @{"$class\::ISA"}, $plugin; } + return $class; + } + + sub setup_plugins { + my ( $class, $plugins ) = @_; + + $plugins ||= []; + for my $plugin ( reverse @$plugins ) { + + unless ( $plugin =~ s/\A\+// ) { + $plugin = "Catalyst::Plugin::$plugin"; + } + + $class->_register_plugin($plugin); + } } }