Skip to content

Commit

Permalink
Add new module WellWell::Plugin for plugin handling.
Browse files Browse the repository at this point in the history
Currently supports plugin scan and activation.
  • Loading branch information
racke committed Oct 14, 2010
1 parent 3977cbc commit 0e11392
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
5 changes: 4 additions & 1 deletion catalog.cfg
Expand Up @@ -27,7 +27,10 @@ ImageDir __IMAGE_URL__/
include code/*.*

# Functions to run after completion of the configuration
StartupHooks prepare_database
StartupHooks <<EOH
prepare_database
plugin_scan
EOH

# Profiles
OrderProfile profiles/*.profile
Expand Down
24 changes: 24 additions & 0 deletions lib/WellWell/Core.pm
Expand Up @@ -26,13 +26,15 @@ use Vend::Config;

use WellWell::Cart;
use WellWell::Data;
use WellWell::Plugin qw/plugin_scan plugin_enable/;

# setup configuration directives
Vend::Config::parse_directive('Hook', 'Hook hook');
Vend::Config::parse_directive('StartupHooks', 'StartupHooks startup_hooks');

# predefined startup hooks
Vend::Config::parse_subroutine('GlobalSub', 'prepare_database WellWell::Data::prepare_database');
Vend::Config::parse_subroutine('GlobalSub', 'plugin_scan WellWell::Core::plugin_scan_sub');

# all what we want is to transfer CGI values from CGI to the Values
# space, and nothing else
Expand All @@ -47,6 +49,28 @@ sub plugins {
return @plugins;
}

# called at startup to scan plugins
sub plugin_scan_sub {
my ($dbif, $plref);

$dbif = WellWell::Data::easy_handle();
$plref = WellWell::Plugin::plugin_scan($dbif, "$Vend::Cfg->{VendRoot}/plugins",
"$Vend::Cfg->{VendRoot}/local/plugins");

for my $plugin (plugins()) {
if (exists $plref->{$plugin}) {
if ($plref->{$plugin}->{status} eq 0) {
Vend::Config::config_error("Plugin $plugin used in PLUGIN variable is explicitly disabled in plugins table.");
return;
}
elsif (! defined($plref->{$plugin}->{status})) {
Vend::Config::config_warn("Enabling plugin $plugin in plugins table.");
plugin_enable($dbif, $plugin);
}
}
}
}

sub hooks {
my ($function, $name, @args) = @_;

Expand Down
12 changes: 12 additions & 0 deletions lib/WellWell/Data.pm
Expand Up @@ -23,6 +23,7 @@ use strict;
use warnings;

use Vend::Data;
use DBIx::Easy;

sub prepare_database {
my ($userdb_ref, $users_table, %dbif);
Expand Down Expand Up @@ -71,4 +72,15 @@ sub prepare_database {
return;
}

sub easy_handle {
my ($dbh, $dbif);

$dbh = database_exists_ref('products')->dbh();

$dbif = new DBIx::Easy ($dbh->{Driver}->{Name}, $dbh->{Name});
$dbif->{CONN} = $dbh->clone;

return $dbif;
}

1;
112 changes: 112 additions & 0 deletions lib/WellWell/Plugin.pm
@@ -0,0 +1,112 @@
# WellWell::Plugin - WellWell plugin routines
#
# Copyright (C) 2010 Stefan Hornburg (Racke) <racke@linuxia.de>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

package WellWell::Plugin;

use vars qw/@ISA @EXPORT_OK/;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw/plugin_scan plugin_enable/;

sub plugin_scan {
my ($dbif, @dirs) = @_;
my (@plugins, $dirname, $infofile, $plugininfo, $dbref);
my ($plugin, $pluginrec);
my ($sth, $href);
my (%plugins);

# read current plugins from database
$sth = $dbif->process('select * from plugins');

while ($href = $sth->fetchrow_hashref()) {
$plugins{$href->{name}} = $href;
}

for my $dir (@dirs) {
opendir(PLUGINS, $dir);
while ($dirname = readdir(PLUGINS)) {
next unless -d "plugins/$dirname";
next if $dirname =~ /^\./;

# info file ?
$infofile = "plugins/$dirname/$dirname.info";
if (-f $infofile) {
$plugininfo = plugin_get_info($infofile);

if (exists $plugins{$dirname}) {
# existing plugin
}
else {
# new plugin
$pluginrec = {name => $dirname,
version => $plugininfo->{version},
label => $plugininfo->{label} || $dirname,
active => undef};

$dbif->insert('plugins', %$pluginrec);

$plugins{$dirname} = $pluginrec;
}
}

if ($plugininfo->{require}) {
my @modules;

@modules = split(/\s*,\s*/, $plugininfo->{require});

for (@modules) {
# warn("Require $_.\n");
}
}
}
closedir(PLUGINS);
}

return \%plugins;
}

sub plugin_enable {
my ($dbif, $plugin) = @_;

$dbif->update('plugins', 'name = ' . $dbif->quote($plugin), active => 1);
}

sub plugin_get_info {
my ($infofile) = @_;
my %info;

open(INFOFILE, $infofile)
|| die "Failed to open plugin info file $infofile\n";
while (<INFOFILE>) {
chomp;
# skip emptylines and comments
next if ! /\S/;
next if /^\s*#/;
next unless /=/;

my ($key, $value) = split(/\s*=\s*/, $_, 2);
$info{$key} = $value;
}
close(INFOFILE);

return \%info;
}

1;

0 comments on commit 0e11392

Please sign in to comment.