Skip to content

Commit

Permalink
deprecate object-oriented Mojo::Loader API
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 20, 2015
1 parent 1cf9ae1 commit 06843f7
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 116 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@

5.81 2015-02-20
- Deprecated object-oriented Mojo::Loader API.
- Added data_file, fine_is_binary, load_class and find_modules functions to

This comment has been minimized.

Copy link
@avkhozov

avkhozov Feb 20, 2015

Contributor

typo in fine_is_binary

Mojo::Loader.

5.80 2015-02-18
- Deprecated Mojo::IOLoop::accept_interval, Mojo::IOLoop::lock and
Expand Down
106 changes: 63 additions & 43 deletions lib/Mojo/Loader.pm
@@ -1,35 +1,28 @@
package Mojo::Loader;
use Mojo::Base -base;
use Mojo::Base -strict;

use Exporter 'import';
use File::Basename 'fileparse';
use File::Spec::Functions qw(catdir catfile splitdir);
use Mojo::Exception;
use Mojo::Util qw(b64_decode class_to_path);
use Mojo::Util qw(b64_decode class_to_path deprecated);

my (%BIN, %CACHE);

sub data { $_[1] ? $_[2] ? _all($_[1])->{$_[2]} : _all($_[1]) : undef }
our @EXPORT_OK = qw(data_section file_is_binary find_modules load_class);

sub is_binary { keys %{_all($_[1])} ? !!$BIN{$_[1]}{$_[2]} : undef }

