MasonX-Interp-WithCallbacks-1.19000755000765000024 011600017366 16174 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/Build.PL000444000765000024 207311600017366 17627 0ustar00davidstaff000000000000use Module::Build; my $build_pkg = eval { require Apache::TestMB } ? 'Apache::TestMB' : 'Module::Build'; $build_pkg->new( module_name => 'MasonX::Interp::WithCallbacks', license => 'perl', configure_requires => { 'Module::Build' => '0.2701' }, build_requires => { 'Module::Build' => '0.2701', 'Test::More' => '0.17', }, recommends => { 'Test::Pod' => '1.20', 'Apache::TestMB' => 0 }, requires => { 'HTML::Mason' => '1.23', 'Test::Simple' => '0.17', 'Class::Container' => '0.09', 'Params::CallbackRequest' => '1.15', }, add_to_cleanup => ['t/mason'], meta_merge => { resources => { homepage => 'http://search.cpan.org/dist/MasonX-Interp-WithCallbacks/', bugtracker => 'http://github.com/theory/masonx-interp-withcallbacks/issues/', repository => 'http://github.com/theory/masonx-interp-withcallbacks/', } }, )->create_build_script; MasonX-Interp-WithCallbacks-1.19/Changes000444000765000024 1120111600017366 17637 0ustar00davidstaff000000000000Revision history for Perl extension MasonX::Interp::WithCallbacks. 1.19 2011-06-21T04:33:14 - Fixed a pasto in the "Support" section. - Moved repostitory to [GitHub](https://github.com/theory/class-meta/). - Fixed installation documentation in the READE so that it demonstrates using `Build.PL rather than `Makefile.PL`, as the latter is not included in the distribution. 1.18 2008-05-03T23:26:42 - Added "configure_requires" parameter to Build.PL and added Test::Pod to the "recommends" parameter. - Updated POD test to require Test::Pod 1.20 or later. - Fixed test failures under Perl 5.6. Reported by Slaven Rezic via CPAN-Testers. - Added a link to the Subversion repository. - Fixed the test suite to run properly with Apache::TestMB under mod_perl 2 as well as mod_perl 1. - Fixed failing test with older versions of CGI, such as that distributed with Perl 5.6.2. 1.17 2007-07-24T19:37:47 - Fixed test failures with Mason 1.36. Patch from Niko Tyni. 1.16 2006-07-21T17:00:55 - Replaced broken passthrough Makefile.PL with a real Makefile.PL. Reported by Scott Lanning. 1.15 2006-05-26T23:02:03 - Added missing t/htdocs/alt.mc to MANIFEST and therefore to the distribution. D'oh! 1.14 2006-05-26T22:55:15 - Now passing the the interp object to callbacks via the new 'requster' parameter the request() method of Params::CallbacRequest, introduced in version 1.15 of that module. - Added the comp_path() accessor. This so that callbacks can change it if they want, by calling $cb->requester->comp_path($comp_path). - Fixed CGI redirect tests to be compliant with the new "302 Found" header created by CGI.pm. It used to be "302 Moved". 1.13 2004-11-19T02:37:50 - Makefile.PL no longer requires Apache::TestMB, since it may not be installed, but MasonX::Interp::WithCallbacks can still be installed with Module::Build. Reported by Scott Lanning. - Added Apache::TestMB to the list of recommended modules in Build.PL. 1.12 2004-06-28T17:04:23 - Fixed Apache tests to eliminate failure under more recent versions of Apache::Test and LWP. - Changed "%PARAMS" to the correct Mason hash, "%ARGS", in the synopsis. Reported by Mike Dorman. - Ported installer from ExtUtils::MakeMaker to Module::Build using the newly released Apache::TestMB. 1.11 Wed Oct 8 20:12:20 2003 - Require Class::Container 0.09 or later. It seems that older versions don't work properly. - Updated Apache test to skip tests if the Apache server doesn't run mod_perl. Suggesed by Geoffrey Young. - Stopped checking for presence of LWP in Apache tests, since Apache::TestClient should work fine for the simple test requests. - The module now requires HTML::Mason 1.23, rather than just the requirements in Makefile.PL. - Require Params::CallbackRequest 1.11 or later to be in sync with request callback order of execution. - Fixed failing tests of notes functionality under Apache. Not sure how they ever passed, frankly. 1.10 Mon Sep 8 21:15:42 2003 - Code moved over from MasonX::ApacheHandler::WithCallbacks, which is deprecated. Differences from that class are as follows. - Callback handling code from MasonX::ApacheHandler::WithCallbacks has been migrated to Params::CallbackRequest. - Code from MasonX::CallbackHandler has been migrated to Params::Callback. - MasonX::CallbackTester has been removed, since it isn't necessary in a non-mod_perl environment. - Params::CallbackRequest::Exceptions supplies the exceptions thrown by Params::CallbackRequest, since that module is not strictly connected to Mason. - Changed the request_args() accessor from MasonX::CallbackHandler to params() in Params::Callback, to reflect the idea that this is a generic parameter-triggered callback architecture. - Replaced the ah() accessor, since the callback controller isn't a Mason ApacheHandler anymore, with cb_request() in Params::Callback. - Added cb_request() accessor to MasonX::Interp::WithCallbacks. - Replaced the "exec_null_cb_values" parameter from MasonX::ApacheHandler::WithCallbaks, which had defaulted to true, with "ignore_nulls" in Params::CallbackRequest, which defaults to false. - Added Params::CallbackRequest notes() interface, which copies all notes to the Mason request notes() interface before the request executes. MasonX-Interp-WithCallbacks-1.19/MANIFEST000444000765000024 52211600017366 17441 0ustar00davidstaff000000000000Build.PL Changes lib/MasonX/Interp/WithCallbacks.pm MANIFEST This list of files README.md t/01basic.t t/02priority.t t/03keys.t t/04errors.t t/05object.t t/06object_request.t t/07combined.t t/08apache.t t/09cgi.t t/10pod.t t/conf/extra.conf.in t/htdocs/alt.mc t/htdocs/dhandler t/lib/TestCallbacks.pm t/lib/TieOut.pm META.yml META.json MasonX-Interp-WithCallbacks-1.19/META.json000444000765000024 316111600017366 17753 0ustar00davidstaff000000000000{ "abstract" : "Mason callback support via Params::CallbackRequest.", "author" : [ "David E. Wheeler " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.38, CPAN::Meta::Converter version 2.110440", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "MasonX-Interp-WithCallbacks", "prereqs" : { "build" : { "requires" : { "Module::Build" : "0.2701", "Test::More" : "0.17" } }, "configure" : { "requires" : { "Module::Build" : "0.2701" } }, "runtime" : { "recommends" : { "Apache::TestMB" : 0, "Test::Pod" : "1.20" }, "requires" : { "Class::Container" : "0.09", "HTML::Mason" : "1.23", "Params::CallbackRequest" : "1.15", "Test::Simple" : "0.17" } } }, "provides" : { "MasonX::Interp::WithCallbacks" : { "file" : "lib/MasonX/Interp/WithCallbacks.pm", "version" : "1.19" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "http://github.com/theory/masonx-interp-withcallbacks/issues/" }, "homepage" : "http://search.cpan.org/dist/MasonX-Interp-WithCallbacks/", "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "http://github.com/theory/masonx-interp-withcallbacks/" } }, "version" : "1.19" } MasonX-Interp-WithCallbacks-1.19/META.yml000444000765000024 175611600017366 17613 0ustar00davidstaff000000000000--- abstract: 'Mason callback support via Params::CallbackRequest.' author: - 'David E. Wheeler ' build_requires: Module::Build: 0.2701 Test::More: 0.17 configure_requires: Module::Build: 0.2701 dynamic_config: 1 generated_by: 'Module::Build version 0.38, CPAN::Meta::Converter version 2.110440' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: MasonX-Interp-WithCallbacks provides: MasonX::Interp::WithCallbacks: file: lib/MasonX/Interp/WithCallbacks.pm version: 1.19 recommends: Apache::TestMB: 0 Test::Pod: 1.20 requires: Class::Container: 0.09 HTML::Mason: 1.23 Params::CallbackRequest: 1.15 Test::Simple: 0.17 resources: bugtracker: http://github.com/theory/masonx-interp-withcallbacks/issues/ homepage: http://search.cpan.org/dist/MasonX-Interp-WithCallbacks/ license: http://dev.perl.org/licenses/ repository: http://github.com/theory/masonx-interp-withcallbacks/ version: 1.19 MasonX-Interp-WithCallbacks-1.19/README.md000444000765000024 374311600017366 17617 0ustar00davidstaff000000000000MasonX/Interp/WithCallbacks version 1.19 ======================================== MasonX::Interp::WithCallbacks subclasses HTML::Mason::Interp in order to provide a [Mason](http://search.cpan.org/dist/HTML-Mason) callback system built on [Params::CallbackRequest](http://search.cpan.org/dist/params-callbackrequest/). Callbacks may be either code references provided to the `new()` constructor, or methods defined in subclasses of Params::Callback. Callbacks are triggered either for every request or by specially named keys in the Mason request arguments, and all callbacks are executed at the beginning of a request, just before Mason creates and executes the request component stack. This module brings support for a sort of plugin architecture based on Params::CallbackRequest to Mason. Mason then executes code before executing any components. This approach allows you to carry out logical processing of data submitted from a form, to affect the contents of the Mason request arguments (and thus the `%ARGS` hash in components), and even to redirect or abort the request before Mason handles it. Installation ------------ To install this module type the following: perl Build.PL ./Build ./Build test ./Build install Dependencies ------------ This module requires these other modules and libraries: * Params::CallbackRequest 1.10 or later * HTML::Mason 1.23 or later * Params::Validate 0.59 or later * Exception::Class 1.10 or later The object-oriented callback interface requires Perl 5.6 or later and these other modules and libraries: * Attribute::Handlers 0.77 or later * Clas::ISA The test suite requires: * Test::Simple 0.17 or later Testing of this module with HTML::Mason::ApacheHandler requires: * Apache::Test 1.03 or later * mod_perl 1.22 or later * LWP Copyright and License --------------------- Copyright (c) 2003-2011 David E. Wheeler. Some Rights Reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. MasonX-Interp-WithCallbacks-1.19/lib000755000765000024 011600017366 16742 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/lib/MasonX000755000765000024 011600017366 20147 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/lib/MasonX/Interp000755000765000024 011600017366 21410 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/lib/MasonX/Interp/WithCallbacks.pm000444000765000024 10345311600017366 24664 0ustar00davidstaff000000000000package MasonX::Interp::WithCallbacks; use strict; use HTML::Mason qw(1.23); use HTML::Mason::Interp; use HTML::Mason::Exceptions (); use Params::CallbackRequest; use vars qw($VERSION @ISA); @ISA = qw(HTML::Mason::Interp); $VERSION = '1.19'; Params::Validate::validation_options ( on_fail => sub { HTML::Mason::Exception::Params->throw( join '', @_ ) } ); use HTML::Mason::MethodMaker( read_only => [qw(cb_request)], read_write => [qw(comp_path)], ); # We'll use this code reference to eval arguments passed in via httpd.conf # PerlSetVar directives. my $eval_directive = { convert => sub { return 1 if ref $_[0]->[0]; for (@{$_[0]}) { $_ = eval $_ } return 1; }}; __PACKAGE__->valid_params ( default_priority => { type => Params::Validate::SCALAR, parse => 'string', default => 5, descr => 'Default callback priority' }, default_pkg_key => { type => Params::Validate::SCALAR, parse => 'string', default => 'DEFAULT', descr => 'Default package key' }, callbacks => { type => Params::Validate::ARRAYREF, parse => 'list', optional => 1, callbacks => $eval_directive, descr => 'Callback specifications' }, pre_callbacks => { type => Params::Validate::ARRAYREF, parse => 'list', optional => 1, callbacks => $eval_directive, descr => 'Callbacks to be executed before argument callbacks' }, post_callbacks => { type => Params::Validate::ARRAYREF, parse => 'list', optional => 1, callbacks => $eval_directive, descr => 'Callbacks to be executed after argument callbacks' }, cb_classes => { type => Params::Validate::ARRAYREF | Params::Validate::SCALAR, parse => 'list', optional => 1, descr => 'List of calback classes from which to load callbacks' }, ignore_nulls => { type => Params::Validate::BOOLEAN, parse => 'boolean', default => 0, descr => 'Execute callbacks with null values' }, cb_exception_handler => { type => Params::Validate::CODEREF, parse => 'code', optional => 1, descr => 'Callback execution exception handler' }, ); sub new { my $class = shift; my $self = $class->SUPER::new(@_); # This causes everything to be validated twice, but it shouldn't matter # much, since interp objects won't be created very often. my $exh = delete $self->{cb_exception_handler}; $self->{cb_request} = Params::CallbackRequest->new ( leave_notes => 1, ($exh ? (exception_handler => $exh) : ()), map { $self->{$_} ? ($_ => delete $self->{$_}) : () } keys %{ __PACKAGE__->valid_params } ); $self; } sub make_request { my ($self, %p) = @_; # We have to grab the parameters and copy them into a hash. my %params = @{$p{args}}; $self->{comp_path} = $p{comp}; # Grab the apache request object, if it exists. my $apache_req = $p{apache_req} || $self->delayed_object_params('request', 'apache_req') || $self->delayed_object_params('request', 'cgi_request'); # Execute the callbacks. my $ret = $self->{cb_request}->request( \%params, requester => $self, $apache_req ? ( apache_req => $apache_req ) : (), ); # Abort the request if that's what the callbacks want. unless (ref $ret) { $self->{cb_request}->clear_notes; HTML::Mason::Exception::Abort->throw( error => 'Callback->abort was called', aborted_value => $ret, ); } # Copy the parameters back -- too much copying! $p{args} = [%params]; $p{comp} = $self->{comp_path}; # Get the request, copy the notes, and continue. my $req = $self->SUPER::make_request(%p); # Should I use the same reference? %{$req->notes} = %{$self->{cb_request}->notes}; return $req; } # We override this method in order to clear out all the callback notes # at the end of the Mason request. sub purge_code_cache { my $self = shift; $self->{cb_request}->clear_notes; $self->SUPER::purge_code_cache; } 1; __END__ =head1 NAME MasonX::Interp::WithCallbacks - Mason callback support via Params::CallbackRequest. =head1 SYNOPSIS In your Mason component: % if (exists $ARGS{answer}) {

Answer: <% $ARGS{answer} %>

% } else {

Enter an epoch time:

% } In F: use strict; use MasonX::Interp::WithCallbacks; sub calc_time { my $cb = shift; my $params = $cb->params; my $val = $cb->value; $params->{answer} = localtime($val || time); } my $ah = HTML::Mason::ApacheHandler->new ( interp_class => 'MasonX::Interp::WithCallbacks', callbacks => [ { cb_key => 'calc_time', pkg_key => 'myCallbacker', cb => \&calc_time } ] ); sub handler { my $r = shift; $ah->handle_request($r); } Or, in a subclass of Params::Callback: package MyApp::CallbackHandler; use base qw(Params::Callback); __PACKAGE__->register_subclass( class_key => 'myCallbacker' ); sub calc_time : Callback { my $self = shift; my $params = $self->params; my $val = $cb->value; $params->{answer} = localtime($val || time); } And then, in F: # Load order is important here! use MyApp::CallbackHandler; use MasonX::Interp::WithCallbacks; my $ah = HTML::Mason::ApacheHandler->new ( interp_class => 'MasonX::Interp::WithCallbacks', cb_classes => [qw(myCallbacker)] ); sub handler { my $r = shift; $ah->handle_request($r); } Or, just use MasonX::Interp::WithCallbacks directly: use MyApp::CallbackHandler; use MasonX::Interp::WithCallbacks; my $interp = MasonX::Interp::WithCallbacks->new ( cb_classes => [qw(myCallbacker)] ); $interp->exec($comp, %args); =begin comment =head1 ABSTRACT MasonX::Interp::WithCallbacks subclasses HTML::Mason::Interp in order to provide functional and object-oriented callbacks via Params::CallbackRequest. Callbacks are executed at the beginning of a request, just before Mason creates and executes the request component stack. =end comment =head1 DESCRIPTION MasonX::Interp::WithCallbacks subclasses HTML::Mason::Interp in order to provide a Mason callback system built on L. Callbacks may be either code references provided to the C constructor, or methods defined in subclasses of Params::Callback. Callbacks are triggered either for every request or by specially named keys in the Mason request arguments, and all callbacks are executed at the beginning of a request, just before Mason creates and executes the request component stack. This module brings support for a sort of plugin architecture based on Params::CallbackRequest to Mason. Mason then executes code before executing any components. This approach allows you to carry out logical processing of data submitted from a form, to affect the contents of the Mason request arguments (and thus the C<%ARGS> hash in components), and even to redirect or abort the request before Mason handles it. Much of the documentation here is based on that in L, although it prefers using HTML form fields for its examples rather than Perl hashes. But see the Params::CallbackRequest documentation for the latest on its interface. =head1 JUSTIFICATION Why would you want to do this? Well, there are a number of reasons. Some I can think of offhand include: =over 4 =item Stricter separation of logic from presentation Most application logic handled in Mason components takes place in C<< <%init> >> blocks, often in the same component as presentation logic. By moving the application logic into Perl modules and then directing Mason to execute that code as callbacks, you obviously benefit from a cleaner separation of application logic and presentation. =item Widgitization Thanks to their ability to preprocess arguments, callbacks enable developers to develop easier-to-use, more dynamic widgets that can then be used in any and all Mason component, or even with other templating systems. For example, a widget that puts many related fields into a form (such as a date selection widget) can have its fields preprocessed by a callback (for example, to properly combine the fields into a unified date field) before the Mason component that responds to the form submission gets the data. See L for an example solution for this very problem. =item Shared Memory Callbacks are just Perl subroutines in modules, and are therefore loaded at server startup time in a mod_perl environment. Thus the memory they consume is all in the Apache parent process, and shared by the child processes. For code that executes frequently, this can be much less resource-intensive than code in Mason components, since components are loaded separately in each Apache child process (unless they're preloaded via the C parameter to the HTML::Mason::Interp constructor). =item Performance Since they're executed before Mason creates a component stack and executes the components, callbacks have the opportunity to short-circuit the Mason processing by doing something else. A good example is redirection. Often the application logic in callbacks does its thing and then redirects the user to a different page. Executing the redirection in a callback eliminates a lot of extraneous processing that would otherwise be executed before the redirection, creating a snappier response for the user. =item Testing Mason components are not easy to test via a testing framework such as Test::Harness. Subroutines in modules, on the other hand, are fully testable. This means that you can write tests in your application test suite to test your callback subroutines. =back And if those aren't enough reasons, then just consider this: Callbacks are just I =head1 USAGE MasonX::Interp::WithCallbacks uses Params::CallbackRequest for its callback architecture, and therefore supports its two different types of callbacks: those triggered by a specially named key in the Mason request arguments hash, and those executed for every request. =head2 Argument-Triggered Callbacks Argument-triggered callbacks are triggered by specially named request argument keys. These keys are constructed as follows: The package name followed by a pipe character ("|"), the callback key with the string "_cb" appended to it, and finally an optional priority number at the end. For example, if you specified a callback with the callback key "save" and the package key "world", a callback field might be added to an HTML form like this: This field, when submitted to the Mason server, would trigger the callback associated with the "save" callback key in the "world" package. If such a callback hasn't been configured, then Params::CallbackRequest will throw a Params::CallbackReuest::Exception::InvalidKey exception. Here's how to configure a functional callback when constructing your MasonX::Interp::WithCallbacks object so that that doesn't happen: my $interp = MasonX::Interp::WithCallbacks->new ( callbacks => [ { pkg_key => 'world', cb_key => 'save', cb => \&My::World::save } ] ); With this configuration, the request argument created by the above HTML form field will trigger the execution of the C<&My::World::save> subroutine. =head3 Functional Callback Subroutines Functional callbacks use a code reference for argument-triggered callbacks, and Params::CallbackRequest executes them with a single argument, a Params::Callback object. Thus, a callback subroutine will generally look something like this: sub foo { my $cb = shift; # Do stuff. } The Params::Callback object provides accessors to data relevant to the callback, including the callback key, the package key, and the request arguments (or parameters). It also includes C and C methods. See the L documentation for all the goodies. Note that all callbacks are executed in a C block, so if any of your callback subroutines C, Params::CallbackRequest will throw an Params::CallbackRequest::Exception::Execution exception If you don't like this, use the C parameter to C to install your own exception handler. =head3 Object-Oriented Callback Methods Object-oriented callback methods are defined in subclasses of Params::Callback. Unlike functional callbacks, they are not called with a Params::Callback object, but with an instance of the callback subclass. These classes inherit all the goodies provided by Params::Callback, so you can essentially use their instances exactly as you would use the Params::Callback object in functional callback subroutines. But because they're subclasses, you can add your own methods and attributes. See L for all the gory details on subclassing, along with a few examples. Generally, callback methods will look like this: sub foo : Callback { my $self = shift; # Do stuff. } As with functional callback subroutines, method callbacks are executed in a C block. Again, see the C parameter to install your own exception handler. B It's important that you C any and all MasonX::Callback subclasses I you C or C. This is to get around an issue with identifying the names of the callback methods in mod_perl. Read the comments in the MasonX::Callback source code if you're interested in learning more. =head3 The Package Key The use of the package key is a convenience so that a system with many functional callbacks can use callbacks with the same keys but in different packages. The idea is that the package key will uniquely identify the module in which each callback subroutine is found, but it doesn't necessarily have to be so. Use the package key any way you wish, or not at all: my $interp = MasonX::Interp::WithCallbacks->new ( callbacks => [ { cb_key => 'save', cb => \&My::World::save } ] ); But note that if you don't use the package key at all, you'll still need to provide one in the parameters to be submitted to C By default, that key is "DEFAULT". Such a callback field in an HTML form would then look like this: If you don't like the "DEFAULT" package name, you can set an alternative default using the C parameter to C: my $interp = MasonX::Interp::WithCallbacks->new ( callbacks => [ { cb_key => 'save', cb => \&My::World::save } ], default_pkg_name => 'MyPkg' ); Then, of course, any callbacks without a specified package key of their own will then use the custom default: =head3 The Class Key The class key is essentially a synonym for the package key, but applies more directly to object-oriented callbacks. The difference is mainly that it corresponds to an actual class, and that all Params::Callback subclasses are I to have a class key; it's not optional as it is with functional callbacks. The class key may be declared in your Params::Callback subclass like so: package MyApp::CallbackHandler; use base qw(Params::Callback); __PACKAGE__->register_subclass( class_key => 'MyCBHandler' ); The class key can also be declared by implementing a C method, like so: package MyApp::CallbackHandler; use base qw(Params::Callback); __PACKAGE__->register_subclass; use constant CLASS_KEY => 'MyCBHandler'; If no class key is explicitly defined, Params::Callback will use the subclass name, instead. In any event, the C method B be called to register the subclass with Params::Callback. See the L documentation for complete details. =head3 Priority Sometimes one callback is more important than another. For example, you might rely on the execution of one callback to set up variables needed by another. Since you can't rely on the order in which callbacks are executed (the Mason request arguments are stored in a hash, and the processing of a hash is, of course, unordered), you need a method of ensuring that the setup callback executes first. In such a case, you can set a higher priority level for the setup callback than for callbacks that depend on it. For functional callbacks, you can do it like this: my $interp = MasonX::Interp::WithCallbacks->new ( callbacks => [ { cb_key => 'setup', priority => 3, cb => \&setup }, { cb_key => 'save', cb => \&save } ] ); For object-oriented callbacks, you can define the priority right in the callback method declaration: sub setup : Callback( priority => 3 ) { my $self = shift; # ... } sub save : Callback { my $self = shift; # ... } In these examples, the "setup" callback has been configured with a priority level of "3". This ensures that it will always execute before the "save" callback, which has the default priority of "5". This is true regardless of the order of the fields in the corresponding HTML::Form: Despite the fact that the "setup" callback field appears after the "save" field (and will generally be submitted by the browser in that order), the "setup" callback will always execute first because of its higher priority. Although the "save" callback got the default priority of "5", this too can be customized to a different priority level via the C parameter to C for functional callbacks and the C to the class declaration for object-oriented callbacks For example, this functional callback configuration: my $interp = MasonX::Interp::WithCallbacks->new ( callbacks => [ { cb_key => 'setup', priority => 3, cb => \&setup }, { cb_key => 'save', cb => \&save } ], default_priority => 2 ); And this Params::Callback subclass declaration: package MyApp::CallbackHandler; use base qw(Params::Callback); __PACKAGE__->register_subclass( class_key => 'MyCBHandler', default_priority => 2 ); Will cause the "save" callback to always execute before the "setup" callback, since its priority level will default to "2". In addition, the priority level can be overridden via the form submission field itself by appending a priority level to the end of the callback field name. Hence, this example: Causes the "save" callback to execute before the "setup" callback by overriding the "save" callback's priority to level "2". Of course, any other form field that triggers the "save" callback without a priority override will still execute "save" at its configured level. =head2 Request Callbacks Request callbacks come in two separate flavors: those that execute before the argument-triggered callbacks, and those that execute after the argument-triggered callbacks. All of them execute before the Mason component stack executes. Functional request callbacks may be specified via the C and C parameters to C, respectively: my $interp = MasonX::Interp::WithCallbacks->new ( pre_callbacks => [ \&translate, \&foobarate ], post_callbacks => [ \&escape, \&negate ] ); Object-oriented request callbacks may be declared via the C and C method attributes, like so: sub translate : PreCallback { ... } sub foobarate : PreCallback { ... } sub escape : PostCallback { ... } sub negate : PostCallback { ... } In these examples, the C and C subroutines or methods will execute (in that order) before any argument-triggered callbacks are executed (none will be in these examples, since none are specified). Conversely, the C and C subroutines or methods will be executed (in that order) after all argument-triggered callbacks have been executed. And regardless of what argument-triggered callbacks may be triggered, the request callbacks will always be executed for I request. Although they may be used for different purposes, the C and C functional callback code references expect the same argument as argument-triggered functional callbacks: a Params::Callback object: sub foo { my $cb = shift; # Do your business here. } Similarly, object-oriented request callback methods will be passed an object of the class defined in the class key portion of the callback trigger -- either an object of the class in which the callback is defined, or an object of a subclass: sub foo : PostCallback { my $self = shift; # ... } Of course, the attributes of the Params::Callback or subclass object will be different than in argument-triggered callbacks. For example, the C, C, and C attributes will naturally be undefined. It will, however, be the same instance of the object passed to all other functional callbacks -- or to all other class callbacks with the same class key -- in a single request. Like the argument-triggered callbacks, request callbacks are executed in a C block, so if any of them Cs, an Params::CallbackRequest::Exception::Execution exception will be thrown. Use the C parameter to C if you don't like this. =head1 INTERFACE =head2 Parameters To The C Constructor In addition to those offered by the HTML::Mason::Interp base class, this module supports a number of its own parameters to the C constructor based on those required by Params::CallbackRequest. Each also has a corresponding F variable as well, so, if you really want to, you can use MasonX::Interp::WithCallbacks right in your F file: PerlModule MasonX::Interp::WithCallbacks PerlSetVar MasonInterpClass MasonX::Interp::WithCallbacks SetHandler perl-script PerlHandler HTML::Mason::ApacheHandler The parameters to C and their corresponding F variables are as follows: =over 4 =item C Argument-triggered functional callbacks are configured via the C parameter. This parameter is an array reference of hash references, and each hash reference specifies a single callback. The supported keys in the callback specification hashes are: =over 4 =item C Required. A string that, when found in a properly-formatted Mason request argument key, will trigger the execution of the callback. =item C Required. A reference to the Perl subroutine that will be executed when the C has been found in a Mason request argument key. Each code reference should expect a single argument: a Params::Callback object. The same instance of a Params::Callback object will be used for all functional callbacks in a single request. =item C Optional. A key to uniquely identify the package in which the callback subroutine is found. This parameter is useful in systems with many callbacks, where developers may wish to use the same C for different subroutines in different packages. The default package key may be set via the C parameter. =item C Optional. Indicates the level of priority of a callback. Some callbacks are more important than others, and should be executed before the others. Params::CallbackRequest supports priority levels ranging from "0" (highest priority) to "9" (lowest priority). The default priority for functional callbacks may be set via the C parameter. =back The parameter can also be specified via the F configuration variable C. Use C to specify several callbacks; each one should be an Cable string that converts into a hash reference as specified here. For example, to specify two callbacks, use this syntax: PerlAddVar MasonCallbacks "{ cb_key => 'foo', cb => sub { ... }" PerlAddVar MasonCallbacks "{ cb_key => 'bar', cb => sub { ... }" Note that the Cable string must be entirely on its own line in the F file. =item C This parameter accepts an array reference of code references that should be executed for I request I any other callbacks. They will be executed in the order in which they're listed in the array reference. Each code reference should expect a single Params::Callback argument. The same instance of a Params::Callback object will be used for all functional callbacks in a single request. Use pre-argument-triggered request callbacks when you want to do something with the arguments submitted for every request, such as convert character sets. The parameter can also be specified via the F configuration variable C. Use multiple C to add multiple pre-request callbacks; each one should be an Cable string that converts into a code reference: PerlAddVar MasonPreCallbacks "sub { ... }" PerlAddVar MasonPreCallbacks "sub { ... }" =item C This parameter accepts an array reference of code references that should be executed for I request I all other callbacks have been called. They will be executed in the order in which they're listed in the array reference. Each code reference should expect a single Params::Callback argument. The same instance of a Params::Callback object will be used for all functional callbacks in a single request. Use post-argument-triggered request callbacks when you want to do something with the arguments submitted for every request, such as HTML-escape their values. The parameter can also be specified via the F configuration variable C. Use multiple C to add multiple post-request callbacks; each one should be an Cable string that converts into a code reference: PerlAddVar MasonPostCallbacks "sub { ... }" PerlAddVar MasonPostCallbacks "sub { ... }" =item C An array reference listing the class keys of all of the Params::Callback subclasses containing callback methods that you want included in your MasonX::Interp::WithCallbacks object. Alternatively, the C parameter may simply be the word "ALL", in which case I Params::Callback subclasses will have their callback methods registered with your MasonX::Interp::WithCallbacks object. See the L documentation for details on creating callback classes and methods. B Be sure to C or C I after you've Cd all of the Params::Callback subclasses you need or else you won't be able to use their callback methods. The parameter can also be specified via the F configuration variable C. Use multiple C to add multiple callback class keys. But, again, be sure to load MasonX::Interp::WithCallbacks or Params::CallbackRequest I after you've loaded all of your MasonX::Callback handler subclasses: PerlModule My::CBClass PerlModule Your::CBClass PerlSetVar MasonCbClasses myCBClass PerlAddVar MasonCbClasses yourCBClass # Load MasonX::Interp::WithCallbacks last! PerlModule MasonX::Interp::WithCallbacks =item C The priority level at which functional callbacks will be executed. Does not apply to object-oriented callbacks. This value will be used in each hash reference passed via the C parameter to C that lacks a C key. You may specify a default priority level within the range of "0" (highest priority) to "9" (lowest priority). If not specified, it defaults to "5". Use the C variable to set the the C parameter in your F file: PerlSetVar MasonDefaultPriority 3 =item C The default package key for functional callbacks. Does not apply to object-oriented callbacks. This value that will be used in each hash reference passed via the C parameter to C that lacks a C key. It can be any string that evaluates to a true value, and defaults to "DEFAULT" if not specified. Use the C variable to set the the C parameter in your F file: PerlSetVar MasonDefaultPkgKey CBFoo =item C By default, Params::CallbackRequest will execute all request callbacks. However, in many situations it may be desirable to skip any callbacks that have no value for the callback field. One can do this by simply checking C<< $cb->value >> in the callback, but if you need to disable the execution of all callbacks, pass the C parameter with a true value. It is set to a false value by default. Use the C variable to set the the C parameter in your F file: PerlSetVar MasonIgnoreNulls 1 =item C When Params::CallbackRequest encounters an exception during the execution of callbacks, it normally calls C to handle the exception. But if you throw your own exceptions in your callbacks, and want to handle them differently (say, to handle them and then let the request continue), pass the C parameter a code reference to do what you need. Use the C variable to set the C parameter in your F file: MasonCbExceptionHandler "sub {...}" B This is the only parameter that differs in name from the same parameter to C<< Params::CallbackRequest->new >>. This is so that it can be easily distinguished from the possible addition of a C parameter to a future version of Mason. =back =head2 Accessor Methods All of the above parameters to C are passed to the Params::CallbackRequest constructor and deleted from the MasonX::Interp::WithCallbacks object. MasonX::Interp::WithCallbacks then contains a Params::CallbackRequest object that it uses to handle the execution of all callbacks for each request. =head3 cb_request my $interp = MasonX::Interp::WithCallbacks->new; my $cb_request = $interp->cb_request; Returns the Params::CallbackRequest object in use during the execution of C. =head3 comp_path my $comp_path = $interp->comp_path; $interp->comp_path($comp_path); Returns the component path resolved by Mason during the execution of C. The cool thing is that it can be changed during the execution of callback methods: sub change_path :Callback { my $cb = shift; my $interp = $cb->requester; $inpter->comp_path($some_other_path); } In this example, we have overridden the component path determined by the Mason resolver in favor of an alternate component, which will be executed, instead. =head2 Requester The MasonX::Interp::WithCallbacks object is available in all callback methods via the C accessor: sub access_interp :Callback { my $cb = shift; my $interp = $cb->requester; # ... } =head2 Notes $interp->cb_request->notes($key => $value); my $note = $interp->cb_request->notes($key); my $notes = $interp->cb_request->notes; The Params::CallbackRequest notes interface remains available via the C method of both Params::CallbackRequest and Params::Callback. Notes stored via this interface will be copied to the HTML::Mason::Request C interface before the execution of the request, I continue to be available for the lifetime of the Mason request via C<< $interp->cb_request->notes >>. Notes will be cleared out at the end of the request, just as with C<< $r->pnotes >>. =head1 SUPPORT This module is stored in an open L. Feel free to fork and contribute! Please file bug reports via L or by sending mail to L. =head1 SEE ALSO L handles the processing of the Mason request arguments and the execution of callbacks. See its documentation for the most up-to-date documentation of the underlying callback architecture. L objects get passed as the sole argument to all functional callbacks, and offer access to data relevant to the callback. Params::Callback also defines the object-oriented callback interface, making its documentation a must-read for anyone who wishes to create callback classes and methods. This module works with L by subclassing L. Inspired by the implementation of callbacks in Bricolage (L), it is however a completely new code base with a rather different approach. =head1 AUTHOR David E. Wheeler =head1 COPYRIGHT AND LICENSE Copyright 2003-2011 by David E. Wheeler. Some Rights Reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut MasonX-Interp-WithCallbacks-1.19/t000755000765000024 011600017366 16437 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/t/01basic.t000444000765000024 3232411600017366 20227 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More tests => 49; use HTML::Mason::Interp; BEGIN { use_ok('MasonX::Interp::WithCallbacks') } my $key = 'myCallbackTester'; my $cbs = []; ############################################################################## # Set up callback functions. ############################################################################## # Simple callback. sub simple { my $cb = shift; isa_ok( $cb, 'Params::Callback' ); isa_ok( $cb->cb_request, 'Params::CallbackRequest' ); my $params = $cb->params; $params->{result} = 'Success'; } push @$cbs, { pkg_key => $key, cb_key => 'simple', cb => \&simple }; ############################################################################## # Priorty order checking. sub priority { my $cb = shift; my $params = $cb->params; my $val = $cb->value; $val = '5' if $val eq 'def'; $params->{result} .= " $val"; } push @$cbs, { pkg_key => $key, cb_key => 'priority', cb => \&priority }; ############################################################################## # Hash value callback. sub hash_check { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; my $val = $cb->value; # For some reason, if I don't eval this, then the code in the rest of # the function doesn't run! eval { isa_ok( $val, 'HASH' ) }; $params->{result} = "$val" } push @$cbs, { pkg_key => $key, cb_key => 'hash_check', cb => \&hash_check }; ############################################################################## # Code value callback. sub code_check { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; my $val = $cb->value; # For some reason, if I don't eval this, then the code in the rest of # the function doesn't run! eval { isa_ok( $val, 'CODE' ) }; $params->{result} = $val->(); } push @$cbs, { pkg_key => $key, cb_key => 'code_check', cb => \&code_check }; ############################################################################## # Count the number of times the callback executes. sub count { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; $params->{result}++; } push @$cbs, { pkg_key => $key, cb_key => 'count', cb => \&count }; ############################################################################## # Abort callbacks. sub test_abort { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; $cb->abort(1); } push @$cbs, { pkg_key => $key, cb_key => 'test_abort', cb => \&test_abort }; ############################################################################## # Check the aborted value. sub test_aborted { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; my $val = $cb->value; eval { $cb->abort(1) } if $val; $params->{result} = $cb->aborted($@) ? 'yes' : 'no'; } push @$cbs, { pkg_key => $key, cb_key => 'test_aborted', cb => \&test_aborted }; ############################################################################## # We'll use this callback just to grab the value of the "submit" parameter. sub submit { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; $params->{result} = $params->{submit}; } push @$cbs, { pkg_key => $key, cb_key => 'submit', cb => \&submit }; ############################################################################## # We'll use this callback to throw exceptions. sub exception { my $cb = shift; my $params = $cb->params; if ($cb->value) { # Throw an exception object. HTML::Mason::Exception->throw( error => "He's dead, Jim" ); } else { # Just die. die "He's dead, Jim"; } } push @$cbs, { pkg_key => $key, cb_key => 'exception', cb => \&exception }; ############################################################################## # We'll use these callbacks to test notes(). sub add_note { my $cb = shift; $cb->notes($cb->value, $cb->params->{note}); } sub get_note { my $cb = shift; $cb->params->{result} = $cb->notes($cb->value); } sub list_notes { my $cb = shift; my $params = $cb->params; my $notes = $cb->notes; for my $k (sort keys %$notes) { $params->{result} .= "$k => $notes->{$k}\n"; } } sub clear { my $cb = shift; $cb->cb_request->clear_notes; } push @$cbs, { pkg_key => $key, cb_key => 'add_note', cb => \&add_note }, { pkg_key => $key, cb_key => 'get_note', cb => \&get_note }, { pkg_key => $key, cb_key => 'list_notes', cb => \&list_notes }, { pkg_key => $key, cb_key => 'clear', cb => \&clear }; ############################################################################## # We'll use this callback to change the result to uppercase. sub upper { my $cb = shift; my $params = $cb->params; if ($params->{do_upper}) { isa_ok( $cb, 'Params::Callback'); $params->{result} = uc $params->{result}; } } ############################################################################## # We'll use this callback to flip the characters of the "submit" parameter. # The value of the "submit" parameter won't be "racecar!" sub flip { my $cb = shift; my $params = $cb->params; if ($params->{do_flip}) { isa_ok( $cb, 'Params::Callback'); $params->{submit} = reverse $params->{submit}; } } ############################################################################## # Set up Mason objects. ############################################################################## my $outbuf; ok( my $interp = MasonX::Interp::WithCallbacks->new ( comp_root => catdir($Bin, qw(htdocs)), callbacks => $cbs, post_callbacks => [\&upper], pre_callbacks => [\&flip], out_method => \$outbuf ), "Construct interp object" ); isa_ok($interp, 'MasonX::Interp::WithCallbacks'); isa_ok($interp, 'HTML::Mason::Interp'); isa_ok($interp->cb_request, 'Params::CallbackRequest'); my $comp = '/dhandler'; ############################################################################## # Try a simple callback. $interp->exec($comp, "$key|simple_cb" => 1); is( $outbuf, 'Success', "Check simple result" ); $outbuf = ''; ############################################################################## # Check that prioritized callbacks execute in the proper order. $interp->exec($comp, "$key|priority_cb0" => 0, "$key|priority_cb2" => 2, "$key|priority_cb9" => 9, "$key|priority_cb7" => 7, "$key|priority_cb1" => 1, "$key|priority_cb4" => 4, "$key|priority_cb" => 'def' ); is($outbuf, " 0 1 2 4 5 7 9", "Check priority order" ); $outbuf = ''; ############################################################################## # Emmulate the sumission of an button. $interp->exec($comp, "$key|simple_cb.x" => 18, "$key|simple_cb.y" => 24 ); is( $outbuf, 'Success', "Check simple image result" ); $outbuf = ''; ############################################################################## # Make sure that an image submit doesn't cause the callback to be called # twice. $interp->exec($comp, "$key|count_cb.x" => 18, "$key|count_cb.y" => 24 ); is( $outbuf, '1', "Check image count result" ); $outbuf = ''; ############################################################################## # Just like the above, but make sure that different priorities execute # at different times. $interp->exec($comp, "$key|count_cb1.x" => 18, "$key|count_cb1.y" => 24, "$key|count_cb2.x" => 18, "$key|count_cb2.y" => 24 ); is( $outbuf, '2', "Check second image count result" ); $outbuf = ''; ############################################################################## # Test the abort functionality. The abort callback's higher priority should # cause it to prevent simple from being called. eval { $interp->exec($comp, "$key|simple_cb" => 1, "$key|test_abort_cb0" => 1 ) }; ok( my $err = $@, "Catch exception" ); isa_ok( $err, 'HTML::Mason::Exception::Abort' ); is( $err->aborted_value, 1, "Check aborted value" ); is( $outbuf, '', "Check abort result" ); $outbuf = ''; ############################################################################## # Test the aborted method. $interp->exec($comp, "$key|test_aborted_cb" => 1 ); is( $outbuf, 'yes', "Check aborted result" ); $outbuf = ''; ############################################################################## # Test notes. my $note_key = 'myNote'; my $note = 'Test note'; $interp->exec($comp, "$key|add_note_cb1" => $note_key, # Executes first. note => $note, "$key|get_note_cb" => $note_key); is( $outbuf, $note, "Check note result" ); $outbuf = ''; # Make sure the note isn't available on the next request. $interp->exec($comp, "$key|get_note_cb" => $note_key ); is( $outbuf, '', "Check no note result" ); # Add multiple notes. $interp->exec($comp, "$key|add_note_cb1" => $note_key, # Executes first. "$key|add_note_cb2" => $note_key . 1, # Executes second. note => $note, "$key|list_notes_cb" => 1); is( $outbuf, "$note_key => $note\n${note_key}1 => $note\n", "Check multiple note result" ); $outbuf = ''; # Make sure that notes percolate back to Mason. $interp->exec($comp, "$key|add_note_cb" => $note_key, note => $note, result => sub { shift->notes($note_key) } ); is( $outbuf, $note, "Check mason note result" ); $outbuf = ''; # Make sure that we can still get at the notes via the callback request object # in Mason components. $interp->exec($comp, "$key|add_note_cb" => $note_key, note => $note, result => sub { shift->interp->cb_request->notes($note_key) } ); is( $outbuf, $note, "Check cb_request note result" ); $outbuf = ''; # Finally, make sure that if we clear it in callbacks, that no one gets it. $interp->exec($comp, "$key|add_note_cb1" => $note_key, # Executes first. note => $note, "$key|clear_cb" => 1, result => sub { shift->notes($note_key) } ); is( $outbuf, '', "Check Mason cleared note result" ); $interp->exec($comp, "$key|add_note_cb1" => $note_key, # Executes first. note => $note, "$key|clear_cb" => 1, result => sub { shift->interp->cb_request->notes($note_key) } ); is( $outbuf, '', "Check cb_request cleared note result" ); ############################################################################## # Test the pre-execution callbacks. my $string = 'yowza'; $interp->exec($comp, "$key|submit_cb" => 1, submit => $string, do_flip => 1 ); is( $outbuf, reverse($string), "Check pre result" ); $outbuf = ''; ############################################################################## # Test the post-execution callbacks. $interp->exec($comp, "$key|simple_cb" => 1, do_upper => 1 ); is( $outbuf, 'SUCCESS', "Check post result" ); $outbuf = ''; ok( $interp = MasonX::Interp::WithCallbacks->new ( comp_root => catdir($Bin, qw(htdocs)), callbacks => $cbs, ignore_nulls => 1, out_method => \$outbuf ), "Construct interp object that ignores nulls" ); $interp->exec($comp, "$key|simple_cb" => 1); is( $outbuf, 'Success', "Check simple result" ); $outbuf = ''; # And try it with a null value. $interp->exec($comp, "$key|simple_cb" => ''); is( $outbuf, '', "Check null result" ); $outbuf = ''; # And with undef. $interp->exec($comp, "$key|simple_cb" => undef); is( $outbuf, '', "Check undef result" ); $outbuf = ''; # But 0 should succeed. $interp->exec($comp, "$key|simple_cb" => 0); is( $outbuf, 'Success', "Check 0 result" ); $outbuf = ''; ############################################################################## # Test the exception handler. ok( $interp = MasonX::Interp::WithCallbacks->new ( comp_root => catdir($Bin, qw(htdocs)), callbacks => $cbs, cb_exception_handler => sub { like( $_[0], qr/^He's dead, Jim at/, "Check our die message" ); }, out_method => \$outbuf ), "Construct interp object that handles exceptions" ); $interp->exec($comp, "$key|exception_cb" => 0); $outbuf = ''; __END__ MasonX-Interp-WithCallbacks-1.19/t/02priority.t000444000765000024 604611600017366 21012 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More tests => 28; use HTML::Mason::Interp; BEGIN { use_ok('MasonX::Interp::WithCallbacks') } my $key = 'myCallbackTester'; my $cbs = []; ############################################################################## # Set up callback functions. ############################################################################## # Priority callback. sub priority { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; my $val = $cb->value; $val = $cb->priority if $val eq 'def'; $params->{result} .= " $val"; } sub chk_priority { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $val = $cb->value; is( $cb->priority, $val, "Check priority value '$val'" ); } sub def_priority { my $cb = shift; isa_ok( $cb, 'Params::Callback'); is( $cb->priority, 5, "Check default priority" ); } push @$cbs, { pkg_key => $key, cb_key => 'priority', cb => \&priority, priority => 6 }, { pkg_key => $key, cb_key => 'chk_priority', cb => \&chk_priority, priority => 2 }, { pkg_key => $key, cb_key => 'def_priority', cb => \&def_priority, }; ############################################################################## # Set up the Interp object. ############################################################################## my $outbuf; ok( my $interp = MasonX::Interp::WithCallbacks->new ( comp_root => catdir($Bin, qw(htdocs)), callbacks => $cbs, out_method => \$outbuf ), "Construct interp object" ); isa_ok($interp, 'MasonX::Interp::WithCallbacks'); isa_ok($interp, 'HTML::Mason::Interp'); my $comp = '/dhandler'; ############################################################################## # Test the callbacks themselves. ############################################################################## # Test the priority ordering. $interp->exec($comp, "$key|priority_cb0" => 0, "$key|priority_cb2" => 2, "$key|priority_cb9" => 9, "$key|priority_cb7" => 7, "$key|priority_cb1" => 1, "$key|priority_cb4" => 4, "$key|priority_cb" => 'def' ); is( $outbuf, " 0 1 2 4 6 7 9", "Check simple result" ); $outbuf = ''; ############################################################################## # Test the default priority. $interp->exec($comp, "$key|def_priority_cb" => 1); ############################################################################## # Check various priority values. $interp->exec($comp, "$key|chk_priority_cb0" => 0, "$key|chk_priority_cb2" => 2, "$key|chk_priority_cb9" => 9, "$key|chk_priority_cb7" => 7, "$key|chk_priority_cb1" => 1, "$key|chk_priority_cb4" => 4, "$key|chk_priority_cb" => 2 ); 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/03keys.t000444000765000024 777711600017366 20121 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More tests => 10; use HTML::Mason::Interp; BEGIN { use_ok('MasonX::Interp::WithCallbacks') } my $key = 'myCallbackTester'; my $cbs = []; ############################################################################## # Set up callback functions. ############################################################################## # Callback to test the value of the package key attribute. sub test_pkg_key { my $cb = shift; my $params = $cb->params; $params->{result} .= $cb->pkg_key; } push @$cbs, { pkg_key => $key, cb_key => 'test_pkg_key', cb => \&test_pkg_key }, { pkg_key => $key . '_more', cb_key => 'test_pkg_key', cb => \&test_pkg_key }; ############################################################################## # Callback to test the value returned by the class_key method. sub test_class_key { my $cb = shift; my $params = $cb->params; $params->{result} .= $cb->class_key; } push @$cbs, { pkg_key => $key, cb_key => 'test_class_key', cb => \&test_class_key }, { pkg_key => $key. '_more', cb_key => 'test_class_key', cb => \&test_class_key }; ############################################################################## # Callback to test the value of the trigger key attribute. sub test_trigger_key { my $cb = shift; my $params = $cb->params; $params->{result} .= $cb->trigger_key; } push @$cbs, { pkg_key => $key, cb_key => 'test_trigger_key', cb => \&test_trigger_key }, { pkg_key => $key . '_more', cb_key => 'test_trigger_key', cb => \&test_trigger_key }; ############################################################################## # Set up the Interp object. ############################################################################## my $outbuf; ok( my $interp = MasonX::Interp::WithCallbacks->new ( comp_root => catdir($Bin, qw(htdocs)), callbacks => $cbs, out_method => \$outbuf ), "Construct interp object" ); isa_ok($interp, 'MasonX::Interp::WithCallbacks'); isa_ok($interp, 'HTML::Mason::Interp'); my $comp = '/dhandler'; ############################################################################## # Test the callbacks themselves. ############################################################################## # Test the package key. $interp->exec($comp, "$key|test_pkg_key_cb" => 1 ); is( $outbuf, $key, "Check pkg key" ); $outbuf = ''; # And multiple package keys. $interp->exec($comp, "$key|test_pkg_key_cb1" => 1, "$key\_more|test_pkg_key_cb2" => 1, "$key|test_pkg_key_cb3" => 1, ); is( $outbuf, "$key$key\_more$key", "Check pkg key again" ); $outbuf = ''; ############################################################################## # Test the class key. $interp->exec($comp, "$key|test_class_key_cb" => 1 ); is( $outbuf, $key, "Check class key" ); $outbuf = ''; # And multiple class keys. $interp->exec($comp, "$key|test_class_key_cb1" => 1, "$key\_more|test_class_key_cb2" => 1, "$key|test_class_key_cb3" => 1, ); is( $outbuf, "$key$key\_more$key", "Check class key again" ); $outbuf = ''; ############################################################################## # Test the trigger key. $interp->exec($comp, "$key|test_trigger_key_cb" => 1 ); is( $outbuf, "$key|test_trigger_key_cb", "Check trigger key" ); $outbuf = ''; # And multiple trigger keys. $interp->exec($comp, "$key|test_trigger_key_cb1" => 1, "$key\_more|test_trigger_key_cb2" => 1, "$key|test_trigger_key_cb3" => 1,); is( $outbuf, "$key|test_trigger_key_cb1$key\_more|" . "test_trigger_key_cb2$key|test_trigger_key_cb3", "Check trigger key again" ); $outbuf = ''; 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/04errors.t000444000765000024 1460611600017366 20470 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More tests => 42; use HTML::Mason::Interp; BEGIN { use_ok('MasonX::Interp::WithCallbacks') } my $key = 'myCallbackTester'; sub mydie { die "Ouch!" } sub myfault { die bless {}, 'TestException' } my %cbs = ( pkg_key => $key, cb_key => 'mydie', cb => \&mydie ); my %fault_cb = ( pkg_key => $key, cb_key => 'myfault', cb => \&myfault ); my $outbuf; my %mason_params = (comp_root => catdir($Bin, qw(htdocs)), out_method => \$outbuf); my $comp = 'dhandler'; ############################################################################## # Set up callback functions. ############################################################################## # Check that we get a warning for when there are no callbacks. { local $SIG{__WARN__} = sub { like( $_[0], qr/You didn't specify any callbacks/, "Check warning") }; ok( MasonX::Interp::WithCallbacks->new(%mason_params), "Construct Interp object without CBs" ); } ############################################################################## # Try to construct a CBE object with a bad callback key. my %c = %cbs; $c{cb_key} = ''; eval {MasonX::Interp::WithCallbacks->new(%mason_params, callbacks => [\%c]) }; ok( my $err = $@, "Catch bad cb_key exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::Params' ); like( $err->error, qr/Missing or invalid callback key/, "Check bad cb_key error message" ); ############################################################################## # Try to construct a CBE object with a bad priority. %c = %cbs; $c{priority} = 'foo'; eval {MasonX::Interp::WithCallbacks->new(%mason_params, callbacks => [\%c]) }; ok( $err = $@, "Catch bad priority exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::Params' ); like( $err->error, qr/Not a valid priority: 'foo'/, "Check bad priority error message" ); ############################################################################## # Test a bad code ref. my $msg = "Callback for package key 'myCallbackTester' and callback key " . "'coderef' not a code reference"; %c = %cbs; $c{cb_key} = 'coderef'; $c{cb} = 'bogus'; # Ooops. eval {MasonX::Interp::WithCallbacks->new(%mason_params, callbacks => [\%c]) }; ok( $err = $@, "Catch bad code ref exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::Params' ); like( $err->error, qr/$msg/, "Check bad code ref error message" ); ############################################################################## # Test for a used key. %c = my %b = %cbs; $c{cb_key} = $b{cb_key} = 'bar'; # Ooops. eval {MasonX::Interp::WithCallbacks->new(%mason_params, callbacks => [\%c, \%b]) }; ok( $err = $@, "Catch used key exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::Params' ); like( $err->error, qr/Callback key 'bar' already used by package key '$key'/, "Check used key error message" ); ############################################################################## # Test a bad request code ref. eval {MasonX::Interp::WithCallbacks->new(%mason_params, pre_callbacks => ['foo']) }; ok( $err = $@, "Catch bad request code ref exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::Params' ); like( $err->error, qr/Request pre callback not a code reference/, 'Check bad request code ref exception' ); ############################################################################## # Construct one to be used for exceptions during the execution of callbacks. ############################################################################## ok( my $interp = MasonX::Interp::WithCallbacks->new(%mason_params, callbacks => [\%cbs, \%fault_cb]), "Construct Interp object" ); isa_ok($interp, 'MasonX::Interp::WithCallbacks' ); ############################################################################## # Test the callbacks themselves. ############################################################################## # Make sure an exception get thrown for a non-existant package. eval { $interp->exec($comp, 'NoSuchLuck|foo_cb' => 1) }; ok( $err = $@, "Catch bad package exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::InvalidKey' ); like( $err->error, qr/No such callback package 'NoSuchLuck'/, "Check bad package message" ); $outbuf = ''; ############################################################################## # Make sure an exception get thrown for a non-existant callback. eval { $interp->exec($comp, "$key|foo_cb" => 1) }; ok( $err = $@, "Catch missing callback exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::InvalidKey' ); like( $err->error, qr/No callback found for callback key '$key|foo_cb'/, "Check missing callback message" ); $outbuf = ''; ############################################################################## # Now die from within our callback function. eval { $interp->exec($comp, "$key|mydie_cb" => 1) }; ok( $err = $@, "Catch our exception" ); isa_ok($err, 'Params::Callback::Exception' ); isa_ok($err, 'Params::Callback::Exception::Execution' ); like( $err->error, qr/^Error thrown by callback: Ouch! at/, "Check our mydie message" ); like( $err->callback_error, qr/^Ouch! at/, "Check our die message" ); $outbuf = ''; ############################################################################## # Now throw our own exception. eval { $interp->exec($comp, "$key|myfault_cb" => 1) }; ok( $err = $@, "Catch our exception" ); isa_ok($err, 'TestException' ); $outbuf = ''; ############################################################################## # Now test exception_handler. ok( $interp = MasonX::Interp::WithCallbacks->new ( %mason_params, callbacks => [\%cbs], cb_exception_handler => sub { like( $_[0], qr/^Ouch! at/, "Custom check our die message" ); }), "Construct Interp object with custom exception handler" ); eval { $interp->exec($comp, "$key|mydie_cb" => 1) }; $outbuf = ''; 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/05object.t000444000765000024 3073611600017366 20425 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More; use HTML::Mason::Interp; my $base_key = 'OOTester'; my $err_msg = "He's dead, Jim"; my $comp = '/dhandler'; ############################################################################## # Figure out if the current configuration can handle OO callbacks. BEGIN { plan skip_all => 'Object-oriented callbacks require Perl 5.6.0 or later' if $] < 5.006; plan skip_all => 'Attribute::Handlers and Class::ISA required for' . ' object-oriented callbacks' unless eval { require Attribute::Handlers } and eval { require Class::ISA }; plan tests => 136; } ############################################################################## # Set up the callback class. ############################################################################## package Params::Callback::TestObjects; use strict; use base 'Params::Callback'; __PACKAGE__->register_subclass( class_key => $base_key); use Params::CallbackRequest::Exceptions abbr => [qw(throw_cb_exec)]; sub simple : Callback { my $self = shift; main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); my $params = $self->params; $params->{result} = 'Simple Success'; } sub complete : Callback(priority => 3) { my $self = shift; main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); main::is($self->priority, 3, "Check priority is '3'" ); my $params = $self->params; $params->{result} = 'Complete Success'; } sub inherit : Callback { my $self = shift; my $params = $self->params; $params->{result} = UNIVERSAL::isa($self, 'Params::Callback') ? 'Yes' : 'No'; } sub highest : Callback(priority => 0) { my $self = shift; main::is( $self->priority, 0, "Check priority is '0'" ); } sub upperit : PreCallback { my $self = shift; my $params = $self->params; $params->{result} = uc $params->{result} if $params->{do_upper}; } sub pre_post : Callback { my $self = shift; my $params = $self->params; $params->{chk_post} = 1; } sub lowerit : PostCallback { my $self = shift; my $params = $self->params; $params->{result} = lc $params->{result} if $params->{do_lower}; } sub class : Callback { my $self = shift; main::isa_ok( $self, __PACKAGE__); main::isa_ok( $self, $self->value); } sub chk_priority : Callback { my $self = shift; my $priority = $self->priority; my $val = $self->value; $val = 5 if $val eq 'def'; main::is($priority, $val, "Check for priority '$val'" ); my $params = $self->params; $params->{result} .= " " . $priority; } sub test_abort : Callback { my $self = shift; $self->abort(1); } sub test_aborted : Callback { my $self = shift; my $params = $self->params; my $val = $self->value; eval { $self->abort(1) } if $val; $params->{result} = $self->aborted($@) ? 'yes' : 'no'; } sub exception : Callback { my $self = shift; if ($self->value) { # Throw an exception object. throw_cb_exec $err_msg; } else { # Just die. die $err_msg; } } sub same_object : Callback { my $self = shift; my $params = $self->params; if ($self->value) { main::is($self, $params->{obj}, "Check for same object" ); } else { $params->{obj} = $self; } } sub isa_interp : Callback { my $self = shift; main::isa_ok $self->requester, 'MasonX::Interp::WithCallbacks', 'the requester object'; } sub change_comp : Callback { my $self = shift; $self->requester->comp_path($self->value); } 1; ############################################################################## # Now set up an emtpy callback subclass. ############################################################################## package Params::Callback::TestObjects::Empty; use strict; use base 'Params::Callback::TestObjects'; __PACKAGE__->register_subclass( class_key => $base_key . 'Empty'); 1; ############################################################################## # Now set up an a subclass that overrides a parent method. ############################################################################## package Params::Callback::TestObjects::Sub; use strict; use base 'Params::Callback::TestObjects'; __PACKAGE__->register_subclass( class_key => $base_key . 'Sub'); # Try a method with the same name as one in the parent, and which # calls the super method. sub inherit : Callback { my $self = shift; $self->SUPER::inherit; my $params = $self->params; $params->{result} .= ' and '; $params->{result} .= UNIVERSAL::isa($self, 'Params::Callback::TestObjects') ? 'Yes' : 'No'; } # Try a totally new method. sub subsimple : Callback { my $self = shift; my $params = $self->params; $params->{result} = 'Subsimple Success'; } # Try a totally new method. sub simple : Callback { my $self = shift; my $params = $self->params; $params->{result} = 'Oversimple Success'; } 1; ############################################################################## # Meanwhile, back at the ranch... ############################################################################## package main; # Keep track of who's who. my %classes = ( $base_key => 'Params::Callback::TestObjects', $base_key . 'Sub' => 'Params::Callback::TestObjects::Sub', $base_key . 'Empty' => 'Params::Callback::TestObjects::Empty'); my $outbuf; my %mason_params = (comp_root => catdir($Bin, qw(htdocs)), out_method => \$outbuf); use_ok('MasonX::Interp::WithCallbacks'); my $all = 'ALL'; for my $key ($base_key, $base_key . "Empty", $all) { # Create the Interp object. my $interp; if ($key eq 'ALL') { # Load all of the callback classes. ok( $interp = MasonX::Interp::WithCallbacks->new( %mason_params, cb_classes => $key ), "Construct $key Interp object" ); $key = $base_key; } else { # Load the base class and the subclass. ok( $interp = MasonX::Interp::WithCallbacks->new ( %mason_params, cb_classes => [$key, $base_key . 'Sub']), "Construct $key Interp object" ); } ########################################################################## # Now make sure that the simple callback executes. $interp->exec($comp, "$key|simple_cb" => 1); is( $outbuf, 'Simple Success', "Check simple result" ); $outbuf = ''; ########################################################################## # And the "complete" callback. $interp->exec($comp, "$key|complete_cb" => 1); is( $outbuf, 'Complete Success', "Check complete result" ); $outbuf = ''; ########################################################################## # Check the class name. $interp->exec($comp, "$key|inherit_cb" => 1); is( $outbuf, 'Yes', "Check inherit result" ); $outbuf = ''; ########################################################################## # Check class inheritance and SUPER method calls. $interp->exec($comp, $base_key . "Sub|inherit_cb" => 1); is( $outbuf, 'Yes and Yes', "Check SUPER inherit result" ); $outbuf = ''; ########################################################################## # Try pre-execution callbacks. $interp->exec($comp, do_upper => 1, result => 'upPer_mE'); is( $outbuf, 'UPPER_ME', "Check pre result" ); $outbuf = ''; ########################################################################## # Try post-execution callbacks. $interp->exec($comp, "$key|simple_cb" => 1, do_lower => 1); is( $outbuf, 'simple success', "Check post result" ); $outbuf = ''; ########################################################################## # Try a method defined only in a subclass. $interp->exec($comp, $base_key . "Sub|subsimple_cb" => 1); is( $outbuf, 'Subsimple Success', "Check subsimple result" ); $outbuf = ''; ########################################################################## # Try a method that overrides its parent but doesn't call its parent. $interp->exec($comp, $base_key . "Sub|simple_cb" => 1); is( $outbuf, 'Oversimple Success', "Check oversimple result" ); $outbuf = ''; ########################################################################## # Try a method that overrides its parent but doesn't call its parent. $interp->exec($comp, $base_key . "Sub|simple_cb" => 1); is( $outbuf, 'Oversimple Success', "Check oversimple result" ); $outbuf = ''; ########################################################################## # Check that the proper class ojbect is constructed. $interp->exec($comp, "$key|class_cb" => $classes{$key}); $outbuf = ''; ########################################################################## # Check priority execution order for multiple callbacks. $interp->exec($comp, "$key|chk_priority_cb0" => 0, "$key|chk_priority_cb2" => 2, "$key|chk_priority_cb9" => 9, "$key|chk_priority_cb7" => 7, "$key|chk_priority_cb1" => 1, "$key|chk_priority_cb4" => 4, "$key|chk_priority_cb" => 'def'); is($outbuf, " 0 1 2 4 5 7 9", "Check priority order result" ); $outbuf = ''; ########################################################################## # Emulate the sumission of an button. $interp->exec($comp, "$key|simple_cb.x" => 18, "$key|simple_cb.y" => 22 ); is( $outbuf, 'Simple Success', "Check single simple result" ); $outbuf = ''; ########################################################################## # Make sure that if we abort, no more callbacks execute. eval { $interp->exec($comp, "$key|test_abort_cb0" => 1, "$key|simple_cb" => 1, result => 'still here') }; is( $outbuf, '', "Check abort result" ); $outbuf = ''; ########################################################################## # Test aborted for a false value. $interp->exec($comp, "$key|test_aborted_cb" => 0); is( $outbuf, 'no', "Check false aborted result" ); $outbuf = ''; ########################################################################## # Test aborted for a true value. $interp->exec($comp, "$key|test_aborted_cb" => 1); is( $outbuf, 'yes', "Check true aborted result" ); $outbuf = ''; ########################################################################## # Try throwing an execption. eval { $interp->exec($comp, "$key|exception_cb" => 1) }; ok( my $err = $@, "Catch $key exception" ); isa_ok($err, 'Params::Callback::Exception'); isa_ok($err, 'Params::Callback::Exception::Execution'); is( $err->error, $err_msg, "Check error message" ); $outbuf = ''; ########################################################################## # Try die'ing. eval { $interp->exec($comp, "$key|exception_cb" => 0) }; ok( $err = $@, "Catch $key die" ); isa_ok($err, 'Params::Callback::Exception'); isa_ok($err, 'Params::Callback::Exception::Execution'); like( $err->error, qr/^Error thrown by callback: $err_msg/, "Check die error message" ); $outbuf = ''; ########################################################################## # Make sure that the same object is called for multiple callbacks in the # same class. $interp->exec($comp, "$key|same_object_cb1" => 0, "$key|same_object_cb" => 1); $outbuf = ''; ########################################################################## # Check priority 0 sticks. $interp->exec($comp, "$key|highest_cb" => undef); $outbuf = ''; ########################################################################## # Requester should be WithCallbacks object. $interp->exec($comp, "$key|isa_interp_cb" => 1); $outbuf = ''; ########################################################################## # Changing the comp path should change the executed component. $interp->exec($comp, "$key|change_comp_cb" => '/alt.mc'); is $outbuf, 'This is the alt component.', 'The alt component should have executed'; $outbuf = ''; } __END__ MasonX-Interp-WithCallbacks-1.19/t/06object_request.t000444000765000024 1527711600017366 22201 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More; use HTML::Mason::Interp; my $base_key; my $err_msg = "He's dead, Jim"; my $comp = '/dhandler'; ############################################################################## # Figure out if the current configuration can handle OO callbacks. BEGIN { plan skip_all => 'Object-oriented callbacks require Perl 5.6.0 or later' if $] < 5.006; plan skip_all => 'Attribute::Handlers and Class::ISA required for' . ' object-oriented callbacks' unless eval { require Attribute::Handlers } and eval { require Class::ISA }; plan tests => 39; $base_key = 'OOTester'; } ############################################################################## # Set up the base callback class. ############################################################################## package Params::Callback::TestObjects; use strict; use base 'Params::Callback'; use constant CLASS_KEY => $base_key; use constant DEFAULT_PRIORITY => 3; __PACKAGE__->register_subclass; sub upperit : PreCallback { my $self = shift; my $params = $self->params; if ($params->{do_upper}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); $params->{result} = uc $params->{result}; } } sub lowerit : PostCallback { my $self = shift; my $params = $self->params; if ($params->{do_lower}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); $params->{result} = lc $params->{result}; } } sub pre_post : Callback { my $self = shift; main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); main::is($self->priority, 3, "Check default priority constant" ); my $params = $self->params; $params->{chk_post} = 1; } sub chk_post : PostCallback { my $self = shift; my $params = $self->params; if ($params->{chk_post}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); # Most of the methods should return undefined values. my @res; foreach my $meth (qw(value pkg_key cb_key priority trigger_key)) { push @res, "$meth => '", $self->$meth, "'\n" if $self->$meth; } if (@res) { $params->{result} = "Oops, some of the accessors have values: @res"; } else { $params->{result} = 'Attributes okay'; } } } ############################################################################## # Now set up an a subclass that overrides pre and post execution callbacks, # and provides a couple of new ones, too. ############################################################################## package Params::Callback::TestObjects::Sub; use strict; use base 'Params::Callback::TestObjects'; use constant CLASS_KEY => $base_key . 'Sub'; __PACKAGE__->register_subclass; sub upperit : PreCallback { my $self = shift; $self->SUPER::upperit; my $params = $self->params; if ($params->{do_upper}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, 'Params::Callback::TestObjects'); main::isa_ok($self, __PACKAGE__); $params->{result} .= ' Overridden'; } } sub lowerit : PostCallback { my $self = shift; $self->SUPER::lowerit; my $params = $self->params; if ($params->{do_lower}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, 'Params::Callback::TestObjects'); main::isa_ok($self, __PACKAGE__); $params->{result} .= ' Overridden'; } } # Try totally new methods. sub sub_pre : PreCallback { my $self = shift; my $params = $self->params; if ($params->{do_lower} or $params->{do_upper}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, 'Params::Callback::TestObjects'); main::isa_ok($self, __PACKAGE__); $params->{result} .= ' PreCallback'; } } sub sub_post : PostCallback { my $self = shift; my $params = $self->params; if ($params->{do_lower} or $params->{do_upper}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, 'Params::Callback::TestObjects'); main::isa_ok($self, __PACKAGE__); $params->{result} .= ' PostCallback'; } } 1; ############################################################################## # Move along, little doggies! ############################################################################## package main; use strict; use_ok( 'MasonX::Interp::WithCallbacks' ); my $outbuf; my %mason_params = (comp_root => catdir($Bin, qw(htdocs)), out_method => \$outbuf); ############################################################################## # Make sure that the base pre and post callbacks work properly. Start with # post. ok( my $interp = MasonX::Interp::WithCallbacks->new ( %mason_params, cb_classes => [$base_key]), "Construct base callback Interp" ); ############################################################################## # Start with post. $interp->exec($comp, do_lower => 1, result => 'LOWER ME, BABY!'); is( $outbuf, 'lower me, baby!', "Check post callback result" ); $outbuf = ''; ############################################################################## # Now check pre. $interp->exec($comp, do_upper => 1, result => 'taKe mE uP!'); is( $outbuf, 'TAKE ME UP!', "Check pre callback result" ); $outbuf = ''; ############################################################################## # Make sure that pre and post execution callback inheritance works properly. ok( $interp = MasonX::Interp::WithCallbacks->new (%mason_params, cb_classes => [$base_key . 'Sub']), "Construct subclasseed callback Interp" ); ############################################################################## # Post first. $interp->exec($comp, do_lower => 1, result => 'LOWER ME'); is( $outbuf, 'lower me precallback Overridden PostCallback', "Check subclassed post callback result" ); $outbuf = ''; ############################################################################## # Now check pre. $interp->exec($comp, do_upper => 1, result => 'taKe mE uP aGain!'); is( $outbuf, 'TAKE ME UP AGAIN! Overridden PreCallback PostCallback', "Check subclassed pre callback result" ); $outbuf = ''; ############################################################################## # Check that no of the unneeded attributes are populated during request # callbacks. $interp->exec($comp, "$base_key|pre_post_cb" => 1); is( $outbuf, 'Attributes okay', "Check attribute check result" ); $outbuf = ''; 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/07combined.t000444000765000024 1023611600017366 20732 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir catfile); use Test::More; use HTML::Mason::Interp; my $base_key = 'OOTester'; my $comp = '/dhandler'; ############################################################################## # Figure out if the current configuration can handle OO callbacks. BEGIN { plan skip_all => 'Object-oriented callbacks require Perl 5.6.0 or later' if $] < 5.006; plan skip_all => 'Attribute::Handlers and Class::ISA required for' . ' object-oriented callbacks' unless eval { require Attribute::Handlers } and eval { require Class::ISA }; plan tests => 16; } ############################################################################## # Set up the callback class. ############################################################################## package Params::Callback::TestObjects; use strict; use base 'Params::Callback'; __PACKAGE__->register_subclass( class_key => $base_key); use Params::CallbackRequest::Exceptions abbr => [qw(throw_cb_exec)]; sub simple : Callback { my $self = shift; main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); my $params = $self->params; $params->{result} = 'Simple Success'; } sub lowerit : PostCallback { my $self = shift; my $params = $self->params; if ($params->{do_lower}) { main::isa_ok($self, 'Params::Callback'); main::isa_ok($self, __PACKAGE__); $params->{result} = lc $params->{result}; } } 1; ############################################################################## # Set up another callback class to test the default class key. package Params::Callback::TestKey; use strict; use base 'Params::Callback'; __PACKAGE__->register_subclass; sub my_key : Callback { my $self = shift; main::is($self->pkg_key, __PACKAGE__, "Check package key" ); main::is($self->class_key, __PACKAGE__, "Check class key" ); } ############################################################################## # Back in the real world... ############################################################################## package main; use strict; use_ok('MasonX::Interp::WithCallbacks'); ############################################################################## # Set up a functional callback we can use. sub another { my $cb = shift; main::isa_ok($cb, 'Params::Callback'); my $params = $cb->params; $params->{result} = 'Another Success'; } ############################################################################## # And a functional request callback. sub presto { my $cb = shift; main::isa_ok($cb, 'Params::Callback'); my $params = $cb->params; $params->{result} = 'PRESTO' if $params->{do_presto}; } ############################################################################## # Construct the combined callback exec object. my $outbuf; ok( my $interp = MasonX::Interp::WithCallbacks->new (comp_root => catdir($Bin, qw(htdocs)), out_method => \$outbuf, callbacks => [{ pkg_key => 'foo', cb_key => 'another', cb => \&another}], cb_classes => 'ALL', pre_callbacks => [\&presto] ), "Construct combined CBExec object" ); ############################################################################## # Make sure the functional callback works. $interp->exec($comp, 'foo|another_cb' => 1); is( $outbuf, 'Another Success', "Check functional result" ); $outbuf = ''; ############################################################################## # Make sure OO callback works. $interp->exec($comp, "$base_key|simple_cb" => 1); is( $outbuf, 'Simple Success', "Check OO result" ); $outbuf = ''; ############################################################################## # Make sure that functional and OO request callbacks execute, too. $interp->exec($comp, do_lower => 1, do_presto => 1); is( $outbuf, 'presto', "Check request result" ); $outbuf = ''; ############################################################################## # Make sure that the default class key is the class name. $interp->exec($comp, "Params::Callback::TestKey|my_key_cb" => 1); $outbuf = ''; 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/08apache.t000444000765000024 2613711600017366 20403 0ustar00davidstaff000000000000#!perl -w use strict; use Test::More; BEGIN { plan skip_all => 'Testing of apache_req requires Apache::Test 1.04' unless eval { require Apache::Test }; plan skip_all => 'Test of apache_req requires mod_perl' unless Apache::Test::have_module('mod_perl.c'); require Apache::TestRequest; Apache::TestRequest->import(qw(GET POST)); plan tests => 178; } Apache::TestRequest::user_agent( reset => 1, requests_redirectable => 0, ); my $key = 'myCallbackTester'; my @keys = ( myCallbackTester => '/test', OOCBTester => '/oop', OOCBTester => '/ooconf', ); ############################################################################## # Just make sure it works. while (my $key = shift @keys) { my $uri = shift @keys; ok( my $res = GET("$uri?$key|simple_cb=1"), "Get response" ); is( $res->code, 200, "Check simple response code" ); is( $res->content, "Success", 'Check simple content' ); # Make sure that POST works. ok( $res = POST($uri, ["$key|simple_cb" => 1]), "Get POST response" ); is( $res->code, 200, "Check simple POST response code" ); is( $res->content, "Success", 'Check simple POST content' ); # Check that multiple callbacks execute in priority order. ok( $res = POST($uri, [ "$key|priority_cb0" => 0, "$key|priority_cb2" => 2, "$key|priority_cb9" => 9, "$key|priority_cb7" => 7, "$key|priority_cb1" => 1, "$key|priority_cb4" => 4, "$key|priority_cb" => 'def' ] ), "Get execution order response" ); is( $res->code, 200, "Check execution order response code" ); is( $res->content, " 0 1 2 4 5 7 9", 'Check execution order content' ); # Execute the one callback with an array of values ok( $res = POST($uri, [ "$key|multi_cb" => 1, "$key|multi_cb" => 1, "$key|multi_cb" => 1, "$key|multi_cb" => 1, "$key|multi_cb" => 1 ] ), "Get array response" ); is( $res->code, 200, "Check array response code" ); is( $res->content, 5, 'Check array content' ); # Emmulate the sumission of an button. ok( $res = POST($uri, [ "$key|simple_cb.x" => 18, "$key|simple_cb.y" => 24 ] ), "Get image button response" ); is( $res->code, 200, "Check image button code" ); is( $res->content, "Success", 'Check image button content' ); # Make sure that an image submit doesn't cause the callback to be called # twice. ok( $res = POST($uri, [ "$key|count_cb.x" => 18, "$key|count_cb.y" => 24 ] ), "Get image button count response" ); is( $res->code, 200, "Check image button count response code" ); is( $res->content, 1, 'Check image button count content' ); # Try the pre request callback. ok( $res = POST($uri, [ do_upper => 1, result => 'yowza!' ]), "Get pre request callback response" ); is( $res->code, 200, "Check pre request callback code" ); is( $res->content, "YOWZA!", 'Check pre request callback content' ); # Try the post request callback. ok( $res = POST($uri, [ "$key|simple_cb" => 1, do_lower => 1 ]), "Get post request callback response" ); is( $res->code, 200, "Check post request callback code" ); is( $res->content, "success", 'Check post request callback content' ); ########################################################################## # Fun with notes. my $note_key = 'myNote'; my $note = 'Test note'; ok( $res = POST($uri, [ "$key|add_note_cb1" => $note_key, # Executes first. note => $note, "$key|get_note_cb" => $note_key]), "Get note response" ); is( $res->code, 200, "Check note callback code" ); is( $res->content, $note, "Check note callback result" ); # Make sure the note isn't available on the next request. ok( $res = POST($uri, ["$key|get_note_cb" => $note_key]), "Get no note response" ); is( $res->code, 200, "Check no note code" ); is( $res->content, '', "Check no note result" ); # Add multiple notes. ok( $res = POST($uri, [ "$key|add_note_cb1" => $note_key, # Executes first. "$key|add_note_cb2" => $note_key . 1, # Executes second. note => $note, "$key|list_notes_cb" => 1 ]), "Get multiple note response" ); is( $res->code, 200, "Check no note code" ); is( $res->content, "$note_key => $note\n${note_key}1 => $note\n", "Check multiple note result" ); # Make sure that notes percolate back to Mason. ok( $res = POST($uri, [ "$key|add_note_cb1" => $note_key, note => $note, "$key|mason_note_cb" => $note_key, ]), "Get mason note response" ); is( $res->code, 200, "Check mason note code" ); is( $res->content, $note, "Check mason note result" ); # Make sure that we can still get at the notes via the callback request # object in Mason components. ok( $res = POST($uri, [ "$key|add_note_cb" => $note_key, note => $note, "$key|cbr_note_cb" => $note_key, ]), "Get cb_request note respone" ); is( $res->code, 200, "Check cb_request note code" ); is( $res->content, $note, "Check cb_request note result" ); # Finally, make sure that if we clear it in callbacks, that no one gets it. ok( $res = POST($uri, [ "$key|add_note_cb1" => $note_key, # Executes first. note => $note, "$key|clear_cb" => 1, "$key|mason_note_cb" => 1, ]), "Get Mason cleared note response" ); is( $res->code, 200, "Check Mason cleared note code" ); is( $res->content, '', "Check Mason cleared note result" ); ok( $res = POST($uri, [ "$key|add_note_cb1" => $note_key, # Executes first. note => $note, "$key|clear_cb" => 1, "$key|cbr_note_cb" => $note_key, ]), "Get cb_request cleared note response" ); is( $res->code, 200, "Check cb_request cleared note code" ); is( $res->content, '', "Check cb_request cleared note result" ); } ############################################################################## # Make sure an exception get thrown for a non-existant package. ok( my $res = POST("/test?myNoSuchLuck|foo_cb=1"), "Get non-existent callback response" ); is( $res->code, 500, "Check non-existent callback response code" ); # Make sure that redirects work. SKIP: { skip "Redirect tests rrequire Apache::Test 1.04 or newer", 8 unless Apache::Test->VERSION >= 1.04; ok( $res = POST("/test", ["$key|redir_cb" => 0, "$key|add_header_cb9" => 1, "header" => 'Age', "value" => 42 ] ), "Get redirect response" ); is( $res->code, 302, "Check redirect response code" ); is( $res->header('Location'), 'http://example.com/', "Check redirect location" ); is( $res->header('Age'), undef, "Check redirect Age header" ); # Make sure that redirect without abort works. ok( $res = POST("/test", ["$key|redir_cb0" => 1, "$key|add_header_cb9" => 1, "header" => 'Age', "value" => 42 ] ), "Get redirect without abort response" ); is( $res->code, 302, "Check redirect without abort response code" ); is( $res->header('Location'), 'http://example.com/', "Check redirect without abort location" ); is( $res->header('Age'), 42, "Check redirect without abort Age header" ); } # Make sure that abort 200 works. ok( $res = POST("/test", [ "$key|test_abort_cb" => 200 ]), "Get abort 200 request" ); is( $res->code, 200, "Check abort 200 response code" ); # Now try to die in the callback. ok( $res = POST("/test", [ "$key|exception_cb" => 0 ]), "Get die response" ); is( $res->code, 500, "Check die response code" ); # Now try to throw and handle an exception in the callback. ok( $res = POST("/exception_handler", [ "$key|exception_cb" => 1 ]), "Get exception handler response" ); is( $res->code, 200, "Check exception handler response code" ); # Now make sure that a callback with a value executes. ok( $res = POST("/no_null", [ "$key|simple_cb" => 1 ]), "Get simle no null response" ); is( $res->code, 200, "Check simple no null code" ); is( $res->content, "Success", 'Check simple no null content' ); # Now make sure that a callback with a null string does not execute. ok( $res = POST("/no_null", [ "$key|simple_cb" => '' ]), "Get simle no null response" ); is( $res->code, 200, "Check simple null code" ); is( $res->content, "", 'Check simple null content' ); # Test MasonCallbacks + MasonDefaultPkgKey. ok( $res = POST("/conf", ["CBFoo|pkg_key_cb" => 1]), "Get MasonDefaultPkgKey response" ); is( $res->code, 200, "Check MasonDefaultPkgKey code" ); is( $res->content, 'CBFoo', 'Check MasonDefaultPkgKey content' ); # Test MasonCallbacks + MasonDefaultPriority. ok( $res = POST("/conf", ["CBFoo|priority_cb" => 1]), "Get MasonDefaultPriority response" ); is( $res->code, 200, "Check MasonDefaultPriority code" ); is( $res->content, 3, 'Check MasonDefaultPriority content' ); # Test MasonPreCallbacks. ok( $res = POST("/conf", [result => 'yes', do_upper => 1]), "Get MasonPreCallbacks response" ); is( $res->code, 200, "Check MasonPreCallbacks code" ); is( $res->content, 'YES', 'Check MasonPreCallbacks content' ); # Test MasonCbExceptionHandler. ok( $res = POST("/conf", ["CBFoo|die_cb" => 1]), "Get MasonCbExceptionHandler response" ); is( $res->code, 200, "Check MasonCbExceptionHandler code" ); is( $res->content, '', 'Check MasonCbExceptionHandler content' ); # Test MasonPostCallbacks. ok( $res = POST("/conf", [result => 'YES', do_lower => 1]), "Get MasonPostCallbacks response" ); is( $res->code, 200, "Check MasonPostCallbacks code" ); is( $res->content, 'yes', 'Check MasonPostCallbacks content' ); # Test MasonIgnoreNulls. ok( $res = POST("/nulls_conf", ["CBFoo|exec_cb" => 1]), "Get MasonIgnoreNulls response" ); is( $res->code, 200, "Check MasonIgnoreNulls code" ); is( $res->content, 'executed', 'Check MasonIgnoreNulls content' ); # Test MasonIgnoreNulls. ok( $res = POST("/nulls_conf", ["CBFoo|exec_cb" => '']), "Get MasonIgnoreNulls null response" ); is( $res->code, 200, "Check MasonIgnoreNulls null code" ); is( $res->content, '', 'Check MasonIgnoreNulls null content' ); 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/09cgi.t000444000765000024 1254411600017366 17722 0ustar00davidstaff000000000000#!perl -w use strict; use FindBin qw($Bin); use File::Spec::Functions qw(catdir); use Test::More tests => 27; use CGI qw(-no_debug); use HTML::Mason::CGIHandler; BEGIN { unshift @INC, catdir $Bin, 'lib'; } use TieOut; BEGIN { use_ok('MasonX::Interp::WithCallbacks') } my $key = 'myCallbackTester'; my $cbs = []; $ENV{PATH_INFO} = '/dhandler'; $ENV{REQUEST_METHOD} = 'GET'; ############################################################################## # This will tie off STDOUT so that it doesn't print do the terminal during # tests. my $stdout = tie *STDOUT, 'TieOut' or die "Cannot tie STDOUT: $!\n"; my $outbuf; ############################################################################## # We'll use this subroutine to clear out various buffers between each test. sub clear_bufs { $outbuf = ''; CGI::initialize_globals(); $stdout->read; } ############################################################################## # Set up callback functions. ############################################################################## # Simple callback. sub simple { my $cb = shift; isa_ok( $cb, 'Params::Callback' ); isa_ok( $cb->cb_request, 'Params::CallbackRequest' ); my $params = $cb->params; $params->{result} = 'Success'; } push @$cbs, { pkg_key => $key, cb_key => 'simple', cb => \&simple }; ############################################################################## # Abort callbacks. sub test_abort { my $cb = shift; isa_ok( $cb, 'Params::Callback'); $cb->abort($cb->value); } push @$cbs, { pkg_key => $key, cb_key => 'test_abort', cb => \&test_abort }; ############################################################################## # Check the aborted value. sub test_aborted { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $params = $cb->params; my $val = $cb->value; eval { $cb->abort(1) } if $val; $params->{result} = $cb->aborted($@) ? 'yes' : 'no'; } push @$cbs, { pkg_key => $key, cb_key => 'test_aborted', cb => \&test_aborted }; ############################################################################## # Set up a redirection callback. my $url = 'http://example.com/'; sub redir { my $cb = shift; my $wait = $cb->value; $cb->redirect($url, $wait); } push @$cbs, { pkg_key => $key, cb_key => 'redir', cb => \&redir }; ############################################################################## # Set up a callback to add a header that we can then check for. sub add_header { my $cb = shift; isa_ok( $cb, 'Params::Callback'); my $r = $cb->apache_req; $r->header_out(Age => 42); } push @$cbs, { pkg_key => $key, cb_key => 'add_header', cb => \&add_header }; ############################################################################## # Set up Mason objects. ############################################################################## ok( my $cgih = HTML::Mason::CGIHandler->new ( comp_root => catdir($Bin, qw(htdocs)), callbacks => $cbs, interp_class => 'MasonX::Interp::WithCallbacks', out_method => \$outbuf ), "Construct CGIHandler object" ); isa_ok($cgih, 'HTML::Mason::CGIHandler'); (ok my $interp = $cgih->interp, "Get Interp object" ); isa_ok($interp, 'MasonX::Interp::WithCallbacks'); isa_ok($interp, 'HTML::Mason::Interp'); ############################################################################## # Try a simple callback. $ENV{QUERY_STRING} = "$key|simple_cb=1"; ok( $cgih->handle_request, "Handle simple callback request" ); is( $outbuf, 'Success', "Check simple result" ); clear_bufs; ############################################################################## # Make sure that abort works properly. For Mason 1.22 and earlier, we need # to catch the exception ourselves. $ENV{QUERY_STRING} = "$key|simple_cb=1" . "&$key|test_abort_cb0=500"; ok( $cgih->handle_request, "Handle abort callback request" ); is( $outbuf, '', "Check abort result" ); clear_bufs; ############################################################################## # Test the aborted method. $ENV{QUERY_STRING} = "$key|test_aborted_cb=1"; ok( $cgih->handle_request, "Handle aborted callback request" ); is( $outbuf, 'yes', "Check aborted result" ); clear_bufs; ############################################################################## # Test redirect. $ENV{QUERY_STRING} = "$key|redir_cb=0"; ok( $cgih->handle_request, "Handle redirection request" ); is( $outbuf, '', "Check redirection result" ); ok( my $out = $stdout->read, "Get contents of STDOUT" ); like( $out, qr/^Status: 302 (?:Moved|Found)/m, "Check Status header" ); like( $out, qr/^(?:Moved\s+)?Location: $url/mi, "Check Location header" ); clear_bufs; ############################################################################## # Make sure that redirect without abort works. $ENV{QUERY_STRING} = "$key|redir_cb0=1" . "&$key|add_header_cb9=1"; ok( $cgih->handle_request, "Handle redirect w/o abort" ); ok( my $res = $stdout->read, "Get response headers" ); like( $res, qr/^Status: 302 (?:Moved|Found)/m, "Check for Status header" ); like( $res, qr/^(?:Moved\s+)?Location: $url/mi, "Check for Location header" ); like( $res, qr/^Age: 42/mi, "Check for age header" ); clear_bufs; __END__ MasonX-Interp-WithCallbacks-1.19/t/10pod.t000444000765000024 26311600017366 17665 0ustar00davidstaff000000000000#!perl -w use strict; use Test::More; eval "use Test::Pod 1.20"; plan skip_all => "Test::Pod 1.20 required for testing POD" if $@; all_pod_files_ok(all_pod_files('bin', 'lib')); MasonX-Interp-WithCallbacks-1.19/t/conf000755000765000024 011600017366 17364 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/t/conf/extra.conf.in000444000765000024 344211600017366 22123 0ustar00davidstaff000000000000 PerlSetVar MasonCompRoot @DocumentRoot@ use File::Spec::Functions qw(catdir); use lib catdir '@ServerRoot@', 'lib'; use lib catdir '@ServerRoot@', '..', 'blib'; use lib catdir '@ServerRoot@', '..', 'lib'; PerlModule TestCallbacks SetHandler perl-script PerlHandler TestCallbacks SetHandler perl-script PerlSetVar MasonCbClasses _ALL_ PerlSetVar MasonInterpClass MasonX::Interp::WithCallbacks PerlHandler HTML::Mason::ApacheHandler SetHandler perl-script PerlSetVar MasonDefaultPriority 3 PerlSetVar MasonDefaultPkgKey CBFoo PerlSetVar MasonCbExceptionHandler "sub {}" PerlAddVar MasonCallbacks "{ cb_key => 'priority', cb => sub { $_[0]->params->{result} = $_[0]->priority } }" PerlAddVar MasonCallbacks "{ cb_key => 'pkg_key', cb => sub { $_[0]->params->{result} = $_[0]->pkg_key } }" PerlAddVar MasonCallbacks "{ cb_key => 'die', cb => sub { die 'Ow!' } }" PerlAddVar MasonPreCallbacks "sub { my $params = $_[0]->params; $params->{result} = uc $params->{result} if $params->{do_upper} }" PerlAddVar MasonPostCallbacks "sub { my $params = $_[0]->params; $params->{result} = lc $params->{result} if $params->{do_lower} }" PerlSetVar MasonInterpClass MasonX::Interp::WithCallbacks PerlHandler HTML::Mason::ApacheHandler SetHandler perl-script PerlSetVar MasonDefaultPkgKey CBFoo PerlSetVar MasonIgnoreNulls 1 PerlAddVar MasonCallbacks "{ cb_key => 'exec', cb => sub { $_[0]->params->{result} = 'executed' } }" PerlSetVar MasonInterpClass MasonX::Interp::WithCallbacks PerlHandler HTML::Mason::ApacheHandler MasonX-Interp-WithCallbacks-1.19/t/htdocs000755000765000024 011600017366 17723 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/t/htdocs/alt.mc000444000765000024 5311600017366 21117 0ustar00davidstaff000000000000% $m->print('This is the alt component.'); MasonX-Interp-WithCallbacks-1.19/t/htdocs/dhandler000444000765000024 12311600017366 21540 0ustar00davidstaff000000000000% $m->print(ref $ARGS{result} ? $ARGS{result}->($m) : $ARGS{result}); % return 200;MasonX-Interp-WithCallbacks-1.19/t/lib000755000765000024 011600017366 17205 5ustar00davidstaff000000000000MasonX-Interp-WithCallbacks-1.19/t/lib/TestCallbacks.pm000444000765000024 1656011600017366 22447 0ustar00davidstaff000000000000package TestOOCallbacks; use strict; use base qw(Params::Callback); use constant CLASS_KEY => 'OOCBTester'; __PACKAGE__->register_subclass; sub simple : Callback { my $self = shift; my $params = $self->params; $params->{result} = 'Success'; } sub priority : Callback { my $self = shift; my $params = $self->params; my $val = $self->value; $val = '5' if $val eq 'def'; $params->{result} .= " $val"; } sub multi : Callback { my $self = shift; my $params = $self->params; my $val = $self->value; $params->{result} = scalar @$val; } sub count : Callback { my $self = shift; my $params = $self->params; $params->{result}++; } sub upperit : PreCallback { my $self = shift; my $params = $self->params; $params->{result} = uc $params->{result} if $params->{do_upper}; } sub lowerit : PostCallback { my $self = shift; my $params = $self->params; $params->{result} = lc $params->{result} if $params->{do_lower}; } sub add_note : Callback { my $self = shift; $self->notes($self->value, $self->params->{note}); } sub get_note : Callback { my $self = shift; $self->params->{result} = $self->notes($self->value); } sub list_notes : Callback { my $self = shift; my $params = $self->params; my $notes = $self->notes; for my $k (sort keys %$notes) { $params->{result} .= "$k => $notes->{$k}\n"; } } sub clear : Callback { my $self = shift; $self->cb_request->clear_notes; } sub mason_note : Callback { my $self = shift; $self->params->{result} = sub { shift->notes($self->value ) }; } sub cbr_note : Callback { my $self = shift; $self->params->{result} = sub { shift->interp->cb_request->notes($self->value ) }; } package TestCallbacks; use strict; use HTML::Mason::ApacheHandler; use HTML::Mason::Exceptions; use constant HTTP_OK => 200; use constant KEY => 'myCallbackTester'; my $server; if ($ENV{MOD_PERL_API_VERSION}) { require Apache2::ServerUtil; $server = Apache2::ServerUtil->server; } else { require Apache; $server = Apache->server; } my $cfg = $server->dir_config; my %params = ( comp_root => $cfg->{MasonCompRoot}, interp_class => 'MasonX::Interp::WithCallbacks', ); sub simple { my $cb = shift; my $params = $cb->params; $params->{result} = 'Success'; } sub priority { my $cb = shift; my $params = $cb->params; my $val = $cb->value; $val = '5' if $val eq 'def'; $params->{result} .= " $val"; } sub multi { my $cb = shift; my $params = $cb->params; my $val = $cb->value; $params->{result} = scalar @$val; } sub count { my $cb = shift; my $params = $cb->params; $params->{result}++; } my $url = 'http://example.com/'; sub redir { my $cb = shift; my $wait = $cb->value; $cb->redirect($url, $wait); } sub add_header { my $cb = shift; my $params = $cb->params; $cb->apache_req->err_headers_out->set(@{$params}{qw(header value)}); } sub test_abort { my $cb = shift; $cb->abort($cb->value); } sub exception { my $cb = shift; my $params = $cb->params; if ($cb->value) { # Throw an exception object. HTML::Mason::Exception->throw( error => "He's dead, Jim" ); } else { # Just die. die "He's dead, Jim"; } } sub add_note { my $cb = shift; $cb->notes($cb->value, $cb->params->{note}); } sub get_note { my $cb = shift; $cb->params->{result} = $cb->notes($cb->value); } sub list_notes { my $cb = shift; my $params = $cb->params; my $notes = $cb->notes; for my $k (sort keys %$notes) { $params->{result} .= "$k => $notes->{$k}\n"; } } sub clear { my $cb = shift; $cb->cb_request->clear_notes; } sub mason_note { my $cb = shift; $cb->params->{result} = sub { shift->notes($cb->value ) }; } sub cbr_note { my $cb = shift; $cb->params->{result} = sub { shift->interp->cb_request->notes($cb->value ) }; } sub upperit { my $cb = shift; my $params = $cb->params; $params->{result} = uc $params->{result} if $params->{do_upper}; } sub lowerit { my $cb = shift; my $params = $cb->params; $params->{result} = lc $params->{result} if $params->{do_lower}; } my $ah = HTML::Mason::ApacheHandler->new ( %params, callbacks => [{ pkg_key => KEY, cb_key => 'simple', cb => \&simple }, { pkg_key => KEY, cb_key => 'priority', cb => \&priority }, { pkg_key => KEY, cb_key => 'multi', cb => \&multi }, { pkg_key => KEY, cb_key => 'count', cb => \&count }, { pkg_key => KEY, cb_key => 'redir', cb => \&redir }, { pkg_key => KEY, cb_key => 'add_header', cb => \&add_header }, { pkg_key => KEY, cb_key => 'test_abort', cb => \&test_abort }, { pkg_key => KEY, cb_key => 'exception', cb => \&exception }, { pkg_key => KEY, cb_key => 'add_note', cb => \&add_note }, { pkg_key => KEY, cb_key => 'get_note', cb => \&get_note }, { pkg_key => KEY, cb_key => 'list_notes', cb => \&list_notes }, { pkg_key => KEY, cb_key => 'clear', cb => \&clear }, { pkg_key => KEY, cb_key => 'mason_note', cb => \&mason_note }, { pkg_key => KEY, cb_key => 'cbr_note', cb => \&cbr_note }, ], pre_callbacks => [\&upperit], post_callbacks => [\&lowerit], ); my %tests = ( '/exception_handler' => sub { my $ahwc = HTML::Mason::ApacheHandler->new ( %params, cb_exception_handler => sub { # Just log the exception. print STDERR 'Got "', shift->error, "\"\n" }, callbacks => [{ pkg_key => KEY, cb_key => 'exception', cb => \&exception }] ); $ahwc->handle_request(@_); }, '/no_null' => sub { my $ahwc = HTML::Mason::ApacheHandler->new ( %params, ignore_nulls => 1, callbacks => [{ pkg_key => KEY, cb_key => 'simple', cb => \&simple }] ); $ahwc->handle_request(@_); }, '/oop' => sub { my $ahwc = HTML::Mason::ApacheHandler->new ( %params, cb_classes => 'ALL' ); $ahwc->handle_request(@_); }, ); sub handler { if (my $code = $tests{$_[0]->uri}) { return $code->(@_) } return $ah->handle_request(@_); } 1; __END__ MasonX-Interp-WithCallbacks-1.19/t/lib/TieOut.pm000444000765000024 65711600017366 21101 0ustar00davidstaff000000000000package TieOut; # This module is swiped and adapted from ExtUtils::MakeMaker. sub TIEHANDLE { bless [], ref $_[0] || $_[0] } sub PRINT { my $self = shift; push @$self, join '', @_; } sub PRINTF { my $self = shift; push @$self, sprintf @_; } sub READLINE { my $self = shift; return shift @$self; } sub read { my $self = shift; my $ret = join '', @$self; @$self = (); return $ret; } 1;