| 1 | package Mojo::Loader; | ||||||||||||||||||||||||||||
| 2 | 34 | ▶ | use Mojo::Base -strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use Mojo::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 34 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use Mojo::Util qw(b64_decode class_to_path deprecated); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 34 | our @EXPORT_OK = qw(data_section file_is_binary find_modules find_packages load_class load_classes); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 34 | my (%BIN, %CACHE); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 237 | ▶ | sub data_section { $_[0] ? $_[1] ? _all($_[0])->{$_[1]} : _all($_[0]) : undef } | ||||||||||||||||||||||||||
|
Subroutine
data_section: called CC=3 SCAR=11.0
Pod
documented
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | sub file_is_binary { keys %{_all($_[0])} ? !!$BIN{$_[0]}{$_[1]} : undef } | ||||||||||||||||||||||||||
|
Subroutine
file_is_binary: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 78 | ▶ | sub find_modules { | ||||||||||||||||||||||||||
|
Subroutine
find_modules: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 78 | ▶ | my ($ns, $options) = (shift, shift // {}); | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // {}
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 78 | my @ns = split /::/, $ns; | |||||||||||||||||||||||||||
| 21 | 78 | my @inc = grep { -d $$_ } map { path($_, @ns) } @INC; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 78 | my %modules; | |||||||||||||||||||||||||||
| 24 | 78 | for my $dir (@inc) { | |||||||||||||||||||||||||||
| 25 | 49 | ▶ | for my $file ($options->{recursive} ? $dir->list_tree->each : $dir->list->each) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 164 | ▶ | next unless $$file =~ s/\.pm$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 158 | $modules{join('::', $ns, @{$file->to_rel($$dir)})}++; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 78 | return sort keys %modules; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 14 | ▶ | sub find_packages { | ||||||||||||||||||||||||||
|
Subroutine
find_packages: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 14 | my $ns = shift; | |||||||||||||||||||||||||||
| 36 | 34 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 14 | ▶ | return sort map { /^(.+)::$/ ? "${ns}::$1" : () } keys %{"${ns}::"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1019 | ▶ | sub load_class { | ||||||||||||||||||||||||||
|
Subroutine
load_class: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 1019 | my $class = shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Invalid class name | ||||||||||||||||||||||||||||
| 44 | 1019 | ▶ | return 1 if ($class || '') !~ /^\w(?:[\w:']*\w)?$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$class || ''
MC/DC0%0/1
$class || ''
$class
Truth table50%1/2
A
A= $class
| |||||||||||||||||||||||||||||
| 45 | 1012 | ▶ | deprecated | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | q{Calling Mojo::Loader::load_class with a class name using the old package separator "'" is deprecated; use "::"} | ||||||||||||||||||||||||||||
| 47 | if $class =~ m/'/; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Load if not already loaded | ||||||||||||||||||||||||||||
| 50 | 1012 | ▶ | return undef if $class->can('new') || eval "require $class; 1"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$class->can("new") or eval "require $class; 1"
MC/DC100%2/2
$class->can("new") or eval "require $class; 1"
$class->can("new")
eval "require $class; 1"
Truth table100%3/3
A or B
A= $class->can("new")B= eval "require $class; 1"
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Does not exist | ||||||||||||||||||||||||||||
| 53 | 239 | ▶ | return 1 if $@ =~ /^Can't locate \Q@{[class_to_path $class]}\E in \@INC/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Real error | ||||||||||||||||||||||||||||
| 56 | 11 | return Mojo::Exception->new($@)->inspect; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 61 | ▶ | sub load_classes { | ||||||||||||||||||||||||||
|
Subroutine
load_classes: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 61 | my $ns = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 61 | my @classes; | |||||||||||||||||||||||||||
| 63 | 61 | for my $module (find_modules($ns, {recursive => 1})) { | |||||||||||||||||||||||||||
| 64 | 26 | ▶ | push @classes, $module unless my $e = load_class($module); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 26 | ▶ | die $e if ref $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 60 | return @classes; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 175 | ▶ | sub _all { | ||||||||||||||||||||||||||
|
Subroutine
_all: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 72 | 175 | my $class = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 175 | ▶ | return $CACHE{$class} if $CACHE{$class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 70 | local $.; | |||||||||||||||||||||||||||
| 76 | 34 | ▶ | my $handle = do { no strict 'refs'; \*{"${class}::DATA"} }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 70 | ▶ | return {} unless fileno $handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 19 | seek $handle, 0, 0; | |||||||||||||||||||||||||||
| 79 | 19 | my $data = join '', <$handle>; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Ignore everything before __DATA__ (some versions seek to start of file) | ||||||||||||||||||||||||||||
| 82 | 19 | $data =~ s/^.*\n__DATA__\r?\n/\n/s; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Ignore everything after __END__ | ||||||||||||||||||||||||||||
| 85 | 19 | $data =~ s/\n__END__\r?\n.*$/\n/s; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Split files | ||||||||||||||||||||||||||||
| 88 | 19 | (undef, my @files) = split /^@@\s*(.+?)\s*\r?\n/m, $data; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Find data | ||||||||||||||||||||||||||||
| 91 | 19 | my $all = $CACHE{$class} = {}; | |||||||||||||||||||||||||||
| 92 | 19 | ▶ | while (@files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 46 | my ($name, $data) = splice @files, 0, 2; | |||||||||||||||||||||||||||
| 94 | 46 | ▶ | $all->{$name} = $name =~ s/\s*\(\s*base64\s*\)$// && ++$BIN{$class}{$name} ? b64_decode $data : $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name =~ s/\s*\(\s*base64\s*\)$//u and ++$BIN{$class}{$name}
MC/DC50%1/2
$name =~ s/\s*\(\s*base64\s*\)$//u and ++$BIN{$class}{$name}
$name =~ s/\s*\(\s*base64\s*\)$//u
++$BIN{$class}{$name}
Truth table66%2/3
A and B
A= $name =~ s/\s*\(\s*base64\s*\)$//uB= ++$BIN{$class}{$name}
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 19 | return $all; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 34 | 1; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =encoding utf8 | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 NAME | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Mojo::Loader - Load all kinds of things | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | use Mojo::Loader qw(data_section find_modules load_class); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Find modules in a namespace | ||||||||||||||||||||||||||||
| 113 | for my $module (find_modules 'Some::Namespace') { | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Load them safely | ||||||||||||||||||||||||||||
| 116 | my $e = load_class $module; | ||||||||||||||||||||||||||||
| 117 | warn qq{Loading "$module" failed: $e} and next if ref $e; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # And extract files from the DATA section | ||||||||||||||||||||||||||||
| 120 | say data_section($module, 'some_file.txt'); | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<Mojo::Loader> is a class loader and plugin framework. Aside from finding modules and loading classes, it allows | ||||||||||||||||||||||||||||
| 126 | multiple files to be stored in the C<DATA> section of a class, which can then be accessed individually. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | package Foo; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1; | ||||||||||||||||||||||||||||
| 131 | __DATA__ | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | @@ test.txt | ||||||||||||||||||||||||||||
| 134 | This is the first file. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | @@ test2.html (base64) | ||||||||||||||||||||||||||||
| 137 | VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUu | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | @@ test | ||||||||||||||||||||||||||||
| 140 | This is the | ||||||||||||||||||||||||||||
| 141 | third file. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Each file has a header starting with C<@@>, followed by the file name and optional instructions for decoding its | ||||||||||||||||||||||||||||
| 144 | content. Currently only the Base64 encoding is supported, which can be quite convenient for the storage of binary data. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | L<Mojo::Loader> implements the following functions, which can be imported individually. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 data_section | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my $all = data_section 'Foo::Bar'; | ||||||||||||||||||||||||||||
| 153 | my $index = data_section 'Foo::Bar', 'index.html'; | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Extract embedded file from the C<DATA> section of a class, all files will be cached once they have been accessed for | ||||||||||||||||||||||||||||
| 156 | the first time. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # List embedded files | ||||||||||||||||||||||||||||
| 159 | say for keys %{data_section 'Foo::Bar'}; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 file_is_binary | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | my $bool = file_is_binary 'Foo::Bar', 'test.png'; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Check if embedded file from the C<DATA> section of a class was Base64 encoded. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 find_packages | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | my @pkgs = find_packages 'MyApp::Namespace'; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Search for packages in a namespace non-recursively. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 find_modules | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | my @modules = find_modules 'MyApp::Namespace'; | ||||||||||||||||||||||||||||
| 176 | my @modules = find_modules 'MyApp::Namespace', {recursive => 1}; | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Search for modules in a namespace. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | These options are currently available: | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =over 2 | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item recursive | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | recursive => 1 | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Search namespace recursively. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =back | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 load_class | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | my $e = load_class 'Foo::Bar'; | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Load a class and catch exceptions, returns a false value if loading was successful, a true value if the class was not | ||||||||||||||||||||||||||||
| 197 | found, or a L<Mojo::Exception> object if loading failed. Note that classes are checked for a C<new> method to see if | ||||||||||||||||||||||||||||
| 198 | they are already loaded, so trying to load the same class multiple times may yield different results. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # Handle exceptions | ||||||||||||||||||||||||||||
| 201 | if (my $e = load_class 'Foo::Bar') { | ||||||||||||||||||||||||||||
| 202 | die ref $e ? "Exception: $e" : 'Not found!'; | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 load_classes | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | my @classes = load_classes 'Foo::Bar'; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Load all classes in a namespace recursively. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||