PPI/Cache.pm

filter: Statementstmt 93.5% 72 / 77 Branchbran 58.8% 20 / 34 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 19 / 19 Podpod 100% 5 / 5 totaltotal 77.9% 120 / 154 CC 22 SCAR 14.9
CC
22
Cov
78%
new
11.2
_md5hex
6.1
store_document
4.3
CRAP 4.5
« PPI/Exception.pm ↑ Summary PPI/Element.pm »
1 package PPI::Cache;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Cache - The PPI Document Caching Layer
8  
9 =head1 SYNOPSIS
10  
11 # Set the cache
12 use PPI::Cache path => '/var/cache/ppi-cache';
13
14 # Manually create a cache
15 my $Cache = PPI::Cache->new(
16 path => '/var/cache/perl/class-PPI',
17 readonly => 1,
18 );
19  
20 =head1 DESCRIPTION
21  
22 C<PPI::Cache> provides the default caching functionality for L<PPI>.
23  
24 It integrates automatically with L<PPI> itself. Once enabled, any attempt
25 to load a document from the filesystem will be cached via cache.
26  
27 Please note that creating a L<PPI::Document> from raw source or something
28 other object will B<not> be cached.
29  
30 =head2 Using PPI::Cache
31  
32 The most common way of using C<PPI::Cache> is to provide parameters to
33 the C<use> statement at the beginning of your program.
34  
35 # Load the class but do not set a cache
36 use PPI::Cache;
37
38 # Use a fairly normal cache location
39 use PPI::Cache path => '/var/cache/ppi-cache';
40  
41 Any of the arguments that can be provided to the C<new> constructor can
42 also be provided to C<use>.
43  
44 =head1 METHODS
45  
46 =cut
47  
48 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use Storable 2.17 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use Digest::MD5 2.35 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Params::Util qw{_INSTANCE _SCALAR};
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use PPI::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 our $VERSION = '1.260'; # VERSION
58  
59 2 ▶use constant VMS => !! ( $^O eq 'VMS' );
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 sub import {
62 2 ▶ my $class = ref $_[0] ? ref shift : shift;
Subroutine import: called CC=4 SCAR=14.5
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
63 2 ▶ return 1 unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 1 2
64  
65 # Create a cache from the params provided
66 1 my $cache = $class->new(@_);
67  
68 # Make PPI::Document use it
69 1 ▶ unless ( PPI::Document->set_cache( $cache ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("PPI::Document"->set_cache($cache)) 0 1 1
70 0 Carp::croak("Failed to set cache in PPI::Document");
71 }
72  
73 1 1;
74 }
75  
76  
77  
78  
79  
80 #####################################################################
81 # Constructor and Accessors
82  
83 =pod
84  
85 =head2 new param => $value, ...
86  
87 The C<new> constructor creates a new standalone cache object.
88  
89 It takes a number of parameters to control the cache.
90  
91 =over
92  
93 =item path
94  
95 The C<path> param sets the base directory for the cache. It must already
96 exist, and must be writable.
97  
98 =item readonly
99  
100 The C<readonly> param is a true/false flag that allows the use of an
101 existing cache by a less-privileged user (such as the web user).
102  
103 Existing documents will be retrieved from the cache, but new documents
104 will not be written to it.
105  
106 =back
107  
108 Returns a new C<PPI::Cache> object, or dies on error.
109  
110 =cut
111  
112 sub new {
113 2 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=24.1
Pod documented
114 2 my %params = @_;
115  
116 # Path should exist and be usable
117 my $path = $params{path}
118 2 ▶ or Carp::croak("Cannot create PPI::Cache, no path provided");
Branch: 1 branch
BranchTrueFalseTotal
my $path = $params{"path"} or Carp::croak("Cannot create PPI::Cache, no path provided") 0 2 2
119 2 ▶ unless ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 2 2
120 0 Carp::croak("Cannot create PPI::Cache, path does not exist");
121 }
122 2 ▶ unless ( -r $path and -x $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $path and -x $path) 0 2 2
Condition33%1/3
-r $path and -x $path
!ll&&!rl&&r
002
MC/DC0%0/2
-r $path and -x $path
-r $path -x $path
Truth table33%1/3
A and B
A= -r $pathB= -x $path
ABresult
0X0
100
111
123 0 Carp::croak("Cannot create PPI::Cache, no read permissions for path");
124 }
125 2 ▶ if ( ! $params{readonly} and ! -w $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"readonly"} or -w $path) 0 2 2
Condition33%1/3
$params{"readonly"} or -w $path
l!l&&r!l&&!r
020
MC/DC0%0/2
$params{"readonly"} or -w $path
$params{"readonly"} -w $path
Truth table33%1/3
A or B
A= $params{"readonly"}B= -w $path
ABresult
1X1
011
000
126 0 Carp::croak("Cannot create PPI::Cache, no write permissions for path");
127 }
128  
129 # Create the basic object
130 my $self = bless {
131 path => $path,
132 readonly => !! $params{readonly},
133 2 }, $class;
134  
135 2 $self;
136 }
137  
138 =pod
139  
140 =head2 path
141  
142 The C<path> accessor returns the path on the local filesystem that is the
143 root of the cache.
144  
145 =cut
146  
147 11 ▶sub path { $_[0]->{path} }
Subroutine path: called CC=1 SCAR=0.0
Pod documented
148  
149 =pod
150  
151 =head2 readonly
152  
153 The C<readonly> accessor returns true if documents should not be written
154 to the cache.
155  
156 =cut
157  
158 4 ▶sub readonly { $_[0]->{readonly} }
Subroutine readonly: called CC=1 SCAR=0.0
Pod documented
159  
160  
161  
162  
163  
164 #####################################################################
165 # PPI::Cache Methods
166  
167 =pod
168  
169 =head2 get_document $md5sum | \$source
170  
171 The C<get_document> method checks to see if a Document is stored in the
172 cache and retrieves it if so.
173  
174 =cut
175  
176 sub get_document {
177 5 ▶ my $self = ref $_[0]
Subroutine get_document: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
178 ? shift
179 : Carp::croak('PPI::Cache::get_document called as static method');
180 5 ▶ my $md5hex = $self->_md5hex(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $md5hex = $self->_md5hex(shift()) or return undef 0 5 5
181 5 $self->_load($md5hex);
182 }
183  
184 =pod
185  
186 =head2 store_document $Document
187  
188 The C<store_document> method takes a L<PPI::Document> as argument and
189 explicitly adds it to the cache.
190  
191 Returns true if saved, or C<undef> (or dies) on error.
192  
193 FIXME (make this return either one or the other, not both)
194  
195 =cut
196  
197 sub store_document {
198 2 ▶ my $self = shift;
Subroutine store_document: called CC=4 SCAR=14.6
Pod documented
199 2 ▶ my $Document = _INSTANCE(shift, 'PPI::Document') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Document = &_INSTANCE(shift(), "PPI::Document") or return undef 0 2 2
200  
201 # Shortcut if we are readonly
202 2 ▶ return 1 if $self->readonly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->readonly 0 2 2
203  
204 # Find the filename to save to
205 2 ▶ my $md5hex = $Document->hex_id or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $md5hex = $Document->hex_id or return undef 0 2 2
206  
207 # Store the file
208 2 $self->_store( $md5hex, $Document );
209 }
210  
211  
212  
213  
214  
215 #####################################################################
216 # Support Methods
217  
218 # Store an arbitrary PPI::Document object (using Storable) to a particular
219 # path within the cache filesystem.
220 sub _store {
221 3 ▶ my ($self, $md5hex, $object) = @_;
Subroutine _store: called CC=2 SCAR=7.0
222 3 my ($dir, $file) = $self->_paths($md5hex);
223  
224 # Save the file
225 3 ▶ File::Path::mkpath( $dir, 0, 0755 ) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 3 0 3
226 3 if ( VMS ) {
227 Storable::lock_nstore( $object, $file );
228 } else {
229 3 Storable::nstore( $object, $file );
230 }
231 }
232  
233 # Load an arbitrary object (using Storable) from a particular
234 # path within the cache filesystem.
235 sub _load {
236 6 ▶ my ($self, $md5hex) = @_;
Subroutine _load: called CC=3 SCAR=11.2
237 6 my (undef, $file) = $self->_paths($md5hex);
238  
239 # Load the file
240 6 ▶ return '' unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 5 6
241 5 my $object = VMS
242 ? Storable::retrieve( $file )
243 : Storable::lock_retrieve( $file );
244  
245 # Security check
246 5 ▶ unless ( _INSTANCE($object, 'PPI::Document') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_INSTANCE($object, "PPI::Document")) 0 5 5
247 0 Carp::croak("Security Violation: Object in '$file' is not a PPI::Document");
248 }
249  
250 5 $object;
251 }
252  
253 # Convert a md5 to a dir and file name
254 sub _paths {
255 9 ▶ my $self = shift;
Subroutine _paths: called CC=1 SCAR=0.0
256 9 my $md5hex = lc shift;
257 9 my $dir = File::Spec->catdir( $self->path, substr($md5hex, 0, 1), substr($md5hex, 0, 2) );
258 9 my $file = File::Spec->catfile( $dir, $md5hex . '.ppi' );
259 9 return ($dir, $file);
260 }
261  
262 # Check a md5hex param
263 sub _md5hex {
264 9 ▶ my $either = shift;
Subroutine _md5hex: called CC=5 SCAR=18.1
265 my $it = _SCALAR($_[0])
266 9 ▶ ? PPI::Util::md5hex(${$_[0]})
Branch: 1 branch
BranchTrueFalseTotal
&_SCALAR($_[0]) ? : 6 3 9
267 : $_[0];
268 9 ▶ return (defined $it and ! ref $it and $it =~ /^[[:xdigit:]]{32}\z/s)
Branch: 1 branch
BranchTrueFalseTotal
defined $it && !ref($it) && $it =~ /^[[:xdigit:]]{32}\z/s ? : 9 0 9
Condition33%2/6
defined $it and not ref $it and $it =~ /^[[:xdigit:]]{32}\z/s
!ll&&!rl&&r
009
defined $it and not ref $it
!ll&&!rl&&r
009
MC/DC0%0/3
defined $it and not ref $it and $it =~ /^[[:xdigit:]]{32}\z/s
defined $it not ref $it $it =~ /^[[:xdigit:]]{32}\z/s
Truth table25%1/4
A and not(B) and C
A= defined $itB= not ref $itC= $it =~ /^[[:xdigit:]]{32}\z/s
ABCresult
0XX0
10X0
1100
1111
269 ? lc $it
270 : undef;
271 }
272  
273 2 1;
274  
275 =pod
276  
277 =head1 TO DO
278  
279 - Finish the basic functionality
280  
281 - Add support for use PPI::Cache auto-setting $PPI::Document::CACHE
282  
283 =head1 SUPPORT
284  
285 See the L<support section|PPI/SUPPORT> in the main module.
286  
287 =head1 AUTHOR
288  
289 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
290  
291 =head1 COPYRIGHT
292  
293 Copyright 2005 - 2011 Adam Kennedy.
294  
295 This program is free software; you can redistribute
296 it and/or modify it under the same terms as Perl itself.
297  
298 The full text of the license can be found in the
299 LICENSE file included with this module.
300  
301 =cut
« PPI/Exception.pm ↑ Summary PPI/Element.pm »