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=00e62faa7590cafa889b9543da282d21e9e1d49f;hb=836e1134dfc70e064464c366a44ebb6aabfa1648;hpb=df912746b14abdadf70ae982f4b79ab459f08377 diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 00e62fa..a0c4d40 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -21,8 +21,6 @@ use Scalar::Util qw/weaken/; use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use attributes; -use JSON; -use File::Slurp; __PACKAGE__->mk_accessors( qw/counter request response state action stack namespace/ @@ -49,11 +47,11 @@ our $DETACH = "catalyst_detach\n"; require Module::Pluggable::Fast; # Helper script generation -our $CATALYST_SCRIPT_GEN = 26; +our $CATALYST_SCRIPT_GEN = 27; __PACKAGE__->mk_classdata($_) for qw/components arguments dispatcher engine log dispatcher_class - engine_class context_class request_class response_class/; + engine_class context_class request_class response_class setup_finished/; __PACKAGE__->dispatcher_class('Catalyst::Dispatcher'); __PACKAGE__->engine_class('Catalyst::Engine::CGI'); @@ -180,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: @@ -373,7 +379,7 @@ sub component { if ( exists $c->components->{$try} ) { my $comp = $c->components->{$try}; - if ( ref $comp && $comp->can('ACCEPT_CONTEXT') ) { + if ( eval { $comp->can('ACCEPT_CONTEXT'); } ) { return $comp->ACCEPT_CONTEXT($c); } else { return $comp } @@ -447,14 +453,24 @@ sub view { Returns or takes a hashref containing the application's configuration. - __PACKAGE__->config({ db => 'dsn:SQLite:foo.db' }); + __PACKAGE__->config( { db => 'dsn:SQLite:foo.db' } ); -You can also use a L config file like myapp.json in your +You can also use a L config file like myapp.yml in your applications home directory. - { - "db": "dsn:SQLite:foo.db" - } + --- + db: dsn:SQLite:foo.db + +=cut + +sub config { + my $c = shift; + + $c->log->warn("Setting config after setup has been run is not a good idea.") + if ( @_ and $c->setup_finished ); + + $c->NEXT::config(@_); +} =head2 $c->debug @@ -526,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); @@ -592,18 +603,6 @@ sub setup { $class->setup_home( delete $flags->{home} ); - # JSON config support - my $confpath = $class->config->{file} - || $class->path_to( - ( Catalyst::Utils::appprefix( ref $class || $class ) . '.json' ) ); - my $conf = {}; - if ( -f $confpath ) { - my $content = read_file("$confpath"); - $conf = jsonToObj($content); - } - my $oldconf = $class->config; - $class->config( { %$oldconf, %$conf } ); - $class->setup_log( delete $flags->{log} ); $class->setup_plugins( delete $flags->{plugins} ); $class->setup_dispatcher( delete $flags->{dispatcher} ); @@ -693,6 +692,8 @@ EOF $class->log->info("$name powered by Catalyst $Catalyst::VERSION"); } $class->log->_flush() if $class->log->can('_flush'); + + $class->setup_finished(1); } =head2 $c->uri_for( $path, [ @args ] ) @@ -719,12 +720,15 @@ sub uri_for { $namespace = '' if $path =~ /^\//; $path =~ s/^\///; + my $params = (scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {}); + # join args with '/', or a blank string my $args = ( scalar @args ? '/' . join( '/', @args ) : '' ); $args =~ s/^\/// unless $path; my $res = URI->new_abs( URI->new_abs( "$path$args", "$basepath$namespace" ), $base ) ->canonical; + $res->query_form(%$params); $res; } @@ -1585,7 +1589,7 @@ sub setup_components { } Catalyst::Exception->throw( message => -qq/Couldn't instantiate component "$component", "new() didn't return a object"/ +qq/Couldn't instantiate component "$component", "COMPONENT() didn't return a object"/ ) unless ref $instance; return $instance; @@ -1840,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); >>. - $plugins ||= []; - for my $plugin ( reverse @$plugins ) { +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. - $plugin = "Catalyst::Plugin::$plugin"; + if ($c->registered_plugins('Some::Plugin')) { + ... + } + +=cut + +{ + 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"}; + } + + 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); + } } }