1 package CatalystX::ModelToControllerReflector;
3 use namespace::autoclean;
5 with 'CatalystX::DynamicComponent' => {
6 name => '_setup_dynamic_controller',
7 pre_immutable_hook => '_setup_dynamic_controller_meta',
10 requires 'setup_components';
12 after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
14 sub _setup_dynamic_controllers {
16 my @model_names = grep { /::Model::/ } keys %{ $app->components };
18 foreach my $model_name (@model_names) {
19 $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
23 sub _reflect_model_to_controller {
24 my ( $app, $model_name, $model ) = @_;
26 my $class = blessed($app) || $app;
28 my $controller_name = $model_name;
29 $controller_name =~ s/::Model::/::Controller::/;
31 my $suffix = $model_name;
32 $suffix =~ s/^.*::Model:://;
34 my $controller = $app->_setup_dynamic_controller( $controller_name, {} );
35 my $meta = $controller->meta;
36 $meta->make_mutable; # Dirty, I should build the class, add the methods, then
37 # last of all make it a component. The only reason it works
38 # like this is that I wrote the simple thing for the model
39 # code, abstracted _just_ enough to make it fly with this
40 # dirty hack, then stopped. EVERY TIME YOU DO THIS KITTENS DIE
41 my $methods = $model->meta->get_method_map;
42 foreach my $method_name (keys %$methods) {
43 $controller->meta->add_method(
44 # Note need to pass model name, as the method actually comes from
45 # the underlying model class, not the Catalyst shim class we autogenerated.
46 $method_name => $app->generate_reflected_controller_action_method($suffix, $methods->{$method_name})
49 $meta->make_immutable;
52 sub _setup_dynamic_controller_meta {
53 my ($app, $meta) = @_;
54 # Wrong namespace, should be config
55 # and we force it to do a role to
56 # add our crap, allowing the user
57 # to overlay functionality..
58 $meta->superclasses($app . '::ControllerBase', $meta->superclasses);
61 sub generate_reflected_controller_action_method {
62 my ( $app, $model, $method ) = @_;
63 my $method_name = $method->name; # Is it worth passing the actual method object here?
65 my ($self, $c, @args) = @_;
66 $c->res->header('X-From-Model', $model);
67 $c->res->header('X-From-Model-Data', $c->model($model)->$method_name(@args));