File Coverage

lib/Mojolicious/Plugin/Fondation/Action/Controllers.pm
Criterion Covered Total %
statement 33 33 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 3 3 100.0
pod 0 1 0.0
total 42 48 87.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Fondation::Action::Controllers;
2             $Mojolicious::Plugin::Fondation::Action::Controllers::VERSION = '0.03';
3             # ABSTRACT: Auto-discovers controller classes under a plugin namespace
4              
5 13     13   94 use Mojo::Base 'Mojolicious::Plugin::Fondation::Action::Base', -signatures;
  13         23  
  13         112  
6              
7 13     13   3359 use Mojo::Loader 'find_modules';
  13         31  
  13         5488  
8              
9 158     158 0 218 sub after_load ($self, $long_name, $conf, $share_dir) {
  158         201  
  158         214  
  158         209  
  158         193  
  158         186  
10 158         319 my $manager = $self->manager;
11 158         555 my $app = $manager->app;
12              
13 158         508 my $plugin_entry = $manager->registry->{$long_name};
14 158 50       635 return unless $plugin_entry;
15              
16 158         282 my $plugin = $plugin_entry->{instance};
17 158 50 33     605 return unless $plugin && ref $plugin;
18              
19 158         262 my $short = $plugin_entry->{short_name};
20              
21             # Plugin's controller namespace
22 158         276 my $controller_ns = "${long_name}::Controller";
23              
24             # Search for modules in this namespace, excluding base classes
25 158         464 my @controllers = grep { !/::Base$/ } find_modules($controller_ns);
  185         88388  
26              
27             # Always set metadata for introspection
28 158         7653 $plugin_entry->{metadata}{controllers_count} = scalar @controllers;
29 158         319 $plugin_entry->{metadata}{controllers_ns} = $controller_ns;
30              
31 158 100       506 return unless @controllers;
32              
33             # Add namespace to routes (once only)
34 121         440 my $routes_ns = $app->routes->namespaces;
35 121 50       980 unless (grep { $_ eq $controller_ns } @$routes_ns) {
  315         549  
36 121         270 push @$routes_ns, $controller_ns;
37 121         176 my $count = scalar @controllers;
38 121         358 $self->log->debug("Controller namespace added: $controller_ns ($count controllers)");
39             }
40              
41             # store the list of controller names
42 121         1664 $plugin_entry->{controllers} = [ sort @controllers ];
43              
44 121         845 return $controller_ns;
45             }
46              
47             1;
48              
49             __END__