1package File::Assets::Filter::Concat;
2
3use strict;
4use warnings;
5
6use base qw/File::Assets::Filter::Collect/;
7
8sub signature {
9    return "concat";
10}
11
12sub build_content {
13    my $self = shift;
14
15    my $matched = $self->matched;
16
17    my $content = "";
18    for my $match (@$matched) {
19        $content .= ${ $match->{asset}->content };
20    }
21
22    return \$content;
23}
24
251;
26
27__END__
28
29use Digest;
30
31use base qw/File::Assets::Filter/;
32
33sub post_process {
34    my $self = shift;
35    my $assets = shift;
36    my $matched = shift;
37
38    return unless @$matched;
39
40    return if 1 == @$matched;
41
42    my $top_match = $matched->[0];
43    my $top_asset = $top_match->{asset};
44
45    for my $match (reverse @$matched) {
46        my $rank = $match->{rank};
47        splice @$assets, $rank, 1, ();
48    }
49
50    my $content = "";
51    for my $match (@$matched) {
52        $content .= ${ $match->{asset}->content };
53    }
54
55    my $type = $top_asset->type;
56    my $path;
57    if ($self->package) {
58        $path = $self->package->path;
59    }
60    if (! defined $path) {
61        my $digest = Digest->new("MD5");
62        $digest->add($content);
63        $path = $digest->hexdigest;
64    }
65    $path .= "." . ($type->extensions)[0] if $path =~ m/(?:^|\/)[^\.]+/;
66
67    my $asset = File::Assets->_parse_asset_by_path(
68        path => $path,
69        type => $type,
70        base => $top_asset->resource->base,
71    );
72
73    my $file = $asset->file;
74    my $dir = $file->parent;
75    $dir->mkpath unless -d $dir;
76    $file->openw->print($content);
77
78    splice @$assets, $top_match->{rank}, 0, $asset;
79}
80
811;
82