line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::BModel; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
2356
|
use 5.010; |
|
3
|
|
|
|
|
10
|
|
4
|
3
|
|
|
3
|
|
14
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
63
|
|
5
|
3
|
|
|
3
|
|
22
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
102
|
|
6
|
3
|
|
|
3
|
|
15
|
use Carp qw/ croak /; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
163
|
|
7
|
3
|
|
|
3
|
|
12
|
use File::Find qw/ find /; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
154
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
2049
|
use Mojo::Loader; |
|
3
|
|
|
|
|
133212
|
|
|
3
|
|
|
|
|
139
|
|
10
|
3
|
|
|
3
|
|
25
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
20
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $CREATE_DIR = 1; |
15
|
|
|
|
|
|
|
my $MODEL_DIR = 'Model'; # directory of poject for the Model-modules |
16
|
|
|
|
|
|
|
my %MODULES = (); |
17
|
|
|
|
|
|
|
my $BASE_MODEL = 'Mojolicious::BModel::Base'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub register { |
20
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $app, $conf ) = @_; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
0
|
my $app_name = ref $app; # name of calling app |
23
|
0
|
|
|
|
|
0
|
my $path_to_model = $app->home->lib_dir . '/' . $app_name . '/' . $MODEL_DIR; |
24
|
0
|
|
|
|
|
0
|
my $dir_exists = $self->check_model_dir( $path_to_model ); |
25
|
0
|
|
0
|
|
|
0
|
my $create_dir = $conf->{create_dir} || $CREATE_DIR; |
26
|
|
|
|
|
|
|
|
27
|
0
|
0
|
0
|
|
|
0
|
if ( ! $dir_exists && ! $create_dir ) { |
|
|
0
|
0
|
|
|
|
|
28
|
0
|
|
|
|
|
0
|
warn "Directory $app_name/$MODEL_DIR does not exist"; |
29
|
0
|
|
|
|
|
0
|
return 1; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
elsif ( ! $dir_exists && $create_dir ) { |
32
|
0
|
0
|
|
|
|
0
|
mkdir $path_to_model or croak "Could not create directory $path_to_model : $!"; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
0
|
$self->load_models( $path_to_model, $app_name, $app ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$app->helper( |
38
|
|
|
|
|
|
|
model => sub { |
39
|
0
|
|
|
0
|
|
0
|
my ( $self, $model_name ) = @_; |
40
|
0
|
|
|
|
|
0
|
$model_name =~ s/\/+/::/g; |
41
|
0
|
0
|
|
|
|
0
|
croak "Unknown model $model_name" unless $MODULES{ $model_name }; |
42
|
0
|
|
|
|
|
0
|
return $MODULES{ $model_name }; |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
0
|
); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
return 1; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub check_model_dir { |
50
|
3
|
|
|
3
|
0
|
53270
|
my ( $self, $path_to_model ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
3
|
100
|
100
|
|
|
147
|
return 1 if -e $path_to_model && -d $path_to_model; |
53
|
2
|
|
|
|
|
47
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub find_models { |
57
|
1
|
|
|
1
|
0
|
847
|
my ( $self, $path_to_model, $model_path ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
3
|
my @model_dirs = ( $model_path ); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# find all subdirs in the directory of model |
62
|
|
|
|
|
|
|
find( |
63
|
|
|
|
|
|
|
sub { |
64
|
4
|
100
|
66
|
4
|
|
230
|
return if ! -d $File::Find::name || $File::Find::name eq $path_to_model; |
65
|
3
|
|
|
|
|
5
|
my $dir_name = $File::Find::name; |
66
|
3
|
|
|
|
|
42
|
$dir_name =~ s/$path_to_model\/?(.+)/$1/; |
67
|
3
|
|
|
|
|
6
|
$dir_name =~ s/(\/)+/::/g; |
68
|
3
|
|
|
|
|
167
|
push @model_dirs, $model_path . '::' . $dir_name; |
69
|
|
|
|
|
|
|
}, |
70
|
1
|
|
|
|
|
101
|
( $path_to_model ) |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
6
|
return \@model_dirs; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# recursive search and download modules with models |
77
|
|
|
|
|
|
|
sub load_models { |
78
|
0
|
|
|
0
|
0
|
|
my ( $self, $path_to_model, $app_name, $app ) = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $model_path = "$app_name\::$MODEL_DIR"; |
81
|
0
|
|
|
|
|
|
my @model_dirs = @{ $self->find_models( $path_to_model, $model_path ) }; |
|
0
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my $base_load_err = Mojo::Loader::load_class( $BASE_MODEL ); |
84
|
0
|
0
|
|
|
|
|
croak "Loading base model $BASE_MODEL failed: $base_load_err" if ref $base_load_err; |
85
|
|
|
|
|
|
|
{ |
86
|
3
|
|
|
3
|
|
5210
|
no strict 'refs'; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
696
|
|
|
0
|
|
|
|
|
|
|
87
|
0
|
|
|
0
|
|
|
*{ "$BASE_MODEL\::app" } = sub { $app }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# load modules from every dirs and subdirs of model |
91
|
0
|
|
|
|
|
|
for my $dir ( @model_dirs ) { |
92
|
0
|
|
|
|
|
|
my @model_packages = Mojo::Loader::find_modules( $dir ); |
93
|
0
|
|
|
|
|
|
for my $pm ( @model_packages ) { |
94
|
0
|
|
|
|
|
|
my $load_err = Mojo::Loader::load_class( $pm ); |
95
|
0
|
0
|
|
|
|
|
croak "Loading '$pm' failed: $load_err" if ref $load_err; |
96
|
0
|
|
|
|
|
|
my ( $basename ) = $pm =~ /$model_path\::(.*)/; |
97
|
0
|
|
|
|
|
|
$MODULES{ $basename } = $pm->new; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
return 1; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
__END__ |