| 1 | =head1 NAME | ||
| 2 | |||
| 3 | MKDoc::Core::ResourceFinder - Inheritable resources module. | ||
| 4 | |||
| 5 | |||
| 6 | =head1 SUMMARY | ||
| 7 | |||
| 8 | There's more to a web application than just code. Typically you also want to | ||
| 9 | have also config files, some images (i.e logos, buttons, etc), some CSS files, | ||
| 10 | some templates, etc. | ||
| 11 | |||
| 12 | Those things need to have a URI and they also need to be accessible from within | ||
| 13 | your program. | ||
| 14 | |||
| 15 | Ideally you want to be able to: | ||
| 16 | |||
| 17 | =over 4 | ||
| 18 | |||
| 19 | =item Bundle those files with your CPAN distribution. | ||
| 20 | |||
| 21 | =item Serve those files 'out of the box' when installing a new site. | ||
| 22 | |||
| 23 | =item Have an easy mechanism to override those files. | ||
| 24 | |||
| 25 | =back | ||
| 26 | |||
| 27 | L<MKDoc::Core::ResourceFinder> is a module providing helper functions in order | ||
| 28 | to achieve this goal. | ||
| 29 | |||
| 30 | Any L<MKDoc::Core> product can bundle resource files anywhere in | ||
| 31 | lib/MKDoc/resources. It means that the files will be installed somewhere in | ||
| 32 | @INC/MKDoc/resources. | ||
| 33 | |||
| 34 | The L<MKDoc::Core::Plugin::Resources> will serve the files so that a request to | ||
| 35 | /.resources/foo/bar.txt serves the right @INC/MKDoc/resources/foo/bar.txt file. | ||
| 36 | |||
| 37 | However, those files are mainly 'factory default', shipped so that you can have | ||
| 38 | something working immediately. You certainly almost always want to redefine | ||
| 39 | them either on a server-wide basis, or even on a per-site basis. | ||
| 40 | |||
| 41 | In order to accomplish this, the L<MKDoc::Core::ResourceFinder> looks for | ||
| 42 | resources in: | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =item <SITE_DIR>/resources | ||
| 47 | |||
| 48 | =item <MKDOC_DIR>/resources | ||
| 49 | |||
| 50 | =item @INC/resources | ||
| 51 | |||
| 52 | =back | ||
| 53 | |||
| 54 | Furthermore, whenever it sees a file which is called "<filename>.deleted" it | ||
| 55 | will consider that the file is not there, even if it's defined later on. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | package MKDoc::Core::ResourceFinder; | ||
| 59 | use warnings; | ||
| 60 | use strict; | ||
| 61 | |||
| 62 | our $DIRS = undef; | ||
| 63 | |||
| 64 | |||
| 65 | sub permanent_resource_dirs | ||
| 66 | { | ||
| 67 | $DIRS and return @{$DIRS}; | ||
| 68 | |||
| 69 | my $MKDOC_DIR = $ENV{MKDOC_DIR} || '.'; | ||
| 70 | |||
| 71 | my @dirs = @INC; | ||
| 72 | my @res = (); | ||
| 73 | push @res, $MKDOC_DIR . '/resources'; | ||
| 74 | push @res, map { $_ ? "$_/MKDoc/resources" : () } grep ( !/^\Q$MKDOC_DIR\E$/, @INC ); | ||
| 75 | |||
| 76 | my @new_res = (); | ||
| 77 | my %seen = (); | ||
| 78 | foreach my $res (@res) | ||
| 79 | { | ||
| 80 | $seen{$res} and next; | ||
| 81 | $seen{$res} = 1; | ||
| 82 | -d $res or next; | ||
| 83 | push @new_res, $res; | ||
| 84 | } | ||
| 85 | |||
| 86 | $DIRS = \@new_res; | ||
| 87 | |||
| 88 | return @{$DIRS}; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | =head1 API | ||
| 93 | |||
| 94 | =head2 MKDoc::Core::ResourceFinder::list ($some_directory); | ||
| 95 | |||
| 96 | Reads the content of multiple directories (<SITE_DIR>/resources, | ||
| 97 | <MKDOC_DIR>/resources, @INC) and merges it in a single list. Returns the | ||
| 98 | merged list. | ||
| 99 | |||
| 100 | Use this instead of readdir(). | ||
| 101 | |||
| 102 | =cut | ||
| 103 | sub list | ||
| 104 | { | ||
| 105 | my $dir = shift; | ||
| 106 | my %files = (); | ||
| 107 | my %nono = (); | ||
| 108 | |||
| 109 | foreach my $base ("$ENV{SITE_DIR}/resources", permanent_resource_dirs()) | ||
| 110 | { | ||
| 111 | -d $base || next; | ||
| 112 | my $dir_path = $base . $dir; | ||
| 113 | |||
| 114 | opendir DD, $dir_path or next; | ||
| 115 | my @files = readdir (DD); | ||
| 116 | closedir (DD); | ||
| 117 | |||
| 118 | %nono = ( (%nono), ( map { s/\.deleted$//; $_ } grep /\.deleted$/, @files ) ); | ||
| 119 | @files = grep !/\.deleted$/, @files; | ||
| 120 | |||
| 121 | for (@files) | ||
| 122 | { | ||
| 123 | $_ || next; | ||
| 124 | $nono{$_} && next; | ||
| 125 | $files{$_} = 1; | ||
| 126 | } | ||
| 127 | closedir DD; | ||
| 128 | } | ||
| 129 | |||
| 130 | my @res = sort keys %files; | ||
| 131 | return wantarray ? @res : \@res; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | =head2 MKDoc::Core::ResourceFinder::rel2abs ($something); | ||
| 136 | |||
| 137 | Searches for $something in multiple directories, and returns the full path if found. | ||
| 138 | Returns undef otherwise. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | sub rel2abs | ||
| 142 | { | ||
| 143 | my $rel = shift; | ||
| 144 | $rel =~ s/^\///; | ||
| 145 | |||
| 146 | foreach my $dir ("$ENV{SITE_DIR}/resources", permanent_resource_dirs()) | ||
| 147 | { | ||
| 148 | $dir || next; | ||
| 149 | my $file = "$dir/$rel"; | ||
| 150 | -f "$file.deleted" and return; | ||
| 151 | -f $file and return $file; | ||
| 152 | } | ||
| 153 | |||
| 154 | return; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 | |||
| 161 | __END__ |