let us use CCAR instead of doing our own thing
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Keyword / Controller.pm
CommitLineData
918fb36e 1use MooseX::Declare;
0a99459e 2use Catalyst::Controller;
918fb36e 3
8e5d9092 4class CatalystX::Declare::Keyword::Controller
5 extends CatalystX::Declare::Keyword::Component {
918fb36e 6
7 use MooseX::MethodAttributes ();
9c11a562 8 use aliased 'CatalystX::Declare::Keyword::Action', 'ActionKeyword';
9c11a562 9 use aliased 'CatalystX::Declare::Controller::DetermineActionClass';
392e5076 10 use aliased 'CatalystX::Declare::Controller::Meta::TypeConstraintMapping';
ed4a2203 11 use aliased 'CatalystX::Declare::Controller::ActionPreparation';
918fb36e 12
856ac9a7 13 use Data::Dump qw( pp );
14
918fb36e 15
16 before add_namespace_customizations (Object $ctx, Str $package) {
a0ebba1d 17
918fb36e 18 MooseX::MethodAttributes->init_meta(for_class => $package);
8e5d9092 19
a1dd1788 20 $ctx->add_preamble_code_parts(
392e5076 21 ['BEGIN',
22 sprintf('Class::MOP::load_class(q(%s))', TypeConstraintMapping),
23 sprintf('%s->meta->apply(%s->meta->meta)', TypeConstraintMapping, $package),
24 ],
0a99459e 25 );
26 }
27
28 after add_extends_option_customizations (Object $ctx, Str $package, $superclasses, $options) {
29
30 $ctx->add_scope_code_parts(
a1dd1788 31 sprintf('with qw( %s )', join ' ',
a1dd1788 32 DetermineActionClass,
ed4a2203 33 ActionPreparation,
a1dd1788 34 ),
35 );
918fb36e 36 }
37
2c3192ac 38 method default_superclasses { 'Catalyst::Controller::ActionRole' }
918fb36e 39
856ac9a7 40 method auto_make_immutable { 0 }
41
656c3cdd 42 around default_inner {
8e5d9092 43
44 my @modifiers = qw( );
45
46 return [
47 ( grep { my $id = $_->identifier; not grep { $id eq $_ } @modifiers } @{ $self->$orig() || [] } ),
48 ActionKeyword->new(identifier => 'action'),
49 ActionKeyword->new(identifier => 'under'),
50 ActionKeyword->new(identifier => 'final'),
51 ];
52 }
53
856ac9a7 54 method add_with_option_customizations (Object $ctx, $package, ArrayRef $roles, HashRef $options) {
55
f00bea98 56 $package = $ctx->qualify_namespace($package);
57
82fcd70a 58 $ctx->add_cleanup_code_parts('Moose::Util::apply_all_roles("' . $package . '", qw/'
59 . join(' ', map { $ctx->qualify_namespace($_) } @$roles)
60 . '/)')
61 if scalar @$roles;
856ac9a7 62
63 $ctx->add_cleanup_code_parts(
64 sprintf '%s->meta->make_immutable', $package
65 ) unless $options->{is}{mutable};
66 }
918fb36e 67}
68
856ac9a7 69__END__
70
71=head1 NAME
72
73CatalystX::Declare::Keyword::Controller - Declare Catalyst Controllers
74
75=head1 SYNOPSIS
76
77 controller MyApp::Web::Controller::Example
78 extends MyApp::Web::ControllerBase::CRUD
79 with MyApp::Web::ControllerRole::Caching {
80
81
82 $CLASS->config(option_name => 'value');
83
84
85 has attr => (is => 'rw', lazy_build => 1);
86
87 method _build_attr { 'Hello World' }
88
89
90 action base as '';
91
92 final action site, under base {
93 $ctx->response->body( $self->attr );
94 }
95 }
96
97=head1 DESCRIPTION
98
99This handler module allows the declaration of Catalyst controllers. The
8e5d9092 100C<controller> keyword is an extension of the
101L<CatalystX::Declare::Keyword::Component>, which in turn is an extension
102of L<MooseX::Declare/class> with all the
856ac9a7 103bells and whistles, including C<extends>, C<with>, C<method> and modifier
104declarations.
105
106In addition to the keywords and features provided by L<MooseX::Declare>, you
107can also specify your controller's actions declaratively. For the whole truth
108about the syntax refer to L<CatalystX::Declare::Keyword::Action>.
109
110For controller roles, please see L<CatalystX::Declare::Keyword::Role>. You can
111extend controllers with the C<extends> keyword and consume roles via C<with> as
112usual.
113
114=head1 SUPERCLASSES
115
116=over
117
8e5d9092 118=item L<CatalystX::Declare::Keyword::Component>
856ac9a7 119
120=back
121
122=head1 METHODS
123
124These methods are implementation details. Unless you are extending or
125developing L<CatalystX::Declare>, you should not be concerned with them.
126
127=head2 add_namespace_customizations
128
129 Object->add_namespace_customizations (Object $ctx, Str $package)
130
131This method modifier will initialise the controller with
8e5d9092 132L<MooseX::MethodAttributes> and add the
ed4a2203 133L<CatalystX::Declare::Controller::ActionPreparation> and
856ac9a7 134L<CatalystX::Declare::Controller::DetermineActionClass> controller roles
135before calling the original.
136
137=head2 default_superclasses
138
139 Str Object->default_superclasses ()
140
141Returns L<Catalyst::Controller> as the default superclass for all declared
142controllers.
143
856ac9a7 144=head2 add_with_option_customizations
145
146 Object->add_with_option_customizations (
147 Object $ctx,
148 Str $package,
149 ArrayRef $roles,
150 HashRef $options,
151 )
152
153This hook method will be called by L<MooseX::Declare> when C<with> options were
82fcd70a 154encountered. Since 0.011 the roles will be applied all at once.
856ac9a7 155
156This method will also add a callback to make the controller immutable to the
157cleanup code parts unless C<is mutable> was specified.
158
8e5d9092 159=head2 auto_make_immutable
160
161 Bool Object->auto_make_immutable ()
162
163Returns C<0>, indicating that L<MooseX::Declare> should not make this class
164immutable by itself. We will do that in the L</add_with_option_customizations>
165method ourselves.
166
856ac9a7 167=head2 default_inner
168
169 ArrayRef[Object] Object->default_inner ()
170
171A method modifier around the original. The inner syntax handlers inherited by
172L<MooseX::Declare::Syntax::Keyword::Class> are extended with instances of the
173L<CatalystX::Declare::Keyword::Action> handler class for the C<action>,
174C<under> and C<final> identifiers.
175
176=head1 SEE ALSO
177
178=over
179
180=item L<CatalystX::Declare>
181
182=item L<CatalystX::Declare::Keyword::Action>
183
8e5d9092 184=item L<CatalystX::Declare::Keyword::Component>
185
856ac9a7 186=item L<MooseX::Declare/class>
187
188=back
189
190=head1 AUTHOR
191
192See L<CatalystX::Declare/AUTHOR> for author information.
193
194=head1 LICENSE
195
196This program is free software; you can redistribute it and/or modify it under
197the same terms as perl itself.
198
199=cut