File Coverage

blib/lib/Dancer2/Plugin/ControllerAutoload.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 6 66.6
condition 1 2 50.0
subroutine 8 8 100.0
pod 0 2 0.0
total 44 49 89.8


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::ControllerAutoload;
2 2     2   900262 use 5.006;
  2         10  
3 2     2   11 use strict;
  2         4  
  2         42  
4 2     2   11 use warnings;
  2         4  
  2         58  
5 2     2   1176 use Dancer2::Plugin;
  2         290312  
  2         15  
6 2     2   48232 use File::Find;
  2         5  
  2         118  
7 2     2   14 use Cwd qw(abs_path);
  2         5  
  2         688  
8              
9             =encoding utf-8
10              
11             =head1 NAME
12              
13             Dancer2::Plugin::ControllerAutoload - Autoload controllers
14              
15             =head1 SYNOPSIS
16              
17             When we C<use> the plugin in MyApp.pm it'll load all the controllers
18             under the C<Controller> directory, so you don't have to write one
19             C<use $controller> in MyApp.pm for each controller.
20              
21             # MyApp.pm
22             package MyApp;
23             use Dancer2;
24             use Dancer2::Plugin::ControllerAutoload;
25              
26             # MyApp/Controller/Users.pm
27             package MyApp::Controller::Users;
28             use Dancer2 appname => 'MyApp';
29              
30             # MyApp/Controller/Users/Thoughts.pm
31             package MyApp::Controller::Users::Thoughts;
32             use Dancer2 appname => 'MyApp';
33              
34             =head1 DESCRIPTION
35              
36             If you have these three controllers
37              
38             # MyApp/Controller/Users.pm
39             package MyApp::Controller::Users;
40             use Dancer2 appname => 'MyApp';
41              
42             # MyApp/Controller/Users/Thoughts.pm
43             package MyApp::Controller::Users::Thoughts;
44             use Dancer2 appname => 'MyApp';
45              
46             # MyApp/Controller/Services.pm
47             package MyApp::Controller::Services;
48             use Dancer2 appname => 'MyApp';
49              
50             you'd have to load each with an C<use>
51              
52             # MyApp.pm
53             package MyApp;
54             use Dancer2;
55             use MyApp::Controller::Users;
56             use MyApp::Controller::Users::Thoughts;
57             use MyApp::Controller::Services;
58              
59             This plugin simplifies this process. When you C<use> the plugin, all
60             controllers will be loaded.
61              
62             # MyApp.pm
63             package MyApp;
64             use Dancer2;
65             use Dancer2::Plugin::ControllerAutoload;
66              
67             A controller will be by default identified as such if it is under the
68             C<Controller> directory. But that's configurable. If your controller
69             directory is called "Contr":
70              
71             # in a config or environment file
72             plugins:
73             ControllerAutoload:
74             controller_dir: Contr
75              
76             =cut
77              
78             our $VERSION = '0.01';
79              
80             my (undef, $caller_filename) = caller;
81             $caller_filename = abs_path $caller_filename;
82             my @files_to_require;
83              
84             sub BUILD {
85 1     1 0 2149 my ($self) = @_;
86 1         3 my $caller_basedir = $caller_filename;
87 1         5 $caller_basedir =~ s/\.pm$//;;
88 1   50     21 my $path_part = $self->config->{controller_dir} || 'Controller';
89              
90 1         69 my $finddir = File::Spec->catdir($caller_basedir, $path_part);
91 1 50       28 if (-d $finddir) {
92 1         134 find(\&process_item, $finddir);
93             }
94              
95 1         6 for my $file (@files_to_require) {
96 3         43919 $self->dsl->info("Loading controller $file");
97 3         1505 require $file;
98             }
99 1         18577 @files_to_require = ();
100             }
101              
102             sub process_item { # $_ is a filename like Users.pm
103 5 100   5 0 251 return unless -f;
104 3 50       20 return unless /\.pm$/;
105             # $File::Find::name will be like /opt/systems/App/lib/App/Controller/X.pm
106 3         95 push @files_to_require, $File::Find::name;
107             }
108              
109             =head1 AUTHOR
110              
111             Gil Magno, C<< <gilmagno at gilmagno.com> >>
112              
113             =head1 BUGS
114              
115             Please report any bugs or feature requests to
116             C<bug-dancer2-plugin-controllerautoload at rt.cpan.org>, or through
117             the web interface at
118             L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer2-Plugin-ControllerAutoload>.
119             I will be notified, and then you'll automatically be notified of
120             progress on your bug as I make changes.
121              
122             Also you can use github:
123             L<https://github.com/gilmagno/Dancer2-Plugin-ControllerAutoload>.
124              
125             =head1 ACKNOWLEDGEMENTS
126              
127             Angel Leyva, José Biskofski, Natanael Lizama, Uriel Lizama
128              
129             =head1 LICENSE AND COPYRIGHT
130              
131             This software is Copyright (c) 2021 by Gil Magno.
132              
133             This is free software, licensed under:
134              
135             The Artistic License 2.0 (GPL Compatible)
136              
137             =cut
138              
139             1;