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