From: Chris W. <la...@us...> - 2005-02-13 20:27:40
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4874/lib/OpenInteract2 Modified Files: Controller.pm Log Message: use OI2::ActionResolver framework to figure out what action to run and move some of our functionality there Index: Controller.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Controller.pm,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** Controller.pm 24 Jan 2005 16:57:55 -0000 1.24 --- Controller.pm 13 Feb 2005 20:27:31 -0000 1.25 *************** *** 4,11 **** use strict; ! use base qw( Class::Accessor::Fast Class::Factory ); use Log::Log4perl qw( get_logger ); - use OpenInteract2::Context qw( CTX ); use OpenInteract2::Constants qw( :log ); use OpenInteract2::Exception qw( oi_error ); --- 4,11 ---- use strict; ! use base qw( Class::Accessor::Fast Class::Factory Class::Observable ); use Log::Log4perl qw( get_logger ); use OpenInteract2::Constants qw( :log ); + use OpenInteract2::Context qw( CTX ); use OpenInteract2::Exception qw( oi_error ); *************** *** 17,72 **** __PACKAGE__->mk_accessors( @FIELDS ); - my ( $NONE_ACTION, $NOTFOUND_ACTION ); - - sub initialize_default_actions { - my ( $class ) = @_; - return if ( $NONE_ACTION and $NOTFOUND_ACTION ); - $NONE_ACTION = CTX->lookup_action_none; - $NOTFOUND_ACTION = CTX->lookup_action_not_found; - } - sub new { my ( $class, $request, $response ) = @_; $log ||= get_logger( LOG_ACTION ); ! my $action = $class->_find_action( $request ); my $impl_type = $action->controller; my $impl_class = $class->_get_controller_implementation_class( $impl_type ); $log->is_debug && ! $log->debug( "Controller for [Action: ", $action->name, "] ", ! "to use [Controller Type: $impl_type] ", ! "[Controller class: $impl_class]" ); my $self = bless( {}, $impl_class ); $self->type( $impl_type ); - $self->initial_action( $action ); - # This will probably remain undocumented for a bit... it would be - # nice to be able to add other observers at request-time to an - # action but I don't want to create a framework without any use - # cases... - - # Add a filter (observer) at runtime to the main action. So you - # could do: - # - # /news/display/?news_id=55&OI_FILTER=pittsburghese - # - # and have the news item be translated to da burg. You could even - # do: - # - # /news/display/?news_id=55&OI_FILTER=pittsburghese&OI_FILTER=bork - # - # and have it run through the yinzer AND the bork filter. - - my @filter_add = $request->param( 'OI_FILTER' ); - if ( scalar @filter_add ) { - foreach my $filter_name ( @filter_add ) { - CTX->map_observer( $filter_name, $action ); - } - } - - # TODO: Why not do this with the class? hmm... - my $controller_info = CTX->lookup_controller_config( $impl_type ); $self->generator_type( $controller_info->{content_generator} ); --- 17,36 ---- __PACKAGE__->mk_accessors( @FIELDS ); sub new { my ( $class, $request, $response ) = @_; $log ||= get_logger( LOG_ACTION ); ! my $action = OpenInteract2::ActionResolver->get_action( $request ); ! my $impl_type = $action->controller; my $impl_class = $class->_get_controller_implementation_class( $impl_type ); $log->is_debug && ! $log->debug( "Action '", $action->name, "' to use controller ", ! "[type: $impl_type] [class: $impl_class]" ); my $self = bless( {}, $impl_class ); $self->type( $impl_type ); $self->initial_action( $action ); my $controller_info = CTX->lookup_controller_config( $impl_type ); $self->generator_type( $controller_info->{content_generator} ); *************** *** 74,77 **** --- 38,43 ---- $self->init; + $self->notify_observers( 'action assigned', $action ); + CTX->controller( $self ); return $self; *************** *** 82,132 **** sub execute { my $class = ref( $_[0] ) || $_[0]; ! oi_error "Subclass [$class] must override execute()"; } - # Ask the request for the action and task name, then lookup the - # action. If it's defined and found use it; if it's defined and not - # found, use the $NOTFOUND_ACTION; if it's not defined use the - # $NONE_ACTION. - - sub _find_action { - my ( $class, $request ) = @_; - $log ||= get_logger( LOG_ACTION ); - - my ( $action_name, $task_name ) = - ( $request->action_name, $request->task_name ); - my ( $action ); - if ( $action_name ) { - $log->is_debug && - $log->debug( "Trying [Action: $action_name] [Task: $task_name] ", - "in controller" ); - $action = eval { - CTX->lookup_action( $action_name, - { REQUEST_URL => $request->url_initial } ) - }; - if ( $@ ) { - $log->warn( "Caught exception from Context trying to lookup ", - "action '$action_name': $@\nUsing action ", - "specified for 'notfound'" ); - $action = $NOTFOUND_ACTION->clone(); - } - else { - $action->task( $task_name ) if ( $task_name ); - } - } - else { - $log->is_debug && - $log->debug( "Using action specified for 'none': ", - "'", $NONE_ACTION->name, "'" ); - $action = $NONE_ACTION->clone(); - } - $log->is_debug && - $log->debug( 'Found action in controller ', - '[Action: ', $action->name, '] ', - '[Task: ', $action->task, ']' ); - return $action; - } - sub _get_controller_implementation_class { my ( $class, $controller_type ) = @_; --- 48,55 ---- sub execute { my $class = ref( $_[0] ) || $_[0]; ! oi_error "Subclass '$class' must override execute()"; } sub _get_controller_implementation_class { my ( $class, $controller_type ) = @_; *************** *** 176,180 **** =head1 SYNOPSIS ! # In whatever adapter you're using you'll have this: my $controller = eval { --- 99,103 ---- =head1 SYNOPSIS ! # In your adapter: my $controller = eval { *************** *** 191,216 **** =head1 DESCRIPTION ! Object that determines which action gets executed and what happens to ! its content. Typically its content gets placed into a larger template (see L<OpenInteract2::Controller::MainTemplate>), but you can perform other tasks as well. =head1 METHODS =head2 Class methods ! B<initialize_default_actions()> ! Class method called by ! L<OpenInteract2::Setup::InitializeControllers>. This finds and stores ! the actions under the 'action_info.none' and 'action_info.not_found' ! server configuration keys. These are returned by the context from the ! C<lookup_action_none()> and C<lookup_action_not_found()> methods. ! B<new( $request, $response )> ! Find the action to create from the data in C<$request>. Given the ! action, determine which controller implementation to use and ! instantiate an object of that type and return it. =head2 Object methods --- 114,146 ---- =head1 DESCRIPTION ! The controller determines from the URL or other identifier which ! action gets executed and what happens the content that action ! generates. Typically that content gets placed into a larger template (see L<OpenInteract2::Controller::MainTemplate>), but you can perform other tasks as well. + In the big picture, the controller is instantiated and invoked from + the adapter (see L<OpenInteract2::Manual::Architecture>) and is really + the gateway to the whole content generation process. + =head1 METHODS =head2 Class methods ! B<new( $request, $response )> ! Find the action to create from the data in C<$request>. We do this by ! passing the request to a series of L<OpenInteract2::ActionResolver> ! objects, each of which looks at the URL (or other information) from ! the C<$request> and decides if it should create an ! L<OpenInteract2::Action> object from it. ! Once we get the action object we ask it for its controller class, ! instantiate an object of that class and assign that controller to the ! context. ! We also notify all the controller observers (classes in ! C<OpenInteract2::Observer::Controller>) with 'action assigned' and the ! action created. =head2 Object methods |