1 package TAP
::Parser
::IteratorFactory
;
4 use vars
qw($VERSION @ISA);
8 use Carp qw( confess );
9 use File
::Basename
qw( fileparse );
11 @ISA = qw(TAP::Object);
13 use constant handlers
=> [];
17 TAP::Parser::IteratorFactory - Figures out which SourceHandler objects to use for a given Source
29 use TAP::Parser::IteratorFactory;
30 my $factory = TAP::Parser::IteratorFactory->new({ %config });
31 my $iterator = $factory->make_iterator( $filename );
35 This is a factory class that takes a L<TAP::Parser::Source> and runs it through all the
36 registered L<TAP::Parser::SourceHandler>s to see which one should handle the source.
38 If you're a plugin author, you'll be interested in how to L</register_handler>s,
39 how L</detect_source> works.
47 Creates a new factory class:
49 my $sf = TAP::Parser::IteratorFactory->new( $config );
51 C<$config> is optional. If given, sets L</config> and calls L</load_handlers>.
56 my ( $self, $config ) = @_;
57 $self->config( $config || {} )->load_handlers;
61 =head3 C<register_handler>
63 Registers a new L<TAP::Parser::SourceHandler> with this factory.
65 __PACKAGE__->register_handler( $handler_class );
69 List of handlers that have been registered.
73 sub register_handler
{
74 my ( $class, $dclass ) = @_;
76 confess
("$dclass must implement can_handle & make_iterator methods!")
77 unless UNIVERSAL
::can
( $dclass, 'can_handle' )
78 && UNIVERSAL
::can
( $dclass, 'make_iterator' );
80 my $handlers = $class->handlers;
81 push @
{$handlers}, $dclass
82 unless grep { $_ eq $dclass } @
{$handlers};
87 ##############################################################################
89 =head2 Instance Methods
93 my $cfg = $sf->config;
94 $sf->config({ Perl => { %config } });
96 Chaining getter/setter for the configuration of the available source handlers.
97 This is a hashref keyed on handler class whose values contain config to be passed
98 onto the handlers during detection & creation. Class names may be fully qualified
101 # these are equivalent
102 $sf->config({ 'TAP::Parser::SourceHandler::Perl' => { %config } });
103 $sf->config({ 'Perl' => { %config } });
109 return $self->{config
} unless @_;
110 unless ( 'HASH' eq ref $_[0] ) {
111 $self->_croak('Argument to &config must be a hash reference');
113 $self->{config
} = shift;
119 return $self->{last_handler
} unless @_;
120 $self->{last_handler
} = shift;
126 return $self->{testing
} unless @_;
127 $self->{testing
} = shift;
131 ##############################################################################
133 =head3 C<load_handlers>
137 Loads the handler classes defined in L</config>. For example, given a config:
140 MySourceHandler => { some => 'config' },
143 C<load_handlers> will attempt to load the C<MySourceHandler> class by looking in
144 C<@INC> for it in this order:
146 TAP::Parser::SourceHandler::MySourceHandler
155 for my $handler ( keys %{ $self->config } ) {
156 my $sclass = $self->_load_handler($handler);
158 # TODO: store which class we loaded anywhere?
164 my ( $self, $handler ) = @_;
167 for my $dclass ( "TAP::Parser::SourceHandler::$handler", $handler ) {
169 if UNIVERSAL
::can
( $dclass, 'can_handle' )
170 && UNIVERSAL
::can
( $dclass, 'make_iterator' );
179 if UNIVERSAL
::can
( $dclass, 'can_handle' )
180 && UNIVERSAL
::can
( $dclass, 'make_iterator' );
182 "handler '$dclass' does not implement can_handle & make_iterator";
186 "Cannot load handler '$handler': " . join( "\n", @errors ) );
189 ##############################################################################
191 =head3 C<make_iterator>
193 my $iterator = $src_factory->make_iterator( $source );
195 Given a L<TAP::Parser::Source>, finds the most suitable L<TAP::Parser::SourceHandler>
196 to use to create a L<TAP::Parser::Iterator> (see L</detect_source>). Dies on error.
201 my ( $self, $source ) = @_;
203 $self->_croak('no raw source defined!') unless defined $source->raw;
205 $source->config( $self->config )->assemble_meta;
207 # is the raw source already an object?
209 if ( $source->meta->{is_object
}
210 && UNIVERSAL
::isa
( $source->raw, 'TAP::Parser::SourceHandler' ) );
212 # figure out what kind of source it is
213 my $sd_class = $self->detect_source($source);
214 $self->_last_handler($sd_class);
216 return if $self->_testing;
219 my $iterator = $sd_class->make_iterator($source);
224 =head3 C<detect_source>
226 Given a L<TAP::Parser::Source>, detects what kind of source it is and
227 returns I<one> L<TAP::Parser::SourceHandler> (the most confident one). Dies
230 The detection algorithm works something like this:
232 for (@registered_handlers) {
233 # ask them how confident they are about handling this source
234 $confidence{$handler} = $handler->can_handle( $source )
236 # choose the most confident handler
238 Ties are handled by choosing the first handler.
243 my ( $self, $source ) = @_;
245 confess
('no raw source ref defined!') unless defined $source->raw;
247 # find a list of handlers that can handle this source:
249 for my $dclass ( @
{ $self->handlers } ) {
250 my $confidence = $dclass->can_handle($source);
252 # warn "handler: $dclass: $confidence\n";
253 $handlers{$dclass} = $confidence if $confidence;
258 # use Data::Dump qw( pp );
261 # error: can't detect source
262 my $raw_source_short = substr( ${ $source->raw }, 0, 50 );
263 confess
("Cannot detect source of '$raw_source_short'!");
267 # if multiple handlers can handle it, choose the most confident one
270 sort { $handlers{$a} cmp $handlers{$b} }
274 # this is really useful for debugging handlers:
275 if ( $ENV{TAP_HARNESS_SOURCE_FACTORY_VOTES
} ) {
278 join( ', ', map {"$_: $handlers{$_}"} @handlers ),
284 return pop @handlers;
293 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
297 If we've done things right, you'll probably want to write a new source,
298 rather than sub-classing this (see L<TAP::Parser::SourceHandler> for that).
300 But in case you find the need to...
302 package MyIteratorFactory;
307 use TAP::Parser::IteratorFactory;
309 @ISA = qw( TAP::Parser::IteratorFactory );
311 # override source detection algorithm
313 my ($self, $raw_source_ref, $meta) = @_;
314 # do detective work, using $meta and whatever else...
325 Originally ripped off from L<Test::Harness>.
327 Moved out of L<TAP::Parser> & converted to a factory class to support
328 extensible TAP source detective work by Steve Purkis.
334 L<TAP::Parser::SourceHandler>,
335 L<TAP::Parser::SourceHandler::File>,
336 L<TAP::Parser::SourceHandler::Perl>,
337 L<TAP::Parser::SourceHandler::RawTAP>,
338 L<TAP::Parser::SourceHandler::Handle>,
339 L<TAP::Parser::SourceHandler::Executable>