| 1 | package KelpX::AppBuilder; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Module::Find 'useall'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use File::ShareDir 'module_dir'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | KelpX::AppBuilder - Create re-usable apps with Kelp | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | KelpX::AppBuilder makes it trivial to reuse your entire route map and views in an entirely new Kelp application. You create a base app, which can still be run normally, and from there you can start a new project and reuse everything from your base app without duplicating things. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 USAGE | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | First off, we'll need to modify the structure so File::ShareDir knows where to find our views/assets globally. It should look something like this | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | BaseApp/lib/auto/BaseApp/views | ||||||||||||||||||||||||||||
| 24 | BaseApp/lib/auto/BaseApp/assets | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | You don't have to use the same names, just the structure. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Then, in your base application | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package BaseApp; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | use Kelp::Base 'Kelp'; | ||||||||||||||||||||||||||||
| 33 | use KelpX::AppBuilder 'Base'; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # we don't need a build method, KelpX::AppBuilder will automatically | ||||||||||||||||||||||||||||
| 36 | # create it based on what we define in the maps method | ||||||||||||||||||||||||||||
| 37 | sub maps { | ||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | '/' => BaseApp::Controller::Root->can('index'), | ||||||||||||||||||||||||||||
| 40 | '/login' => BaseApp::Controller::Auth->can('login'), | ||||||||||||||||||||||||||||
| 41 | '/accounts/manage/:id' => { | ||||||||||||||||||||||||||||
| 42 | to => BaseApp::Controller::Accounts->can('manage'), | ||||||||||||||||||||||||||||
| 43 | bridge => 1 | ||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | '/accounts/manage/:id/view', BaseApp::Controller::Accounts->can('view'), | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | B<Optionally> you can include an 'auto' method into your application. The auto method gets called before every single page, so it's handy to use to ensure a user is logged in. You can do this by adding | ||||||||||||||||||||||||||||
| 52 | 'auto' => 1 | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | to your maps hash ref. It will handle the route line and bridging for you. Don't forget to create the auto method in BaseApp::Controller::Root if you enable this though! An example: | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | package BaseApp::Controller::Root; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub auto { | ||||||||||||||||||||||||||||
| 59 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 60 | my $url = $self->named->{page}; | ||||||||||||||||||||||||||||
| 61 | unless ($url eq 'login') { | ||||||||||||||||||||||||||||
| 62 | if (my $user = $self->user) { | ||||||||||||||||||||||||||||
| 63 | return 1; | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | return; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | return 1; | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | We'll call our new app 'TestApp' (original, eh?). Copy across your config from BaseApp into your TestApp conf, then tell KelpX::AppBuilder what module it should use as its base dir. Once this is done, it will generate a method called C<base_path> for you to use. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | use KelpX::AppBuilder Config => 'BaseApp'; | ||||||||||||||||||||||||||||
| 75 | middleware_init => { | ||||||||||||||||||||||||||||
| 76 | Static => { | ||||||||||||||||||||||||||||
| 77 | path => qw{^/assets/|^/apps/}, | ||||||||||||||||||||||||||||
| 78 | root => base_path(), | ||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | ... | ||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # use local views, and the one from BaseApp | ||||||||||||||||||||||||||||
| 85 | 'Template::Toolkit' => { | ||||||||||||||||||||||||||||
| 86 | ENCODING => 'utf8', | ||||||||||||||||||||||||||||
| 87 | INCLUDE_PATH => [ | ||||||||||||||||||||||||||||
| 88 | './views', | ||||||||||||||||||||||||||||
| 89 | base_path() . '/views' | ||||||||||||||||||||||||||||
| 90 | ], | ||||||||||||||||||||||||||||
| 91 | RELATIVE => 1, | ||||||||||||||||||||||||||||
| 92 | TAG_STYLE => 'asp', | ||||||||||||||||||||||||||||
| 93 | }, | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | The final part is loading your BaseApp controllers into your TestApp. That's fairly easy. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | package TestApp; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | use Kelp::Base 'Kelp'; | ||||||||||||||||||||||||||||
| 100 | use KelpX::AppBuilder; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub build { | ||||||||||||||||||||||||||||
| 103 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 104 | my $r = $self->routes; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | KelpX::AppBuilder->new('BaseApp')->add_maps($r); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # now you can add TestApp's own routing | ||||||||||||||||||||||||||||
| 109 | $r->add('/hello', sub { "Hello, world!" }); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Congratulations. You've just reused the controllers from your BaseApp. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub import { | ||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my ($me, @opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=8 SCAR=28.4
| |||||||||||||||||||||||||||||
| 120 | 2 | my $class = caller; | |||||||||||||||||||||||||||
| 121 | 2 | ▶ | if (@opts and $opts[0] eq 'Base') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@opts and $opts[0] eq "Base"
MC/DC50%1/2
@opts and $opts[0] eq "Base"
@opts
$opts[0] eq "Base"
Truth table66%2/3
A and B
A= @optsB= $opts[0] eq "Base"
| |||||||||||||||||||||||||||||
| 122 | 1 | my @controllers = useall "${class}::Controller"; | |||||||||||||||||||||||||||
| 123 | 1 | for my $c (@controllers) { | |||||||||||||||||||||||||||
| 124 | 0 | eval "use $c"; | |||||||||||||||||||||||||||
| 125 | 0 | say "=> Loaded controller $c"; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 1 | *{"${class}::build"} = sub { | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 132 | 0 | my $r = $self->routes; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | my $maps = $class->maps; | |||||||||||||||||||||||||||
| 135 | 0 | for my $method (keys %$maps) { | |||||||||||||||||||||||||||
| 136 | 0 | $r->add($method, $maps->{$method}); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 1 | }; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | if (@opts and $opts[0] eq 'Config') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@opts and $opts[0] eq "Config"
MC/DC0%0/2
@opts and $opts[0] eq "Config"
@opts
$opts[0] eq "Config"
Truth table66%2/3
A and B
A= @optsB= $opts[0] eq "Config"
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (scalar @opts > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | my $mod = $opts[1]; | |||||||||||||||||||||||||||
| 145 | 0 | eval "use $mod"; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | die "[error] Could not load base module $mod into config: $@\n"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | *{"${class}::base_path"} = sub { return module_dir($mod) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||
| 156 | 0 | die "[error] Config import option expects a base app name\n"; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub new { | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my ($class, $base) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $self = { name => $base }; | |||||||||||||||||||||||||||
| 164 | 0 | eval "use $base"; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | print STDERR "[error] Failed to open $base: $@\n"; | |||||||||||||||||||||||||||
| 167 | 0 | exit 5; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | my @controllers = useall "${base}::Controller"; | |||||||||||||||||||||||||||
| 171 | eval "use $_" | ||||||||||||||||||||||||||||
| 172 | 0 | for @controllers; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub load_controllers { | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my ($self, @controllers) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_controllers: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 0 | for my $c (@controllers) { | |||||||||||||||||||||||||||
| 181 | 0 | eval "use $self->{name}::Controller::$c"; | |||||||||||||||||||||||||||
| 182 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | die "[error] Could not load controller $c: $@\n"; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | return $self; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub add_maps { | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | my ($self, $r) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_maps: not called CC=6 SCAR=34.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 0 | my $class = caller; | |||||||||||||||||||||||||||
| 194 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | my $mod = $self->{name}; | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | unless ($mod->can('maps')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | print STDERR "Base mod $mod is missing 'maps' method\n"; | |||||||||||||||||||||||||||
| 199 | 0 | exit 3; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | my @no_import = qw(new build import); | |||||||||||||||||||||||||||
| 203 | 0 | foreach my $method (keys %{"${mod}::"}) { | |||||||||||||||||||||||||||
| 204 | 0 | *{"${class}::${method}"} = *{"${mod}::${method}"} | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | unless grep { $_ eq $method } @no_import; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 0 | my $maps = $mod->maps; | |||||||||||||||||||||||||||
| 208 | 0 | foreach my $path (keys %$maps) { | |||||||||||||||||||||||||||
| 209 | 0 | ▶ | if ($path eq 'auto') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | my $root = "${mod}::Controller::Root"; | |||||||||||||||||||||||||||
| 211 | 0 | $r->add('/:page' => { to => $root->can('auto'), bridge => 1 }); | |||||||||||||||||||||||||||
| 212 | 0 | next; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | $r->add($path, $maps->{$path}); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 PLEASE NOTE | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | This module is still a work in progress, so I would advise against using KelpX::AppBuilder in a production environment. I'm still looking at ways to make KelpX::AppBuilder more user friendly, but unfortunately reusing an application is not a simple process :-) | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Brad Haywood <brad@perlpowered.com> | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | You may distribute this code under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | 2 | 1; | |||||||||||||||||||||||||||
| 235 | __END__ | ||||||||||||||||||||||||||||