File Coverage

lib/Mojolicious/Plugin/Qooxdoo.pm
Criterion Covered Total %
statement 61 66 92.4
branch 15 24 62.5
condition 6 15 40.0
subroutine 6 7 85.7
pod 1 1 100.0
total 89 113 78.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Qooxdoo;
2              
3 2     2   1437 use Mojo::Base 'Mojolicious::Plugin';
  2         6  
  2         16  
4 2     2   1222 use File::Spec::Functions qw(splitdir updir catdir file_name_is_absolute);
  2         4  
  2         144  
5 2     2   14 use Cwd qw(abs_path);
  2         4  
  2         2266  
6              
7             our $VERSION = '1.0.13';
8              
9             sub register {
10 2     2 1 144 my ($self, $app, $conf) = @_;
11              
12             # Config
13 2   50     11 $conf ||= {};
14 2 50       8 if ($conf->{prefix}) {
15 2         12 $conf->{prefix} =~ s{^/+}{};
16 2         7 $conf->{prefix} .= '/';
17 2         7 $conf->{prefix} =~ s{/+/$}{/};
18             }
19 2   50     10 my $root = ($conf->{prefix} || '/');
20 2   50     6 my $path = $conf->{path} || 'jsonrpc';
21 2         16 my $r = $app->routes;
22              
23 2 50       17 if ($conf->{controller}){
24             $r->any($root.$path)->to(
25             controller => $conf->{controller},
26             action => 'dispatch',
27 2 50       29 $conf->{namespace} ? ( namespace => $conf->{namespace}):(),
28             );
29             }
30 2 100       1155 if ($ENV{QX_SRC_MODE}){
31             my $qx_app_src = abs_path(
32             $ENV{QX_SRC_PATH} && file_name_is_absolute($ENV{QX_SRC_PATH})
33             ? $ENV{QX_SRC_PATH}
34 1 50 33     8 : $app->home->rel_file($ENV{QX_SRC_PATH} || catdir(updir,'frontend','source'))
      33        
35             );
36 1         138 $app->log->info("Runnning in QX_SRC_MODE with files from $qx_app_src");
37 1         80 unshift @{$app->static->paths}, $qx_app_src;
  1         6  
38 1         10 my %prefixCache;
39 1         5 my $static = Mojolicious::Static->new();
40             my $static_cb = sub {
41 1     1   19225 my $ctrl = shift;
42 1         6 my $prefix = $ctrl->param('prefix');
43 1 50       46 if ($ctrl->param('file')){
44 1         30 $ctrl->req->url->path('/'.$prefix.'/'.$ctrl->param('file'));
45             }
46 1 50       116 if (not defined $prefixCache{$prefix}){
47 1         6 my $prefix_local = catdir(split /\//, $prefix);
48 1         3 my $path = $qx_app_src;
49 1         3 my $last_path = '';
50 1   33     34 while ($path ne $last_path and not -d catdir($path,$prefix_local)){
51 0         0 $last_path = $path;
52 0         0 $path = abs_path(catdir($last_path,updir));
53             }
54 1         8 $app->log->info("Auto register static path mapping from '$prefix' to '$path'");
55 1         33 $prefixCache{$prefix} = $path;
56             }
57 1         8 $static->paths([$prefixCache{$prefix}]);
58              
59 1 50       16 unless ($static->dispatch($ctrl)){
60 0         0 $ctrl->render(text=>$ctrl->req->url->path.' not found', status => 404);
61             }
62 1         10 };
63              
64 1         9 $r->get('/*prefix/framework/source/*a' => $static_cb );
65 1         463 $r->get('/*prefix/source/class/*a' => $static_cb );
66 1         389 $r->get('/*prefix/source/resource/*a' => $static_cb );
67 1         418 $r->get('/*prefix/source/script/*a' => $static_cb );
68 1         384 $r->get('/*prefix/downloads/*a/source/*b' => $static_cb );
69 1         479 $r->get('/source/index.html' => {prefix=>'source'} => $static_cb );
70 1         365 $r->get('/source/class/*b' => {prefix=>'source'} => $static_cb );
71 1         362 $r->get('/source/resource/*b' => {prefix=>'source'} => $static_cb );
72 1         435 $r->get('/source/script/*b' => {prefix=>'source'} => $static_cb );
73 1         369 $r->get($root.'*file' => {prefix => 'source', file => 'index.html' } => $static_cb);
74             }
75             else {
76             # redirect root to index.html
77 1     0   8 $r->any('/' => sub { shift->reply->static('/index.html')});
  0         0  
78 1 50       195 if ($root ne '/'){
79             $app->hook(before_dispatch => sub {
80 19     19   230184 my $self = shift;
81 19         89 my $file = $self->req->url->path->to_string;
82 19 100       1674 if ($file =~ s|^/${root}|/|){
83 18 50       64 if ($file eq '/') {
84 0         0 $file = '/index.html';
85             }
86 18 100       70 if (-r $app->home->rel_file('public'.$file)){
87 1         89 $self->req->url->path($file);
88 1         44 return $app->static->dispatch($self);
89             }
90             }
91 1         9 });
92             }
93             }
94             }
95              
96             1;
97              
98             __END__