]> Dogcows Code - chaz/p5-Catalyst-Plugin-Sitemap/blob - lib/Catalyst/Plugin/Sitemap.pm
73639508f26c3c2b1fa3034011aa8b9861e4e0d2
[chaz/p5-Catalyst-Plugin-Sitemap] / lib / Catalyst / Plugin / Sitemap.pm
1 package Catalyst::Plugin::Sitemap;
2
3 use strict;
4 use warnings;
5
6 use Moose::Role;
7
8 no warnings qw/uninitialized/;
9
10 use Search::Sitemap;
11 use List::Util qw/ first /;
12
13 sub sitemap {
14 my $self = shift;
15
16 my $sitemap = Search::Sitemap->new;
17 $sitemap->pretty(1);
18
19 for my $controller ( $self->controller(qr//) ) {
20 ACTION:
21 for my $a ( $controller->get_action_methods ) {
22
23 my $action = $controller->action_for( $a->name );
24
25 my $attr = $action->attributes->{Sitemap} or next ACTION;
26
27 die "more than one attribute 'Sitemap' for sub ",
28 $a->fully_qualified_name
29 if @$attr > 1;
30
31 my @attr = split /\s*(?:,|=>)\s*/, $attr->[0];
32
33 my %uri_params;
34
35 if ( @attr == 1 ) {
36 if ( $attr[0] eq '*' ) {
37 my $sitemap_method = $action->name . "_sitemap";
38
39 if ( $controller->can($sitemap_method) ) {
40 $controller->$sitemap_method( $self, $sitemap );
41 next ACTION;
42 }
43 }
44
45 if ( $attr[0] + 0 > 0 ) {
46 # it's a number
47 $uri_params{priority} = $attr[0];
48 }
49
50 }
51 elsif ( @attr > 0 ) {
52 %uri_params = @attr;
53 }
54
55 $uri_params{loc} = $self->uri_for_action( $action->private_path );
56
57 $sitemap->add( \%uri_params );
58
59 next ACTION;
60 }
61
62 }
63
64 return $sitemap;
65 }
66
67 1;
68
69 __END__
70
71 ways I can do it
72
73 is Sitemap( <yadah> ) ? => use it as is
74 else => has <action>_sitemap? => use it
75 else => use the uri directly
76
77
78 sitemap => sub {
79 my ( $self, $c, $sitemap ) = @_;
80
81 ...
82 };
83
84 sub action_sitemap {
85 my ( $self, $c, $sitemap ) = @_;
86 }
87
88 sub do_stuff :Local :Sitemap {
89 }
This page took 0.039897 seconds and 3 git commands to generate.