]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Application.pm
support for unknown flags, for future backwards compatibility
[maypole.git] / lib / Maypole / Application.pm
1 package Maypole::Application;
2
3 use strict;
4 use warnings;
5 use UNIVERSAL::require;
6 use Maypole;
7
8 # funny little gimmick ;)
9 my $banner = <<'';
10  __  __                         _
11 |  \/  | __ _ _   _ _ __   ___ | | ___
12 | |\/| |/ _` | | | | '_ \ / _ \| |/ _ \
13 | |  | | (_| | |_| | |_) | (_) | |  __/
14 |_|  |_|\__,_|\__, | .__/ \___/|_|\___|
15               |___/|_| -= VERSION =-
16
17 $banner =~ s/VERSION/$Maypole::VERSION/;
18 warn $banner;
19
20 our @ISA;
21
22 sub import {
23     my ( $self, @plugins ) = @_;
24     my $caller = caller(0);
25     no strict 'refs';
26     push @{"${caller}::ISA"}, $self;
27     foreach (@plugins) {
28         if    (/^\-Setup$/) { $caller->setup }
29         elsif (/^\-Debug$/) {
30             *{"$caller\::debug"} = sub { 1 };
31             warn "Debugging enabled";
32         }
33         elsif (/^-.*$/) { warn "Unknown flag: $_" }
34         else {
35
36             # The plugin caller should be our application class
37             eval "package $caller; require Maypole::Plugin::$_";
38             if ($@) { warn qq(Loading plugin "Maypole::Plugin::$_" failed: $@) }
39             else {
40                 warn "Loaded plugin: Maypole::Plugin::$_" if $caller->debug;
41                 unshift @ISA, "Maypole::Plugin::$_";
42             }
43         }
44     }
45 }
46
47 if ( $ENV{MOD_PERL} ) {
48     Apache::MVC->require or die "Loading Apache frontend failed: $@";
49     push @ISA, 'Apache::MVC';
50 }
51 else {
52     CGI::Maypole->require or die "Loading CGI frontend failed: $@";
53     push @ISA, 'CGI::Maypole';
54 }
55
56 1;
57
58 =head1 NAME
59
60 Maypole::Application - Maypole Universal Frontend
61
62 =head1 SYNOPSIS
63
64     use Maypole::Application;
65
66     use Maypole::Application qw(Config::YAML);
67
68     use Maypole::Application qw(-Debug Config::YAML -Setup);
69
70     use Maypole::Application qw(Config::YAML Loader -Setup -Debug);
71
72 =head1 DESCRIPTION
73
74 This is a universal frontend for mod_perl1, mod_perl2 and CGI.
75
76 You can omit the Maypole::Plugin:: prefix from plugins.
77 So Maypole::Plugin::Config::YAML becomes Config::YAML.
78
79     use Maypole::Application qw(Config::YAML);
80
81 You can also set special flags like -Setup and -Debug.
82
83     use Maypole::Application qw(-Debug Config::YAML -Setup);
84
85 The position of plugins and flags in the chain is important,
86 because they are loaded/executed in the same order they appear.
87
88 =head2 -Setup
89
90     use Maypole::Application qw(-Setup);
91
92 is equivalent to
93
94     use Maypole::Application;
95     MyApp->setup;
96
97 =head2 -Debug
98
99     use Maypole::Application qw(-Debug);
100
101 is equivalent to
102
103     use Maypole::Application;
104     sub debug { 1 }
105
106 =head1 AUTHOR
107
108 Sebastian Riedel, C<sri@oook.de>
109 Idea by Marcus Ramberg, C<marcus@thefeed.no>
110
111 =head1 LICENSE
112
113 You may distribute this code under the same terms as Perl itself.