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