File Coverage

lib/Mojolicious/Plugin/QooxdooJsonrpc.pm
Criterion Covered Total %
statement 21 57 36.8
branch 1 10 10.0
condition 3 15 20.0
subroutine 5 7 71.4
pod 1 1 100.0
total 31 90 34.4


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::QooxdooJsonrpc;
2              
3 1     1   1222 use Mojo::Base 'Mojolicious::Plugin';
  1         3  
  1         7  
4 1     1   1195 use File::Spec::Functions qw(splitdir updir catdir file_name_is_absolute);
  1         2  
  1         56  
5 1     1   5 use Cwd qw(abs_path);
  1         1  
  1         54  
6              
7             our $VERSION = '0.95';
8             # the dispatcher module gets autoloaded, we list it here to
9             # make sure it is available and compiles at startup time and not
10             # only on demand.
11 1     1   6 use MojoX::Dispatcher::Qooxdoo::Jsonrpc;
  1         1  
  1         7  
12              
13             sub register {
14 1     1 1 141 my ($self, $app, $conf) = @_;
15              
16             # Config
17 1   50     6 $conf ||= {};
18 1   50     12 my $root = ($conf->{prefix} || '') . '/';
19 1         2 my $services = $conf->{services};
20 1   50     7 my $path = $conf->{path} || 'jsonrpc';
21 1         28 my $r = $app->routes;
22              
23 1         15 $r->route($root.$path)->to(
24             controller => 'Jsonrpc',
25             action => 'dispatch',
26             namespace => 'MojoX::Dispatcher::Qooxdoo',
27             # our own properties
28             services => $services
29             );
30              
31 1 50       654 if ($ENV{QX_SRC_MODE}){
32 0 0 0     0 my $qx_app_src = abs_path(
      0        
33             $ENV{QX_SRC_PATH} && file_name_is_absolute($ENV{QX_SRC_PATH})
34             ? $ENV{QX_SRC_PATH}
35             : $app->home->rel_dir($ENV{QX_SRC_PATH} || catdir(updir,'frontend','source'))
36             );
37 0         0 $app->log->info("Runnning in QX_SRC_MODE with files from $qx_app_src");
38 0         0 $app->static->paths([$qx_app_src]);
39 0         0 my %prefixCache;
40 0         0 my $static = Mojolicious::Static->new();
41             my $static_cb = sub {
42 0     0   0 my $self = shift;
43 0         0 my $prefix = $self->param('prefix');
44 0 0       0 if ($self->param('file')){
45 0         0 $self->req->url->path('/'.$prefix.'/'.$self->param('file'));
46             }
47 0 0       0 if (not defined $prefixCache{$prefix}){
48 0         0 my $prefix_local = catdir(split /\//, $prefix);
49 0         0 my $path = $qx_app_src;
50 0         0 my $last_path = '';
51 0   0     0 while ($path ne $last_path and not -d catdir($path,$prefix_local)){
52 0         0 $last_path = $path;
53 0         0 $path = abs_path(catdir($last_path,updir));
54             }
55 0         0 $app->log->info("Auto register static path mapping from '$prefix' to '$path'");
56 0         0 $prefixCache{$prefix} = $path;
57             }
58 0         0 $static->paths([$prefixCache{$prefix}]);
59              
60 0 0       0 unless ($static->dispatch($self)){
61 0         0 $self->render(text=>$self->req->url->path.' not found', status => 404);
62             }
63 0         0 };
64              
65 0         0 $r->get('/(*prefix)/framework/source/(*a)' => $static_cb );
66 0         0 $r->get('/(*prefix)/source/class/(*a)' => $static_cb );
67 0         0 $r->get('/(*prefix)/source/resource/(*a)' => $static_cb );
68 0         0 $r->get('/(*prefix)/source/script/(*a)' => $static_cb );
69 0         0 $r->get('/(*prefix)/downloads/(*a)/source/(*b)' => $static_cb );
70 0         0 $r->get('/source/index.html' => {prefix=>'source'} => $static_cb );
71 0         0 $r->get('/source/class/(*b)' => {prefix=>'source'} => $static_cb );
72 0         0 $r->get('/source/resource/(*b)' => {prefix=>'source'} => $static_cb );
73 0         0 $r->get('/source/script/(*b)' => {prefix=>'source'} => $static_cb );
74 0         0 $r->get($root.'(*file)' => {prefix => 'source', file => 'index.html' } => $static_cb);
75             }
76             else {
77             $r->get($root.'(*file)' => {file => 'index.html' } => sub {
78 0     0     my $self = shift;
79 0           my $file = $self->param('file');
80 0           $self->req->url->path('/'.$file);
81 0           return $app->static->dispatch($self);
82 1         18 });
83             }
84             }
85              
86             1;
87              
88             __END__