X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Component-ACCEPT_CONTEXT.git;a=blobdiff_plain;f=lib%2FCatalyst%2FComponent%2FACCEPT_CONTEXT.pm;h=7526c771ef893e7970246542935713acbe698ed6;hp=f1e84c9fc5005efb6ac2c8eda85426da2bd07458;hb=75f37967953d7c4558c4e4bfd6525e2afc61fd7c;hpb=6166f03d12382556473156bd939721414a11fcb2 diff --git a/lib/Catalyst/Component/ACCEPT_CONTEXT.pm b/lib/Catalyst/Component/ACCEPT_CONTEXT.pm index f1e84c9..7526c77 100644 --- a/lib/Catalyst/Component/ACCEPT_CONTEXT.pm +++ b/lib/Catalyst/Component/ACCEPT_CONTEXT.pm @@ -1,11 +1,144 @@ package Catalyst::Component::ACCEPT_CONTEXT; -use strict; + use warnings; +use strict; +use NEXT; +use Scalar::Util qw(weaken); =head1 NAME -Catalyst::Component::ACCEPT_CONTEXT - +Catalyst::Component::ACCEPT_CONTEXT - Make the current Catalyst +request context available in Models and Views. + +=head1 VERSION + +Version 0.05 + +=cut + +our $VERSION = '0.05'; + +=head1 SYNOPSIS + +Models and Views don't usually have access to the request object, +since they probably don't really need it. Sometimes, however, having +the request context available outside of Controllers makes your +application cleaner. If that's the case, just use this module as a +base class: + + package MyApp::Model::Foobar; + use base qw|Catalyst::Component::ACCEPT_CONTEXT Catalyst::Model|; + +Then, you'll be able to get the current request object from within +your model: + + sub do_something { + my $self = shift; + print "The current URL is ". $self->context->req->uri->as_string; + } + +=head1 METHODS + +=head2 context + +Returns the current request context. + +=cut + +sub context { + return shift->{context}; +} + +=head2 ACCEPT_CONTEXT + +Catalyst calls this method to give the current context to your model. +You should never call it directly. + +Note that a new instance of your component isn't created. All we do +here is shove C<$c> into your component. ACCEPT_CONTEXT allows for +other behavior that may be more useful; if you want something else to +happen just implement it yourself. + +See L for details. + +=cut + +sub ACCEPT_CONTEXT { + my $self = shift; + my $context = shift; + + $self->{context} = $context; + weaken($self->{context}); + + return $self->NEXT::ACCEPT_CONTEXT($context, @_) || $self; +} + +=head2 COMPONENT + +Overridden to use initial application object as context before a request. + +=cut + +sub COMPONENT { + my $class = shift; + my $app = shift; + my $args = shift; + $args->{context} = $app; + weaken($args->{context}) if ref $args->{context}; + return $class->NEXT::COMPONENT($app, $args, @_); +} + +=head1 AUTHOR + +Jonathan Rockway, C<< >> + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L. +I will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Catalyst::Component::ACCEPT_CONTEXT + +You can also look for information at: + +=over 4 + +=item * Catalyst Website + +L + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * RT: CPAN's request tracker + +L + +=item * Search CPAN + +L + +=back + +=head1 COPYRIGHT & LICENSE + +Copyright 2007 Jonathan Rockway. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. =cut -1; +1; # End of Catalyst::Component::ACCEPT_CONTEXT