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   1557 use Mojo::Base 'Mojolicious::Plugin';
  2         6  
  2         19  
4 2     2   1480 use File::Spec::Functions qw(splitdir updir catdir file_name_is_absolute);
  2         5  
  2         150  
5 2     2   14 use Cwd qw(abs_path);
  2         6  
  2         2371  
6              
7             our $VERSION = '1.0.12';
8              
9             sub register {
10 2     2 1 146 my ($self, $app, $conf) = @_;
11              
12             # Config
13 2   50     11 $conf ||= {};
14 2 50       9 if ($conf->{prefix}) {
15 2         12 $conf->{prefix} =~ s{^/+}{};
16 2         7 $conf->{prefix} .= '/';
17 2         7 $conf->{prefix} =~ s{/+/$}{/};
18             }
19 2   50     7 my $root = ($conf->{prefix} || '/');
20 2   50     8 my $path = $conf->{path} || 'jsonrpc';
21 2         14 my $r = $app->routes;
22              
23 2 50       16 if ($conf->{controller}){
24             $r->any($root.$path)->to(
25             controller => $conf->{controller},
26             action => 'dispatch',
27 2 50       24 $conf->{namespace} ? ( namespace => $conf->{namespace}):(),
28             );
29             }
30 2 100       1012 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     7 : $app->home->rel_file($ENV{QX_SRC_PATH} || catdir(updir,'frontend','source'))
      33        
35             );
36 1         129 $app->log->info("Runnning in QX_SRC_MODE with files from $qx_app_src");
37 1         72 unshift @{$app->static->paths}, $qx_app_src;
  1         5  
38 1         9 my %prefixCache;
39 1         5 my $static = Mojolicious::Static->new();
40             my $static_cb = sub {
41 1     1   18458 my $ctrl = shift;
42 1         5 my $prefix = $ctrl->param('prefix');
43 1 50       39 if ($ctrl->param('file')){
44 1         27 $ctrl->req->url->path('/'.$prefix.'/'.$ctrl->param('file'));
45             }
46 1 50       67 if (not defined $prefixCache{$prefix}){
47 1         5 my $prefix_local = catdir(split /\//, $prefix);
48 1         3 my $path = $qx_app_src;
49 1         2 my $last_path = '';
50 1   33     31 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         7 $app->log->info("Auto register static path mapping from '$prefix' to '$path'");
55 1         31 $prefixCache{$prefix} = $path;
56             }
57 1         7 $static->paths([$prefixCache{$prefix}]);
58              
59 1 50       8 unless ($static->dispatch($ctrl)){
60 0         0 $ctrl->render(text=>$ctrl->req->url->path.' not found', status => 404);
61             }
62 1         9 };
63              
64 1         8 $r->get('/*prefix/framework/source/*a' => $static_cb );
65 1         468 $r->get('/*prefix/source/class/*a' => $static_cb );
66 1         400 $r->get('/*prefix/source/resource/*a' => $static_cb );
67 1         473 $r->get('/*prefix/source/script/*a' => $static_cb );
68 1         387 $r->get('/*prefix/downloads/*a/source/*b' => $static_cb );
69 1         424 $r->get('/source/index.html' => {prefix=>'source'} => $static_cb );
70 1         380 $r->get('/source/class/*b' => {prefix=>'source'} => $static_cb );
71 1         383 $r->get('/source/resource/*b' => {prefix=>'source'} => $static_cb );
72 1         375 $r->get('/source/script/*b' => {prefix=>'source'} => $static_cb );
73 1         366 $r->get($root.'*file' => {prefix => 'source', file => 'index.html' } => $static_cb);
74             }
75             else {
76             # redirect root to index.html
77 1     0   9 $r->any('/' => sub { shift->reply->static('/index.html')});
  0         0  
78 1 50       193 if ($root ne '/'){
79             $app->hook(before_dispatch => sub {
80 19     19   274778 my $self = shift;
81 19         159 my $file = $self->req->url->path->to_string;
82 19 100       2138 if ($file =~ s|^/${root}|/|){
83 18 50       91 if ($file eq '/') {
84 0         0 $file = '/index.html';
85             }
86 18 100       91 if (-r $app->home->rel_file('public'.$file)){
87 1         129 $self->req->url->path($file);
88 1         46 return $app->static->dispatch($self);
89             }
90             }
91 1         10 });
92             }
93             }
94             }
95              
96             1;
97              
98             __END__