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