RoutesAuthDBI/Log.pm untested

filter: Statementstmt 0.0% 0 / 28 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 81 CC 4 SCAR 24.0
CC
4
Cov
0%
log
20.0
new
2.0
BEGIN
2.0
CRAP 11.0
1 package Mojolicious::Plugin::RoutesAuthDBI::Log;
2 use Mojo::Base -base;#'Mojolicious::Plugin::Authentication'
3 use Mojolicious::Plugin::RoutesAuthDBI::Util qw(json_enc json_dec);
4 use Mojo::Util qw(decode url_unescape);
5  
6 #~ use constant PKG => __PACKAGE__;
7  
8 has [qw(app plugin)], undef, weak=>1;
9 has qw(model);
10  
11 has hook => sub {
12 my $self = shift;
13 $self->app->hook("after_dispatch" => sub {
14 $self->log(shift);
15 });
16
17 };
18  
19 sub new {
20 state $self = shift->SUPER::new(@_);
21 #~ $self->app->hook("before_dispatch" => sub {
22 #~ my $c = shift;
23 #~ $c->timing->begin(PKG);
24 #~ });
25 $self->hook();
26 }
27  
28 sub log {
29 my ($self, $c) = @_;
30 my $conf = $self->plugin->merge_conf;
31
32 return
33 if $conf->{log}{skip_static} && $c->stash->{'mojo.static'};
34
35 my $auth_helper = $conf->{auth}{current_user_fn};
36 my $u = $c->$auth_helper || ($self->plugin->guest && $self->plugin->guest->current($c))
37 or return;
38 my $route = $c->match->endpoint || {'non_static_url'=>$c->req->url->to_string};#$c->req->url->path->to_route or return;
39 my $route_id = $route->{'Mojolicious::Plugin::RoutesAuthDBI'} && $route->{'Mojolicious::Plugin::RoutesAuthDBI'}{route} && $route->{'Mojolicious::Plugin::RoutesAuthDBI'}{route}{id};
40 #~ my $elapsed = $c->timing->elapsed(PKG);
41 my $elapsed = $c->timing->elapsed('mojo.timer')
42 or return;# не будет для статики
43 #~ $c->app->log->debug(sprintf "%s elapsed:%s`s", ($route_id || decode('UTF-8', url_unescape($route->{non_static_url} || '/')), $elapsed)#join(', ', sort keys %$route_db)
44 $self->model->log(user_id=>$u->{id}, route_id=>$route_id, url=>$route_id ? undef : decode('UTF-8', url_unescape($route->{non_static_url} || '/')), status=>$c->res->code, elapsed=>$elapsed)
45 if $route_id || $route->{non_static_url};# без статики
46
47 }
48  
49  
50  
51 1;
52  
53 =pod
54  
55 =encoding utf8
56  
57 Доброго всем
58  
59  
60 =head1 Mojolicious::Plugin::RoutesAuthDBI::Log
61  
62 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
63  
64 =head1 NAME
65  
66 Mojolicious::Plugin::RoutesAuthDBI::Log - store log in DBI table.
67  
68 =head1 SYNOPSIS
69  
70 $app->plugin('RoutesAuthDBI',
71 ...
72 log => {< hashref options list below >},
73 ...
74 );
75  
76 =head1 OPTIONS
77  
78 =head2 namespace
79  
80 String, default to 'Mojolicious::Plugin::RoutesAuthDBI'.
81  
82 =head2 module
83  
84 String, default to 'Guest' (this module).
85  
86 =head2 disabled
87  
88 Boolean, disable logging.
89  
90 =head2 skip_static
91  
92 Boolean. True then log non-static routes only. Default false.
93  
94 =head2 tables
95  
96 Hashref, any DB tables names. See L<Mojolicious::Plugin::RoutesAuthDBI::Schema#Default-variables-for-SQL-templates>.
97  
98 =head2 table
99  
100 String, DB table B<logs> name. See L<Mojolicious::Plugin::RoutesAuthDBI::Schema#Default-variables-for-SQL-templates>.
101  
102  
103 =head1 METHODS
104  
105 =head2 new
106  
107 Apply logging into DBI table by "after_dispatch" hook
108  
109  
110 =head1 SEE ALSO
111  
112 L<Mojolicious::Plugin::RoutesAuthDBI>
113  
114 =head1 AUTHOR
115  
116 Михаил Че (Mikhail Che), C<< <mche [on] cpan.org> >>
117  
118 =head1 BUGS / CONTRIBUTING
119  
120 Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues>. Pull requests welcome also.
121  
122 =head1 COPYRIGHT
123  
124 Copyright 2018+ Mikhail Che.
125  
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself.
128  
129 =cut