X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=768ad238520c1d384ed1318124b0ba0fa893a539;hb=f45789b1af1b76c31c498f4b713ab48a5f28ba0c;hp=599a95d83bbb6d039f7243b30c970e90e640bd46;hpb=ae4e40a7d5968d8086a86bff588a0989f167d5db;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 599a95d..768ad23 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -3,6 +3,7 @@ package Catalyst; use strict; use base 'Catalyst::Base'; use UNIVERSAL::require; +use Catalyst::Exception; use Catalyst::Log; use Catalyst::Utils; use Text::ASCIITable; @@ -123,34 +124,209 @@ Returns a hashref containing your applications settings. =cut sub import { - my ( $self, @options ) = @_; + my ( $class, @arguments ) = @_; + my $caller = caller(0); + + if ( $caller eq 'main' ) { + return; + } # Prepare inheritance - unless ( $caller->isa($self) ) { + unless ( $caller->isa($class) ) { no strict 'refs'; - push @{"$caller\::ISA"}, $self; + push @{"$caller\::ISA"}, $class; } - + if ( $caller->engine ) { - return; # Catalyst is already initialized + $caller->log->warn( qq/Attempt to re-initialize "$caller"/ ); + return; + } + + # Process options + my $flags = { }; + + foreach (@arguments) { + + if ( /^-Debug$/ ) { + $flags->{log} = ( $flags->{log} ) ? 'debug,' . $flags->{log} : 'debug'; + } + elsif (/^-(\w+)=?(.*)$/) { + $flags->{ lc $1 } = $2; + } + else { + push @{ $flags->{plugins} }, $_; + } + } + + $caller->setup_log ( delete $flags->{log} ); + $caller->setup_plugins ( delete $flags->{plugins} ); + $caller->setup_dispatcher ( delete $flags->{dispatcher} ); + $caller->setup_engine ( delete $flags->{engine} ); + $caller->setup_home ( delete $flags->{home} ); + + for my $flag ( sort keys %{ $flags } ) { + + if ( my $code = $caller->can( 'setup_' . $flag ) ) { + &$code( $caller, delete $flags->{$flag} ); + } + else { + $caller->log->warn(qq/Unknown flag "$flag"/); + } + } + + $caller->log->warn( "You are running an old helper script! " + . "Please update your scripts by regenerating the " + . "application and copying over the new scripts." ) + if ( $ENV{CATALYST_SCRIPT_GEN} + && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) ); + + + if ( $caller->debug ) { + + my @plugins = (); + + { + no strict 'refs'; + @plugins = grep { /^Catalyst::Plugin/ } @{"$caller\::ISA"}; + } + + if ( @plugins ) { + my $t = Text::ASCIITable->new; + $t->setOptions( 'hide_HeadRow', 1 ); + $t->setOptions( 'hide_HeadLine', 1 ); + $t->setCols('Class'); + $t->setColWidth( 'Class', 75, 1 ); + $t->addRow($_) for @plugins; + $caller->log->debug( "Loaded plugins:\n" . $t->draw ); + } + + my $dispatcher = $caller->dispatcher; + my $engine = $caller->engine; + my $home = $caller->config->{home}; + + $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/); + $caller->log->debug(qq/Loaded engine "$engine"/); + + $home + ? ( -d $home ) + ? $caller->log->debug(qq/Found home "$home"/) + : $caller->log->debug(qq/Home "$home" doesn't exist/) + : $caller->log->debug(q/Couldn't find home/); + } +} + +=item $c->engine + +Contains the engine class. + +=item $c->log + +Contains the logging object. Unless it is already set Catalyst sets this up with a +C object. To use your own log class: + + $c->log( MyLogger->new ); + $c->log->info("now logging with my own logger!"); + +Your log class should implement the methods described in the C +man page. + +=item $c->plugin( $name, $class, @args ) + +Instant plugins for Catalyst. +Classdata accessor/mutator will be created, class loaded and instantiated. + + MyApp->plugin( 'prototype', 'HTML::Prototype' ); + + $c->prototype->define_javascript_functions; + +=cut + +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"/ + ); + } + + eval { $plugin->import }; + $class->mk_classdata($name); + my $obj; + eval { $obj = $plugin->new(@args) }; + + if ( $@ ) { + Catalyst::Exception->throw( + message => qq/Couldn't instantiate instant plugin "$plugin", "$@"/ + ); + } + + $class->$name($obj); + $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/) + if $class->debug; +} + +=item $c->setup_dispatcher + +=cut + +sub setup_dispatcher { + my ( $class, $dispatcher ) = @_; + + if ( $dispatcher ) { + $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher; + } + + if ( $ENV{CATALYST_DISPATCHER} ) { + $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER}; + } + + if ( $ENV{ uc($class) . '_DISPATCHER' } ) { + $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' }; } - unless ( $caller->log ) { - $caller->log( Catalyst::Log->new ); + unless ( $dispatcher ) { + $dispatcher = 'Catalyst::Dispatcher'; } - # Debug? - if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($caller) . '_DEBUG' } ) { + $dispatcher->require; + + if ( $@ ) { + Catalyst::Exception->throw( + message => qq/Couldn't load dispatcher "$dispatcher", "$@"/ + ); + } + + { no strict 'refs'; - *{"$caller\::debug"} = sub { 1 }; - $caller->log->debug('Debug messages enabled'); + push @{"$class\::ISA"}, $dispatcher; } - my $engine = 'Catalyst::Engine::CGI'; - my $dispatcher = 'Catalyst::Dispatcher'; + $class->dispatcher($dispatcher); +} + +=item $c->setup_engine - if ( $ENV{MOD_PERL} ) { +=cut + +sub setup_engine { + my ( $class, $engine ) = @_; + + if ( $engine ) { + $engine = 'Catalyst::Engine::' . $engine; + } + + if ( $ENV{CATALYST_ENGINE} ) { + $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE}; + } + + if ( $ENV{ uc($class) . '_ENGINE' } ) { + $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' }; + } + + if ( ! $engine && $ENV{MOD_PERL} ) { my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/; @@ -187,7 +363,9 @@ sub import { } else { - die( qq/Unsupported mod_perl version: $ENV{MOD_PERL}/ ); + Catalyst::Exception->throw( + message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/ + ); } } @@ -196,170 +374,99 @@ sub import { } else { - die( qq/Unsupported mod_perl: $ENV{MOD_PERL}/ ); + Catalyst::Exception->throw( + message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/ + ); } } - $caller->log->info( "You are running an old helper script! " - . "Please update your scripts by regenerating the " - . "application and copying over the new scripts." ) - if ( $ENV{CATALYST_SCRIPT_GEN} - && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) ); - - # Process options - my @plugins; - foreach (@options) { - - if (/^\-Debug$/) { - next if $caller->debug; - no strict 'refs'; - *{"$caller\::debug"} = sub { 1 }; - $caller->log->debug('Debug messages enabled'); - } - - elsif (/^-Dispatcher=(.*)$/) { - $dispatcher = "Catalyst::Dispatcher::$1"; - } - - elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" } - elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) } - - else { - my $plugin = "Catalyst::Plugin::$_"; - - $plugin->require; - - if ($@) { die qq/Couldn't load plugin "$plugin", "$@"/ } - else { - push @plugins, $plugin; - no strict 'refs'; - push @{"$caller\::ISA"}, $plugin; - } - } - + unless ( $engine ) { + $engine = 'Catalyst::Engine::CGI'; } - # Plugin table - my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } ); - $t->setCols('Class'); - $t->setColWidth( 'Class', 75, 1 ); - $t->addRow($_) for @plugins; - $caller->log->debug( 'Loaded plugins', $t->draw ) - if ( @plugins && $caller->debug ); - - # Dispatcher - $dispatcher = "Catalyst::Dispatcher::$ENV{CATALYST_DISPATCHER}" - if $ENV{CATALYST_DISPATCHER}; - my $appdis = $ENV{ uc($caller) . '_DISPATCHER' }; - $dispatcher = "Catalyst::Dispatcher::$appdis" if $appdis; + $engine->require; - $dispatcher->require; - die qq/Couldn't load dispatcher "$dispatcher", "$@"/ if $@; - { - no strict 'refs'; - push @{"$caller\::ISA"}, $dispatcher; + if ( $@ ) { + Catalyst::Exception->throw( + message => qq/Couldn't load engine "$engine", "$@"/ + ); } - $caller->dispatcher($dispatcher); - $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/) if $caller->debug; - - # Engine - $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}" - if $ENV{CATALYST_ENGINE}; - my $appeng = $ENV{ uc($caller) . '_ENGINE' }; - $engine = "Catalyst::Engine::$appeng" if $appeng; - - $engine->require; - die qq/Couldn't load engine "$engine", "$@"/ if $@; { no strict 'refs'; - push @{"$caller\::ISA"}, $engine; + push @{"$class\::ISA"}, $engine; } - $caller->engine($engine); - $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug; - - # Find home - my $home = Catalyst::Utils::home($caller); + $class->engine($engine); +} - if ( my $h = $ENV{CATALYST_HOME} ) { +=item $c->setup_home - $home = $h if -d $h; +=cut - unless ( -e _ ) { - $caller->log->warn(qq/CATALYST_HOME does not exist "$h"/); - } +sub setup_home { + my ( $class, $home ) = @_; - unless ( -e _ && -d _ ) { - $caller->log->warn(qq/CATALYST_HOME is not a directory "$h"/); - } + if ( $ENV{CATALYST_HOME} ) { + $home = $ENV{CATALYST_HOME}; } - if ( my $h = $ENV{ uc($caller) . '_HOME' } ) { - - $home = $h if -d $h; - - unless ( -e _ ) { - my $e = uc($caller) . '_HOME'; - $caller->log->warn(qq/$e does not exist "$h"/) - } + if ( $ENV{ uc($class) . '_HOME' } ) { + $home = $ENV{ uc($class) . '_HOME' }; + } - unless ( -e _ && -d _ ) { - my $e = uc($caller) . '_HOME'; - $caller->log->warn(qq/$e is not a directory "$h"/); - } + unless ( $home ) { + $home = Catalyst::Utils::home($class); } - - if ( $caller->debug ) { - $home - ? ( -d $home ) - ? $caller->log->debug(qq/Found home "$home"/) - : $caller->log->debug(qq/Home "$home" doesn't exist/) - : $caller->log->debug(q/Couldn't find home/); + + if ( $home ) { + $class->config->{home} = $home; + $class->config->{root} = dir($home)->subdir('root'); } - $caller->config->{home} = $home || ''; - $caller->config->{root} = defined $home ? dir($home)->subdir('root') : ''; } -=item $c->engine +=item $c->setup_log -Contains the engine class. +=cut -=item $c->log +sub setup_log { + my ( $class, $debug ) = @_; -Contains the logging object. Unless it is already set Catalyst sets this up with a -C object. To use your own log class: + unless ( $class->log ) { + $class->log( Catalyst::Log->new ); + } - $c->log( MyLogger->new ); - $c->log->info("now logging with my own logger!"); + if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) { + no strict 'refs'; + *{"$class\::debug"} = sub { 1 }; + $class->log->debug('Debug messages enabled'); + } +} -Your log class should implement the methods described in the C -man page. +=item $c->setup_plugins -=item $c->plugin( $name, $class, @args ) +=cut -Instant plugins for Catalyst. -Classdata accessor/mutator will be created, class loaded and instantiated. +sub setup_plugins { + my ( $class, $plugins ) = @_; - MyApp->plugin( 'prototype', 'HTML::Prototype' ); + for my $plugin ( @$plugins ) { - $c->prototype->define_javascript_functions; + $plugin = "Catalyst::Plugin::$plugin"; -=cut + $plugin->require; -sub plugin { - my ( $class, $name, $plugin, @args ) = @_; - $plugin->require; - my $error = $UNIVERSAL::require::ERROR; - die qq/Couldn't load instant plugin "$plugin", "$error"/ if $error; - eval { $plugin->import }; - $class->mk_classdata($name); - my $obj; - eval { $obj = $plugin->new(@args) }; - die qq/Couldn't instantiate instant plugin "$plugin", "$@"/ if $@; - $class->$name($obj); - $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/) - if $class->debug; + if ( $@ ) { + Catalyst::Exception->throw( + message => qq/Couldn't load plugin "$plugin", "$@"/ + ); + } + + { + no strict 'refs'; + push @{"$class\::ISA"}, $plugin; + } + } } =back