sub load {
my ($self, $module) = @_;
my (%BIN, %CACHE);

# Check module name
return 1 if !$module || $module !~ /^\w(?:[\w:']*\w)?$/;
# DEPRECATED in Tiger Face!
sub data { shift and data_section(@_) }

# Load
return undef if $module->can('new') || eval "require $module; 1";
sub data_section { $_[0] ? $_[1] ? _all($_[0])->{$_[1]} : _all($_[0]) : undef }

# Exists
return 1 if $@ =~ /^Can't locate \Q@{[class_to_path $module]}\E in \@INC/;
# DEPRECATED in Tiger Face!
sub is_binary { shift and file_is_binary(@_) }

# Real error
return Mojo::Exception->new($@);
}
sub file_is_binary { keys %{_all($_[0])} ? !!$BIN{$_[0]}{$_[1]} : undef }

sub search {
my ($self, $ns) = @_;
sub find_modules {
my $ns = shift;

my %modules;
for my $directory (@INC) {
Expand All @@ -46,6 +39,34 @@ sub search {
return [keys %modules];
}

# DEPRECATED in Tiger Face!
sub load { shift and load_class(@_) }

sub load_class {
my $class = shift;

# Check class name
return 1 if !$class || $class !~ /^\w(?:[\w:']*\w)?$/;

# Load
return undef if $class->can('new') || eval "require $class; 1";

# Exists
return 1 if $@ =~ /^Can't locate \Q@{[class_to_path $class]}\E in \@INC/;

# Real error
return Mojo::Exception->new($@);
}

# DEPRECATED in Tiger Face!
sub new {
deprecated 'Object-oriented Mojo::Loader API is DEPRECATED';
Mojo::Base::new(@_);
}

# DEPRECATED in Tiger Face!
sub search { shift and find_modules(@_) }

sub _all {
my $class = shift;

Expand Down Expand Up @@ -85,62 +106,61 @@ Mojo::Loader - Loader
=head1 SYNOPSIS
use Mojo::Loader;
use Mojo::Loader qw(data_section find_modules load_class);
# Find modules in a namespace
my $loader = Mojo::Loader->new;
for my $module (@{$loader->search('Some::Namespace')}) {
for my $module (@{find_modules 'Some::Namespace'}) {
# Load them safely
my $e = $loader->load($module);
my $e = load_class $module;
warn qq{Loading "$module" failed: $e} and next if ref $e;
# And extract files from the DATA section
say $loader->data($module, 'some_file.txt');
say data_section($module, 'some_file.txt');
}
=head1 DESCRIPTION
L<Mojo::Loader> is a class loader and plugin framework.
=head1 METHODS
=head1 FUNCTIONS
L<Mojo::Loader> inherits all methods from L<Mojo::Base> and implements the
following new ones.
L<Mojo::Loader> implements the following functions, which can be imported
individually.
=head2 data
=head2 data_section
my $all = $loader->data('Foo::Bar');
my $index = $loader->data('Foo::Bar', 'index.html');
my $all = data_section 'Foo::Bar';
my $index = data_section 'Foo::Bar', 'index.html';
Extract embedded file from the C<DATA> section of a class, all files will be
cached once they have been accessed for the first time.
say for keys %{$loader->data('Foo::Bar')};
say for keys %{data_section 'Foo::Bar'};
=head2 is_binary
=head2 file_is_binary
my $bool = $loader->is_binary('Foo::Bar', 'test.png');
my $bool = file_is_binary 'Foo::Bar', 'test.png';
Check if embedded file from the C<DATA> section of a class was Base64 encoded.
=head2 load
=head2 find_modules
my $e = $loader->load('Foo::Bar');
my $modules = find_modules 'MyApp::Namespace';
Search for modules in a namespace non-recursively.
=head2 load_class
my $e = load_class 'Foo::Bar';
Load a class and catch exceptions. Note that classes are checked for a C<new>
method to see if they are already loaded.
if (my $e = $loader->load('Foo::Bar')) {
if (my $e = load_class 'Foo::Bar') {
die ref $e ? "Exception: $e" : 'Not found!';
}
=head2 search
my $modules = $loader->search('MyApp::Namespace');
Search for modules in a namespace non-recursively.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Reactor.pm
Expand Up @@ -3,13 +3,13 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use IO::Poll qw(POLLIN POLLPRI);
use Mojo::Loader;
use Mojo::Loader 'load_class';

sub again { croak 'Method "again" not implemented by subclass' }

sub detect {
my $try = $ENV{MOJO_REACTOR} || 'Mojo::Reactor::EV';
return Mojo::Loader->new->load($try) ? 'Mojo::Reactor::Poll' : $try;
return load_class($try) ? 'Mojo::Reactor::Poll' : $try;
}

sub io { croak 'Method "io" not implemented by subclass' }
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Server.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Cwd 'abs_path';
use Mojo::Loader;
use Mojo::Loader 'load_class';
use Mojo::Util 'md5_sum';
use POSIX;
use Scalar::Util 'blessed';
Expand All @@ -15,7 +15,7 @@ has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };
sub build_app {
my ($self, $app) = @_;
local $ENV{MOJO_EXE};
return $app->new unless my $e = Mojo::Loader->new->load($app);
return $app->new unless my $e = load_class $app;
die ref $e ? $e : qq{Can't find application class "$app" in \@INC. (@INC)\n};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command.pm
Expand Up @@ -6,7 +6,7 @@ use Cwd 'getcwd';
use File::Basename 'dirname';
use File::Path 'mkpath';
use File::Spec::Functions qw(catdir catfile);
use Mojo::Loader;
use Mojo::Loader 'data_section';
use Mojo::Server;
use Mojo::Template;
use Mojo::Util qw(spurt unindent);
Expand Down Expand Up @@ -59,7 +59,7 @@ sub rel_file { catfile getcwd(), split('/', pop) }
sub render_data {
my ($self, $name) = (shift, shift);
Mojo::Template->new->name("template $name from DATA section")
->render(Mojo::Loader->new->data(ref $self, $name), @_);
->render(data_section(ref $self, $name), @_);
}

sub render_to_file {
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojolicious/Command/inflate.pm
@@ -1,7 +1,7 @@
package Mojolicious::Command::inflate;
use Mojo::Base 'Mojolicious::Command';

use Mojo::Loader;
use Mojo::Loader qw(data_section file_is_binary);
use Mojo::Util 'encode';

has description => 'Inflate embedded files to real files';
Expand All @@ -12,12 +12,11 @@ sub run {

# Find all embedded files
my %all;
my $app = $self->app;
my $loader = Mojo::Loader->new;
my $app = $self->app;
for my $class (@{$app->renderer->classes}, @{$app->static->classes}) {
for my $name (keys %{$loader->data($class)}) {
my $data = $loader->data($class, $name);
$data = encode 'UTF-8', $data unless $loader->is_binary($class, $name);
for my $name (keys %{data_section $class}) {
my $data = data_section $class, $name;
$data = encode 'UTF-8', $data unless file_is_binary $class, $name;
$all{$name} = $data;
}
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Commands;
use Mojo::Base 'Mojolicious::Command';

use Getopt::Long 'GetOptionsFromArray';
use Mojo::Loader;
use Mojo::Loader qw(find_modules load_class);
use Mojo::Server;
use Mojo::Util 'tablify';

Expand Down Expand Up @@ -60,10 +60,9 @@ sub run {

# Find all available commands
my %all;
my $loader = Mojo::Loader->new;
for my $ns (@{$self->namespaces}) {
$all{substr $_, length "${ns}::"} //= $_->new->description
for grep { _command($_) } @{$loader->search($ns)};
for grep { _command($_) } @{find_modules $ns};
}

my @rows = map { [" $_", $all{$_}] } sort keys %all;
Expand Down Expand Up @@ -91,7 +90,7 @@ BEGIN { _args([@ARGV]) }
sub _command {
my ($module, $fatal) = @_;
return $module->isa('Mojolicious::Command') ? $module : undef
unless my $e = Mojo::Loader->new->load($module);
unless my $e = load_class $module;
$fatal && ref $e ? die $e : return undef;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides.pod
Expand Up @@ -305,8 +305,6 @@ This is the class hierarchy of the L<Mojolicious> distribution.

=item * L<Mojo::JSON::Pointer>

=item * L<Mojo::Loader>

=item * L<Mojo::Parameters>

=item * L<Mojo::Path>
Expand Down Expand Up @@ -441,6 +439,8 @@ This is the class hierarchy of the L<Mojolicious> distribution.

=item * L<Mojo::JSON>

=item * L<Mojo::Loader>

=item * L<Mojo::Util>

=item * L<ojo>
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Plugins.pm
@@ -1,7 +1,7 @@
package Mojolicious::Plugins;
use Mojo::Base 'Mojo::EventEmitter';

use Mojo::Loader;
use Mojo::Loader 'load_class';
use Mojo::Util 'camelize';

has namespaces => sub { ['Mojolicious::Plugin'] };
Expand Down Expand Up @@ -48,8 +48,7 @@ sub register_plugin {

sub _load {
my $module = shift;
return $module->isa('Mojolicious::Plugin')
unless my $e = Mojo::Loader->new->load($module);
return $module->isa('Mojolicious::Plugin') unless my $e = load_class $module;
ref $e ? die $e : return undef;
}

Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -5,7 +5,7 @@ use File::Spec::Functions 'catfile';
use Mojo::Cache;
use Mojo::JSON 'encode_json';
use Mojo::Home;
use Mojo::Loader;
use Mojo::Loader 'data_section';
use Mojo::Util qw(decamelize encode md5_sum monkey_patch slurp);

has cache => sub { Mojo::Cache->new };
Expand All @@ -29,8 +29,6 @@ $HOME->parse(
$HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
my %TEMPLATES = map { $_ => slurp $HOME->rel_file($_) } @{$HOME->list_files};

my $LOADER = Mojo::Loader->new;

sub DESTROY { Mojo::Util::_teardown($_) for @{shift->{namespaces}} }

sub accepts {
Expand Down Expand Up @@ -59,7 +57,7 @@ sub get_data_template {

# Find template
return undef unless my $template = $self->template_name($options);
return $LOADER->data($self->{index}{$template}, $template);
return data_section $self->{index}{$template}, $template;
}

sub get_helper {
Expand Down Expand Up @@ -248,7 +246,7 @@ sub _warmup {

# Handlers and classes for DATA templates
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for my @keys = sort keys %{$LOADER->data($class)};
$index->{$_} = $class for my @keys = sort keys %{data_section $class};
s/\.(\w+)$// and unshift @{$templates->{$_}}, $1 for reverse @keys;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Routes::Route';

use List::Util 'first';
use Mojo::Cache;
use Mojo::Loader;
use Mojo::Loader 'load_class';
use Mojo::Util 'camelize';
use Mojolicious::Routes::Match;
use Scalar::Util 'weaken';
Expand Down Expand Up @@ -202,7 +202,7 @@ sub _load {

# Load unless already loaded
return 1 if $self->{loaded}{$app};
if (my $e = Mojo::Loader->new->load($app)) { ref $e ? die $e : return undef }
if (my $e = load_class $app) { ref $e ? die $e : return undef }

# Check base classes
return 0 unless first { $app->isa($_) } @{$self->base_classes};
Expand Down

0 comments on commit 06843f7

Please sign in to comment.