import my old svn repo here
[catagits/Catalyst-Component-ACCEPT_CONTEXT.git] / lib / Catalyst / Component / ACCEPT_CONTEXT.pm
CommitLineData
6166f03d 1package Catalyst::Component::ACCEPT_CONTEXT;
75f37967 2
6166f03d 3use warnings;
75f37967 4use strict;
5use NEXT;
6use Scalar::Util qw(weaken);
6166f03d 7
8=head1 NAME
9
75f37967 10Catalyst::Component::ACCEPT_CONTEXT - Make the current Catalyst
11request context available in Models and Views.
12
13=head1 VERSION
14
15Version 0.05
16
17=cut
18
19our $VERSION = '0.05';
20
21=head1 SYNOPSIS
22
23Models and Views don't usually have access to the request object,
24since they probably don't really need it. Sometimes, however, having
25the request context available outside of Controllers makes your
26application cleaner. If that's the case, just use this module as a
27base class:
28
29 package MyApp::Model::Foobar;
30 use base qw|Catalyst::Component::ACCEPT_CONTEXT Catalyst::Model|;
31
32Then, you'll be able to get the current request object from within
33your model:
34
35 sub do_something {
36 my $self = shift;
37 print "The current URL is ". $self->context->req->uri->as_string;
38 }
39
40=head1 METHODS
41
42=head2 context
43
44Returns the current request context.
45
46=cut
47
48sub context {
49 return shift->{context};
50}
51
52=head2 ACCEPT_CONTEXT
53
54Catalyst calls this method to give the current context to your model.
55You should never call it directly.
56
57Note that a new instance of your component isn't created. All we do
58here is shove C<$c> into your component. ACCEPT_CONTEXT allows for
59other behavior that may be more useful; if you want something else to
60happen just implement it yourself.
61
62See L<Catalyst::Component> for details.
63
64=cut
65
66sub ACCEPT_CONTEXT {
67 my $self = shift;
68 my $context = shift;
69
70 $self->{context} = $context;
71 weaken($self->{context});
72
73 return $self->NEXT::ACCEPT_CONTEXT($context, @_) || $self;
74}
75
76=head2 COMPONENT
77
78Overridden to use initial application object as context before a request.
79
80=cut
81
82sub COMPONENT {
83 my $class = shift;
84 my $app = shift;
85 my $args = shift;
86 $args->{context} = $app;
87 weaken($args->{context}) if ref $args->{context};
88 return $class->NEXT::COMPONENT($app, $args, @_);
89}
90
91=head1 AUTHOR
92
93Jonathan Rockway, C<< <jrockway at cpan.org> >>
94
95=head1 BUGS
96
97Please report any bugs or feature requests to
98C<bug-catalyst-component-accept_context at rt.cpan.org>, or through the web interface at
99L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Component-ACCEPT_CONTEXT>.
100I will be notified, and then you'll automatically be notified of progress on
101your bug as I make changes.
102
103=head1 SUPPORT
104
105You can find documentation for this module with the perldoc command.
106
107 perldoc Catalyst::Component::ACCEPT_CONTEXT
108
109You can also look for information at:
110
111=over 4
112
113=item * Catalyst Website
114
115L<http://www.catalystframework.org/>
116
117=item * AnnoCPAN: Annotated CPAN documentation
118
119L<http://annocpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
120
121=item * CPAN Ratings
122
123L<http://cpanratings.perl.org/d/Catalyst-Component-ACCEPT_CONTEXT>
124
125=item * RT: CPAN's request tracker
126
127L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Component-ACCEPT_CONTEXT>
128
129=item * Search CPAN
130
131L<http://search.cpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
132
133=back
134
135=head1 COPYRIGHT & LICENSE
136
137Copyright 2007 Jonathan Rockway.
138
139This program is free software; you can redistribute it and/or modify it
140under the same terms as Perl itself.
6166f03d 141
142=cut
143
75f37967 1441; # End of Catalyst::Component::ACCEPT_CONTEXT