line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Util::RandomString; |
2
|
2
|
|
|
2
|
|
2599
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
11
|
|
3
|
2
|
|
|
2
|
|
1351
|
use Session::Token; |
|
2
|
|
|
|
|
2009
|
|
|
2
|
|
|
|
|
1660
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our (%generator, %setting, %default, %param); |
8
|
|
|
|
|
|
|
our $read_config; |
9
|
|
|
|
|
|
|
our $ok = 1; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Register plugin |
12
|
|
|
|
|
|
|
sub register { |
13
|
4
|
|
|
4
|
1
|
983
|
my ($plugin, $mojo, $param) = @_; |
14
|
|
|
|
|
|
|
|
15
|
4
|
|
|
|
|
9
|
$ok--; |
16
|
|
|
|
|
|
|
|
17
|
4
|
50
|
|
|
|
14
|
if (ref $param ne 'HASH') { |
18
|
0
|
|
|
|
|
0
|
$mojo->log->fatal(__PACKAGE__ . ' expects a hash reference'); |
19
|
0
|
|
|
|
|
0
|
return; |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
4
|
50
|
|
|
|
13
|
if ($param) { |
23
|
4
|
|
|
|
|
33
|
$param{$_} = $param->{$_} foreach keys %$param; |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Load parameter from Config file |
28
|
4
|
100
|
|
|
|
15
|
unless ($read_config) { |
29
|
2
|
100
|
|
|
|
14
|
if (my $config_param = $mojo->config('Util-RandomString')) { |
30
|
1
|
|
|
|
|
14
|
%param = ( %$config_param, %param ); |
31
|
|
|
|
|
|
|
}; |
32
|
2
|
|
|
|
|
23
|
$read_config = 1; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Reseed on fork |
37
|
|
|
|
|
|
|
Mojo::IOLoop->timer( |
38
|
|
|
|
|
|
|
0 => sub { |
39
|
4
|
|
|
4
|
|
478
|
my %created = (); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Create generators by param |
42
|
4
|
|
|
|
|
14
|
foreach (keys %param) { |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Named generator |
45
|
20
|
100
|
100
|
|
|
74
|
if (ref $param{$_} && ref $param{$_} eq 'HASH') { |
46
|
|
|
|
|
|
|
|
47
|
14
|
50
|
|
|
|
32
|
next if $created{$_}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Construct object |
50
|
14
|
50
|
|
|
|
18
|
unless ($generator{$_} = Session::Token->new( |
51
|
14
|
|
|
|
|
48
|
%{ $param{$_} } |
52
|
|
|
|
|
|
|
)) { |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Unable to construct object |
55
|
0
|
|
|
|
|
0
|
$mojo->log->fatal(qq!Unable to create generator for "$_"!); |
56
|
0
|
|
|
|
|
0
|
next; |
57
|
|
|
|
|
|
|
}; |
58
|
14
|
|
|
|
|
2049
|
$setting{$_} = { %{$param{$_}} }; |
|
14
|
|
|
|
|
53
|
|
59
|
14
|
|
|
|
|
35
|
$created{$_} = 1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Default parameter |
63
|
|
|
|
|
|
|
else { |
64
|
6
|
|
|
|
|
12
|
$default{$_} = $param{$_}; |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Plugin registered |
69
|
4
|
|
|
|
|
8
|
$ok++; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Create default generator |
72
|
4
|
100
|
|
|
|
15
|
unless (exists $generator{default}) { |
73
|
2
|
|
|
|
|
8
|
$generator{default} = Session::Token->new( %default ); |
74
|
|
|
|
|
|
|
}; |
75
|
4
|
|
|
|
|
33
|
}); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Establish 'random_string' helper |
79
|
|
|
|
|
|
|
$mojo->helper( |
80
|
|
|
|
|
|
|
random_string => sub { |
81
|
218
|
|
|
218
|
|
313259
|
my $c = shift; |
82
|
218
|
|
|
|
|
359
|
my $gen = $_[0]; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# One tick for loop until the plugin is registered |
85
|
218
|
|
100
|
|
|
682
|
Mojo::IOLoop->one_tick until Mojo::IOLoop->is_running || $ok > 0; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Generate from generator |
88
|
218
|
100
|
|
|
|
3337
|
unless ($_[1]) { |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Generator doesn't exist |
91
|
206
|
100
|
100
|
|
|
991
|
if ($gen && !exists $generator{$gen}) { |
92
|
2
|
|
|
|
|
6
|
$c->app->log->warn(qq!RandomString generator "$gen" is unknown!); |
93
|
2
|
|
|
|
|
54
|
return ''; |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Get from generator |
97
|
204
|
|
100
|
|
|
1244
|
return $generator{$gen || 'default'}->get; |
98
|
|
|
|
|
|
|
}; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Overwrite default configuration |
101
|
12
|
100
|
|
|
|
37
|
return Session::Token->new(%default, @_)->get unless @_ % 2; |
102
|
|
|
|
|
|
|
|
103
|
10
|
|
|
|
|
16
|
$gen = shift; |
104
|
10
|
100
|
|
|
|
26
|
return Session::Token->new(%default, @_)->get if $gen eq 'default'; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Overwrite specific configuration |
107
|
8
|
100
|
|
|
|
22
|
if ($setting{ $gen }) { |
108
|
6
|
|
|
|
|
9
|
return Session::Token->new( %{ $setting{ $gen } } , @_)->get; |
|
6
|
|
|
|
|
36
|
|
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# Generator is unknown |
112
|
2
|
|
|
|
|
17
|
$c->app->log->warn(qq!RandomString generator "$gen" is unknown!); |
113
|
2
|
|
|
|
|
244
|
return ''; |
114
|
|
|
|
|
|
|
} |
115
|
4
|
100
|
|
|
|
368
|
) unless exists $mojo->renderer->helpers->{random_string}; |
116
|
|
|
|
|
|
|
}; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |