| 1 | package WWW::MenuGrinder; | ||
| 2 | our $VERSION = '0.01_01'; | ||
| 3 | |||
| 4 | |||
| 5 | # ABSTRACT: A tool for managing dynamic website menus - base class. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Moose; | ||
| 11 | |||
| 12 | use WWW::MenuGrinder::Role::Plugin; | ||
| 13 | |||
| 14 | has 'menu' => ( | ||
| 15 | is => 'rw', | ||
| 16 | ); | ||
| 17 | |||
| 18 | has 'plugins' => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'ArrayRef[WWW::MenuGrinder::Role::Plugin]', | ||
| 21 | default => sub { [] }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has 'plugin_hash' => ( | ||
| 25 | is => 'rw', | ||
| 26 | default => sub { + {} }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has 'loader' => ( | ||
| 30 | is => 'rw', | ||
| 31 | # isa => 'WWW::MenuGrinder::Role::Loader', | ||
| 32 | lazy => 1, | ||
| 33 | default => sub { | ||
| 34 | my $self = shift; | ||
| 35 | my @loaders = @{ $self->plugins_with(-Loader) }; | ||
| 36 | die "Need exactly one Loader plugin" unless @loaders == 1; | ||
| 37 | $loaders[0]; | ||
| 38 | }, | ||
| 39 | ); | ||
| 40 | |||
| 41 | has 'output' => ( | ||
| 42 | is => 'rw', | ||
| 43 | # isa => 'WWW::MenuGrinder::Role::Output', | ||
| 44 | lazy => 1, | ||
| 45 | default => sub { | ||
| 46 | my $self = shift; | ||
| 47 | my @outputs = @{ $self->plugins_with(-Output) }; | ||
| 48 | die "Need exactly one Output plugin" unless @outputs == 1; | ||
| 49 | $outputs[0]; | ||
| 50 | }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | has 'premogrifiers' => ( | ||
| 54 | is => 'rw', | ||
| 55 | # isa => 'ArrayRef[WWW::MenuGrinder::Role::PreMogrifier]', | ||
| 56 | lazy => 1, | ||
| 57 | default => sub { | ||
| 58 | my $self = shift; | ||
| 59 | $self->plugins_with(-PreMogrifier); | ||
| 60 | }, | ||
| 61 | ); | ||
| 62 | |||
| 63 | has 'mogrifiers' => ( | ||
| 64 | is => 'rw', | ||
| 65 | # isa => 'ArrayRef[WWW::MenuGrinder::Role::Mogrifier]', | ||
| 66 | lazy => 1, | ||
| 67 | default => sub { | ||
| 68 | my $self = shift; | ||
| 69 | $self->plugins_with(-Mogrifier); | ||
| 70 | }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | has 'config' => ( | ||
| 74 | is => 'rw', | ||
| 75 | default => sub { + {} }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub plugins_with { | ||
| 79 | my ($self, $role) = @_; | ||
| 80 | |||
| 81 | $role =~ s/^-/WWW::MenuGrinder::Role::/; | ||
| 82 | |||
| 83 | return [ grep $_->does($role), @{ $self->plugins } ] | ||
| 84 | } | ||
| 85 | |||
| 86 | sub register_plugin { | ||
| 87 | my ($self, $class, $plugin) = @_; | ||
| 88 | |||
| 89 | push @{ $self->plugins }, $plugin; | ||
| 90 | $self->plugin_hash->{$class} = $plugin; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub load_plugin { | ||
| 94 | my ($self, $class) = @_; | ||
| 95 | |||
| 96 | my $shortname; | ||
| 97 | |||
| 98 | if ($class =~ /^\+/) { | ||
| 99 | $class =~ s/^\+//; | ||
| 100 | } else { | ||
| 101 | $shortname = $class; | ||
| 102 | $class =~ s/^/WWW::MenuGrinder::Plugin::/; | ||
| 103 | } | ||
| 104 | |||
| 105 | return $self->plugin_hash->{$class} if $self->plugin_hash->{$class}; | ||
| 106 | |||
| 107 | eval "require $class; 1" or die $@; | ||
| 108 | |||
| 109 | if ($class->can('plugin_depends')) { | ||
| 110 | my @deps = $class->plugin_depends; | ||
| 111 | for my $dep (@deps) { | ||
| 112 | eval { | ||
| 113 | $self->load_plugin($dep); | ||
| 114 | }; | ||
| 115 | if ($@) { | ||
| 116 | die "$@ while loading $dep, which was required by $class"; | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | my %plugin_config; | ||
| 122 | |||
| 123 | if (defined $shortname) { | ||
| 124 | my $config = $self->config->{$shortname}; | ||
| 125 | %plugin_config = %$config if defined $config; | ||
| 126 | } else { | ||
| 127 | my $config = $self->config->{$class}; | ||
| 128 | %plugin_config = %$config if defined $config; | ||
| 129 | } | ||
| 130 | |||
| 131 | my $plugin = $class->new( %plugin_config, grinder => $self ); | ||
| 132 | |||
| 133 | $plugin->verify_plugin; | ||
| 134 | |||
| 135 | $self->register_plugin($class, $plugin); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub load_plugins { | ||
| 139 | my ($self, @args) = @_; | ||
| 140 | |||
| 141 | my $plugins = $self->config->{plugins}; | ||
| 142 | |||
| 143 | return unless $plugins; | ||
| 144 | |||
| 145 | for my $class (@$plugins) { | ||
| 146 | $self->load_plugin($class); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub init_menu { | ||
| 151 | my ($self) = @_; | ||
| 152 | |||
| 153 | my $menu = $self->loader->load; | ||
| 154 | $menu = $self->pre_mogrify($menu); | ||
| 155 | $self->menu($menu); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub BUILD { | ||
| 159 | my ($self) = @_; | ||
| 160 | |||
| 161 | $self->load_plugins; | ||
| 162 | $self->init_menu; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub pre_mogrify { | ||
| 166 | my ($self, $menu) = @_; | ||
| 167 | |||
| 168 | $_->before_pre_mogrify($menu) for @{ $self->plugins_with(-BeforePreMogrify) }; | ||
| 169 | |||
| 170 | my @pre_mog = @{ $self->premogrifiers }; | ||
| 171 | |||
| 172 | $menu = $_->pre_mogrify($menu) for @pre_mog; | ||
| 173 | |||
| 174 | return $menu; | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | sub cleanup { | ||
| 179 | my ($self, $menu) = @_; | ||
| 180 | |||
| 181 | for my $plugin (@{ $self->plugins }) { | ||
| 182 | $plugin->cleanup() if $plugin->can('cleanup'); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | sub get_menu { | ||
| 187 | my ($self) = @_; | ||
| 188 | |||
| 189 | $_->before_mogrify($self->menu) for @{ $self->plugins_with(-BeforeMogrify) }; | ||
| 190 | |||
| 191 | my $menu = $self->menu; | ||
| 192 | |||
| 193 | my @mog = @{ $self->mogrifiers }; | ||
| 194 | |||
| 195 | $menu = $_->mogrify($menu) for @mog; | ||
| 196 | |||
| 197 | return $self->output->output($menu); | ||
| 198 | } | ||
| 199 | |||
| 200 | no Moose; | ||
| 201 | 1; | ||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | __END__ | ||
| 206 | =head1 NAME | ||
| 207 | |||
| 208 | WWW::MenuGrinder - A tool for managing dynamic website menus - base class. | ||
| 209 | |||
| 210 | =head1 VERSION | ||
| 211 | |||
| 212 | version 0.01_01 | ||
| 213 | |||
| 214 | =head1 SYNOPSIS | ||
| 215 | |||
| 216 | my $grinder = My::Subclass::Of::MenuGrinder->new( | ||
| 217 | config => { | ||
| 218 | plugins => [ | ||
| 219 | 'XMLLoader', | ||
| 220 | 'Hotkeys', | ||
| 221 | 'NullOutput', | ||
| 222 | ], | ||
| 223 | filename => "/foo/menu.xml" | ||
| 224 | }, | ||
| 225 | ); | ||
| 226 | |||
| 227 | # Some time later... | ||
| 228 | |||
| 229 | my $menu = $grinder->get_menu | ||
| 230 | |||
| 231 | =head1 DESCRIPTION | ||
| 232 | |||
| 233 | C<WWW::MenuGrinder> is a framework for integrating menus into web applications. | ||
| 234 | |||
| 235 | =head1 AUTHOR | ||
| 236 | |||
| 237 | Andrew Rodland <andrew@hbslabs.com> | ||
| 238 | |||
| 239 | =head1 COPYRIGHT AND LICENSE | ||
| 240 | |||
| 241 | This software is copyright (c) 2009 by HBS Labs, LLC.. | ||
| 242 | |||
| 243 | This is free software; you can redistribute it and/or modify it under | ||
| 244 | the same terms as perl itself. | ||
| 245 | |||
| 246 | =head1 WARNING | ||
| 247 | |||
| 248 | Currently this is B<alpha code>. I welcome any opinions, ideas for extensions, | ||
| 249 | new plugins, etc. However, documentation is incomplete, tests are nonexistent, | ||
| 250 | and interfaces are subject to change. B<don't use this in production> unless | ||
| 251 | you want to get yourself in deep. | ||
| 252 | |||
| 253 | =head1 SEE ALSO | ||
| 254 | |||
| 255 | =over 4 | ||
| 256 | |||
| 257 | =item * | ||
| 258 | |||
| 259 | L<WWW::MenuGrinder::Extending> for the best current documentation of internals. | ||
| 260 | |||
| 261 | =item * | ||
| 262 | |||
| 263 | C<t/MyApp/> in L<Catalyst::Model::MenuGrinder> for an example of MenuGrinder | ||
| 264 | in use. | ||
| 265 | |||
| 266 | =item * | ||
| 267 | |||
| 268 | The documentation for each individual plugin, for an idea of the kinds of | ||
| 269 | things that are possible. | ||
| 270 | |||
| 271 | =item * | ||
| 272 | |||
| 273 | L<http://github.com/arodland/www-menugrinder/> for the latest code, and change | ||
| 274 | history. | ||
| 275 | |||
| 276 | =item * | ||
| 277 | |||
| 278 | C<hobbs> on C<irc.perl.org>. I can be found in C<#catalyst> but private | ||
| 279 | messages are okay to avoid off-topicness. | ||
| 280 | |||
| 281 | =back | ||
| 282 |