Fix bugs introduced previously
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent / ModelToControllerReflector.pm
1 package CatalystX::DynamicComponent::ModelToControllerReflector;
2 use Moose::Role;
3 use Moose::Util qw/does_role/;
4 use MooseX::Types::Moose qw/Str/;
5 use Moose::Util::TypeConstraints;
6 use namespace::autoclean;
7
8 my $mangle_attributes_on_generated_methods = sub {
9     my ($meta, $config) = @_;
10     foreach my $name (keys %{ $config->{methods}}) {
11         my $m = $meta->get_method($name);
12         $meta->register_method_attributes($m->body, ['Local']);
13     }
14 };
15
16 with 'CatalystX::DynamicComponent' => {
17     name => '_setup_dynamic_controller',
18     pre_immutable_hook => $mangle_attributes_on_generated_methods,
19 };
20
21 requires 'setup_components';
22
23 after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
24
25 sub _setup_dynamic_controllers {
26     my ($app) = @_;
27
28     my @model_names = grep { /::Model::/ } keys %{ $app->components };
29     foreach my $model_name (@model_names) {
30         $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
31     }
32 }
33
34 my $interface = 'CatalystX::DynamicComponent::ModelToControllerReflector::Strategy';
35 role_type $interface;
36
37 sub _reflect_model_to_controller {
38     my ( $app, $model_name, $model ) = @_;
39
40     # Model passed in as MyApp::Model::Foo, strip MyApp
41     $model_name =~ s/^[^:]+:://;
42
43     # Get Controller::Foo
44     my $controller_name = $model_name;
45     $controller_name =~ s/^Model::/Controller::/;
46
47     # Get Foo
48     my $suffix = $model_name;
49     $suffix =~ s/Model:://;
50
51     my %controller_methods;
52     # FIXME - Abstract this strategy crap out.
53
54     my $config = exists $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'}
55         ? $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'} : {};
56     my $strategy = exists $config->{reflection_strategy} ? $config->{reflection_strategy} : 'InterfaceRoles';
57     $strategy = "CatalystX::DynamicComponent::ModelToControllerReflector::Strategy::$strategy";
58     Class::MOP::load_class($strategy);
59     $strategy->new;
60
61     my $model_methods = $model->meta->get_method_map;
62     foreach my $method_name ( $strategy->get_reflected_method_list($app, $model_name) ) {
63         # Note need to pass model name, as the method actually comes from
64         # the underlying model class, not the Catalyst shim class we autogenerated.
65         $controller_methods{$method_name} = 
66              $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
67     }
68
69     # Shallow copy so we don't stuff method refs in config
70     my $controller_config = { %{$app->config->{$controller_name}||{}} };
71
72     $controller_config->{methods} = \%controller_methods;
73     $app->_setup_dynamic_controller( $controller_name, $controller_config );
74 }
75
76 sub generate_reflected_controller_action_method {
77     my ( $app, $model, $method ) = @_;
78     my $method_name = $method->name; # Is it worth passing the actual method object here?
79     sub {
80         my ($self, $c, @args) = @_;
81         $c->res->header('X-From-Model', $model);
82         my $response = $c->model($model)->$method_name($c->req->data);
83         $c->res->header('X-From-Model-Data', $response);
84         $c->res->body('OK');
85         $c->stash->{response} = $response;
86     };
87 }
88
89 1;
90
91 __END__
92
93 =head1 NAME
94
95 CatalystX::DynamicComponent::ModelToControllerReflector - Generate Catalyst controllers automaticall from models and configuration.
96
97 =head1 SYNOPSIS
98
99 =head1 DESCRIPTION
100
101 =head1 LINKS
102
103 L<Catalyst>, L<MooseX::MethodAttributes>, L<CatalystX::ModelsFromConfig>.
104
105 =head1 BUGS
106
107 Probably plenty, test suite certainly isn't comprehensive.. Patches welcome.
108
109 =head1 AUTHOR
110
111 Tomas Doran (t0m) <bobtfish@bobtfish.net>
112
113 =head1 LICENSE
114
115 This code is copyright (c) 2009 Tomas Doran. This code is licensed on the same terms as perl
116 itself.
117
118 =cut
119