Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'topic/plugins_compat'

  • Loading branch information...
commit cf69e5cf867c60e7035480893292f4ce5a1352f2 2 parents 425b8ee + 02a694b
Alexis Sukrieh authored
View
2  contrib/lib/Dancer/Plugin/Bar.pm
@@ -17,5 +17,5 @@ register wrap_request => sub {
# to_yaml(plugin_settings);
#};
-register_plugin;
+register_plugin for_versions => [ 2 ];
1;
View
2  contrib/lib/Dancer/Plugin/Foo.pm
@@ -13,5 +13,5 @@ register foo => sub {
# to_yaml(plugin_settings);
#};
-register_plugin;
+register_plugin for_versions => [ 2 ];
1;
View
3  lib/Dancer.pm
@@ -10,10 +10,9 @@ use Dancer::Core::App;
use Dancer::Core::DSL;
use Dancer::FileUtils;
-our $VERSION = '1.9999_01';
+our $VERSION = '2.000000_01';
our $AUTHORITY = 'SUKRIA';
-
#
# private
#
View
6 lib/Dancer/Core/DSL.pm
@@ -9,6 +9,10 @@ use Carp;
with 'Dancer::Core::Role::DSL';
sub dsl_keywords {
+
+ # the flag means : 1 = is global, 0 = is not global. global means can be
+ # called from anywhere. not global means must be called rom within a route
+ # handler
[
[any => 1],
[app => 1],
@@ -21,6 +25,7 @@ sub dsl_keywords {
[core_debug => 1],
[dance => 1],
[dancer_app => 1],
+ [dancer_version => 1],
[debug => 1],
[del => 1],
[dirname => 1],
@@ -79,6 +84,7 @@ sub dsl_keywords_as_list {
}
sub dancer_app { shift->app }
+sub dancer_version { (split /\./, $Dancer::VERSION)[0] }
sub dsl { shift }
sub debug { shift->log(debug => @_) }
View
35 lib/Dancer/Plugin.pm
@@ -94,12 +94,38 @@ sub register {
];
}
+=method register_plugin
+
+A Dancer plugin must end with this statement. This lets the plugin register all
+the symbols define with C<register> as exported symbols (via the L<Exporter>
+module).
+
+A Dancer plugin inherits from Dancer::Plugin and Exporter transparently.
+
+=cut
+
sub register_plugin {
my $plugin = caller;
my $caller = caller(1);
+ my %params = @_;
+
+ # For backward compatibility, no params means "supports only Dancer 1"
+ defined $params{for_versions}
+ or $params{for_versions} = [ 1 ];
+
+ my $supported_versions = $params{for_versions} || [ 1 ];
+ ref $supported_versions eq 'ARRAY'
+ or croak "register_plugin must be called with an array ref";
# if the caller has not a dsl, we cant register the plugin
return if ! $caller->can('dsl');
+ my $dancer_version = $caller->dsl->dancer_version;
+ my $plugin_version = eval "\$${plugin}::VERSION" || '??';
+
+ # make sure the plugin is compatible with this version of Dancer
+ grep /^$dancer_version$/, @{ $supported_versions }
+ or croak "$plugin $plugin_version does not support Dancer $dancer_version.";
+
# we have a $dsl in our caller, we can register our symbols then
my $dsl = $caller->dsl;
@@ -222,6 +248,15 @@ for B<Dancer::Plugin::Foo::Bar>, use:
"Foo::Bar":
key: value
+=item B<major_version>
+
+ my $plugin_system_version = Dancer::Plugin->major_version
+
+Returns the Dancer plugin system major version. Useful for a plugin to know if
+it's being loaded in a Dancer 1 or Dancer 2 plugin system.
+
+Returns always 2 ( because that's Dancer 2, eh ! )
+
=back
=head1 AUTHORS
View
2  lib/Dancer/Plugin/Ajax.pm
@@ -34,7 +34,7 @@ register 'ajax' => sub {
$dsl->any( ['get', 'post'] => $pattern, $ajax_route );
};
-register_plugin;
+register_plugin for_versions => [ 2 ];
1;
__END__
View
1  t/dsl.t
@@ -19,4 +19,5 @@ any ['get', 'post'], '/' => sub {
is $r->[2][0], 'POST';
}
+is dancer_version, 2, 'dancer_version';
done_testing;
View
2  t/lib/Dancer1Plugin.pm
@@ -10,7 +10,7 @@ register around_get => sub {
};
};
-register_plugin;
+register_plugin for_versions => [ 2 ];
1;
View
2  t/lib/FooPlugin.pm
@@ -51,5 +51,5 @@ sub _retreive_get_urls {
};
-register_plugin;
+register_plugin for_versions => [ 2 ];
1;
View
12 t/plugin_register.t
@@ -44,4 +44,16 @@ subtest 'plugin reserved keywords' => sub {
}
};
+subtest 'plugin version' => sub {
+
+ package Foo;
+ our $VERSION = '1.034';
+ use Dancer;
+ use Dancer::Plugin;
+
+ eval { register_plugin };
+ Test::More::like $@, qr{Foo 1.034 does not support Dancer 2};
+};
+
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.