Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

まぜてもよいように

  • Loading branch information...
commit 2c45ee98caf3701d483e5f70afb388ac891a51c7 1 parent 29f9536
@nekokak authored
Showing with 19 additions and 6 deletions.
  1. +19 −6 lib/Kamui/Web/Context.pm
View
25 lib/Kamui/Web/Context.pm
@@ -9,12 +9,25 @@ sub _plugin_name {
return $pkg;
}
-my @initialize_plugins;
-my @finalize_plugins;
sub load_plugins {
my ($class, $plugins) = @_;
$class = ref $class || $class;
+ unless ($class->can('_plugins')) {
+ my $_plugins = +{
+ initialize => [],
+ finalize => [],
+ };
+ my $code = sub {
+ my ($class, $key, $method) = @_;
+ push @{$_plugins->{$key}}, $method if $method;
+ $_plugins->{$key};
+ };
+
+ no strict 'refs';
+ *{"$class\::_plugins"} = $code;
+ }
+
for my $plugin (@{$plugins}) {
my $pkg = _plugin_name($plugin);
$class->load_class($pkg);
@@ -23,10 +36,10 @@ sub load_plugins {
while (my($method, $initialize_code) = each %{ $register_methods }) {
if ($pkg->do_initialize) {
- push @initialize_plugins, $method;
+ $class->_plugins('initialize', $method);
}
if ($pkg->do_finalize) {
- push @finalize_plugins, $method;
+ $class->_plugins('finalize', $method);
}
my $code = sub {
my $context = shift;
@@ -190,7 +203,7 @@ sub initialize {
sub initialize_plugins {
my $self = shift;
- for my $plugin (@initialize_plugins) {
+ for my $plugin (@{$self->_plugins('initialize')}) {
$self->$plugin->initialize;
}
}
@@ -205,7 +218,7 @@ sub finalize {
sub finalize_plugins {
my ($self, $response) = @_;
- for my $plugin (@finalize_plugins) {
+ for my $plugin (@{$self->_plugins('finalize')}) {
$self->$plugin->finalize($response);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.