| 1 |
package Dancer2::Plugin::FooPlugin; |
| 2 |
use Dancer2::Plugin; |
| 3 |
|
| 4 |
on_plugin_import { |
| 5 |
my $dsl = shift; |
| 6 |
$dsl->get( '/sitemap' => sub { _html_sitemap($dsl) } ); |
| 7 |
}; |
| 8 |
|
| 9 |
sub _html_sitemap { |
| 10 |
join( ', ', _retrieve_get_urls(@_) ); |
| 11 |
} |
| 12 |
|
| 13 |
register foo_wrap_request => sub { |
| 14 |
my ($self) = @_; |
| 15 |
return $self->app->request; |
| 16 |
}, { is_global => 0 }; |
| 17 |
|
| 18 |
register foo_route => sub { |
| 19 |
my ($self) = @_; |
| 20 |
$self->get( '/foo', sub {'foo'} ); |
| 21 |
} => { is_global => 1, prototype => '$@' }; |
| 22 |
|
| 23 |
register p_config => sub { |
| 24 |
my $dsl = shift; |
| 25 |
my $config = plugin_setting; |
| 26 |
return $config; |
| 27 |
}; |
| 28 |
|
| 29 |
# taken from SiteMap |
| 30 |
sub _retrieve_get_urls { |
| 31 |
my $dsl = shift; |
| 32 |
my ( $route, @urls ); |
| 33 |
|
| 34 |
for my $app ( @{ $dsl->runner->apps } ) { |
| 35 |
my $routes = $app->routes; |
| 36 |
|
| 37 |
# push the static get routes into an array. |
| 38 |
get_route: |
| 39 |
for my $get_route ( @{ $routes->{get} } ) { |
| 40 |
my $regexp = $get_route->regexp; |
| 41 |
|
| 42 |
# If the pattern is a true comprehensive regexp or the route |
| 43 |
# has a :variable element to it, then omit it. |
| 44 |
next get_route if ( $regexp =~ m/[()[\]|]|:\w/ ); |
| 45 |
|
| 46 |
# If there is a wildcard modifier, then drop it and have the |
| 47 |
# full route. |
| 48 |
$regexp =~ s/\?//g; |
| 49 |
|
| 50 |
# Other than that, its cool to be added. |
| 51 |
push( @urls, $regexp ) |
| 52 |
if !grep { $regexp =~ m/$_/i } |
| 53 |
@$Dancer2::Plugin::SiteMap::OMIT_ROUTES; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return sort(@urls); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
register_plugin; |
| 62 |
1; |