memory leak. catalyst 5.8 will fix this problem
[catagits/Reaction.git] / lib / Reaction / UI / Controller.pm
1 package Reaction::UI::Controller;
2
3 use base qw(Catalyst::Controller Reaction::Object);
4
5 use Reaction::Class;
6 use Scalar::Util 'weaken';
7 use namespace::clean -except => [ qw(meta) ];
8
9 has context => (is => 'ro', isa => 'Object', weak_ref => 1);
10 with 'Catalyst::Component::InstancePerContext';
11
12 sub build_per_context_instance {
13   my ($self, $c, @args) = @_;
14   my $newself =  $self->new($self->_application, {%$self, context => $c, @args});
15   weaken $newself->{context}; #stopgap till cat 5.8
16   return $newself;
17 }
18
19 sub push_viewport {
20   my $self = shift;
21   my $c = $self->context;
22   my $focus_stack = $c->stash->{focus_stack};
23   my ($class, @proto_args) = @_;
24   my %args;
25   if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
26     if (ref($vp_attr) eq 'ARRAY') {
27       $vp_attr = $vp_attr->[0];
28     }
29     if (ref($vp_attr) eq 'HASH') {
30       if (my $conf_class = delete $vp_attr->{class}) {
31         $class = $conf_class;
32       }
33       %args = %{ $self->merge_config_hashes($vp_attr, {@proto_args}) };
34     } else {
35       $class = $vp_attr;
36       %args = @proto_args;
37     }
38   } else {
39     %args = @proto_args;
40   }
41
42   $args{ctx} = $c;
43
44   if (exists $args{next_action} && !ref($args{next_action})) {
45     $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
46   }
47   $focus_stack->push_viewport($class, %args);
48 }
49
50 sub pop_viewport {
51   return shift->context->stash->{focus_stack}->pop_viewport;
52 }
53
54 sub pop_viewports_to {
55   my ($self, $vp) = @_;
56   return $self->context->stash->{focus_stack}->pop_viewports_to($vp);
57 }
58
59 sub redirect_to {
60   my ($self, $c, $to, $cap, $args, $attrs) = @_;
61
62   #the confess calls could be changed later to $c->log ?
63   my $action;
64   my $reftype = ref($to);
65   if( $reftype eq '' ){
66     $action = $self->action_for($to);
67     confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
68   } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
69     $action = $c->controller($to->[0])->action_for($to->[1]);
70     confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
71   } elsif( blessed $to && $to->isa('Catalyst::Action') ){
72     $action = $to;
73   } else{
74     confess("Failed to locate action from ${to}");
75   }
76
77   $cap ||= $c->req->captures;
78   $args ||= $c->req->args;
79   $attrs ||= {};
80   my $uri = $c->uri_for($action, $cap, @$args, $attrs);
81   $c->res->redirect($uri);
82 }
83
84 sub make_context_closure {
85   my($self, $closure) = @_;
86   my $ctx = $self->context;
87   weaken($ctx);
88   return sub { $closure->($ctx, @_) };
89 }
90
91 1;
92
93 __END__;
94
95 =head1 NAME
96
97 Reaction::UI::Controller
98
99 =head1 DESCRIPTION
100
101 Base Reaction Controller class. Inherits from:
102
103 =over 4
104
105 =item L<Catalyst::Controller>
106 =item L<Catalyst::Component::ACCEPT_CONTEXT>
107 =item L<Reaction::Object>
108
109 =back
110
111 =head1 METHODS
112
113 =head2 push_viewport $vp_class, %args
114
115 Creates a new instance of the L<Reaction::UI::ViewPort> class
116 ($vp_class) using the rest of the arguments given (%args). Defaults of
117 the action can be overridden by using the C<ViewPort> key in the
118 controller configuration. For example to override the default number
119 of items in a CRUD list action:
120
121 __PACKAGE__->config(
122                     action => { 
123                         list => { ViewPort => { per_page => 50 } },
124     }
125   );
126
127 The ViewPort is added to the L<Reaction::UI::Window>'s FocusStack in
128 the stash, and also returned to the calling code.
129
130 Related items:
131
132 =over
133
134 =item L<Reaction::UI::Controller::Root>
135 =item L<Reaction::UI::Window>
136
137 =back
138
139 TODO: explain how next_action as a scalar gets converted to the redirect arrayref thing
140
141 =head2 pop_viewport
142
143 =head2 pop_viewport_to $vp
144
145 Call L<Reaction::UI::FocusStack/pop_viewport> or
146 L<Reaction::UI::FocusStack/pop_viewport_to> on 
147 the C<< $c->stash->{focus_stack} >>.
148
149 =head2 redirect_to $c, $to, $captures, $args, $attrs
150
151 Construct a URI and redirect to it.
152
153 $to can be:
154
155 =over
156
157 =item The name of an action in the current controller.
158
159 =item A L<Catalyst::Action> instance.
160
161 =item An arrayref of controller name and the name of an action in that
162 controller.
163
164 =back
165
166 $captures and $args default to the current requests $captures and
167 $args if not supplied.
168
169 =head1 AUTHORS
170
171 See L<Reaction::Class> for authors.
172
173 =head1 LICENSE
174
175 See L<Reaction::Class> for the license.
176
177 =cut