Apache2-Response-FileMerge

 view release on metacpan or  search on metacpan

lib/Apache2/Response/FileMerge.pm  view on Meta::CPAN

        my $delta = _time() - $start;
        $content  = sprintf(
            STATS_PATTERN,
            $uri,
            $mtime,
            $DO_MODIFIED,
            $DO_MIN_JS,
            $DO_MIN_CSS,
            $SEPARATOR,
            $DO_COMPRESS,
            $DOC_ROOT,
            $APPEND_INC_NAME,
            $delta,
            $content
        ) if ( $DO_STATS );

        $r->content_type( $CONTENT_TYPES{$type} || 'text/plain' );
        my $headers                 = $r->headers_out();
        $headers->{LAST_MODIFIED()} = time2str($mtime);
        $cache{$uri}{'mtime'}       = $mtime;

        if ( $DO_COMPRESS ) {
            $r->content_encoding('gzip');
            $content = _compress($content);
        }

        $r->print($content);

        return ( $has_content ) ? Apache2::Const::OK 
                                : Apache2::Const::NOT_FOUND;
    }

    {
        my %loaded;

        sub _init {
            my( $r ) = @_;

            %loaded  = ();
            %VARS    = map {
                my( $k, $v ) = split(/=/);
                $k => URI::Escape::uri_unescape( ( $v || '' ) )
            } split( /[&;]/, $r->args() );
        }

        sub _load_content {
            my ( $root, $location, $file_name, $type ) = @_;
            return unless ( $file_name );

            _substitute_vars( \$location, \$file_name ) if ( %VARS );

            $LOG->debug( "\$location = $location" );
            $location =~ s/\/$//g;
            $location = "$location/";
            $LOG->debug( "\$location = $location" );

            $file_name       =  "${location}${file_name}" if ( $location );
            $file_name       =  "$root/$file_name.$type";
            my $cname        =  $file_name;
            $cname           =~ s/\///g;
            my $this_mtime   =  ( stat($file_name) )[9];
            $mtime         ||= 0;
            $mtime           =  $this_mtime if ( ! $mtime || $mtime > ( $this_mtime || 0 ) );
            my $content      =  '';

            if ( exists( $loaded{$cname} ) ) {
                $LOG->debug("Attempting to include \"$file_name\" more than once");
                return;
            }
            else {
                $loaded{$cname} = \0;
                $LOG->debug("Loading: $file_name");        
            }

            if ( open( my $handle, '<', $file_name ) ) {
                {
                    local $/ = undef;
                    $content = <$handle>;
                }
                close( $handle );
            }
            else {
                $LOG->error("File not found: \"$file_name\".");
                return;
            }

            $content = _sf_escape($content);
            while ( $content =~ /(\/\\\*\\\*\s*[Ii]nc(?:lude)\s*([-\{\}\w\.\/]+)\s*\\\*\\\*\/)/sgm ) {
                my ( $matcher, $file )      =  ( $1, $2 );
                my ( $inc_file, $inc_type ) =  $file =~ /^(.*?)\.(js|css)$/;
                my $new_file_content        =  _load_content( $root, '', $inc_file, $inc_type ) || '';
                $new_file_content           = sprintf( COMMENT_PATTERN, "$root/$inc_file" ) 
                                                . "\n\n$new_file_content" if ( $APPEND_INC_NAME );
                $content                    =~ s/\/\\\*\\\*\s*[Ii]nc(?:lude)\s*[-\{\}\w\.\/]+\s*\\\*\\\*\//$new_file_content/sm;
            }

            return _sf_unescape($content);
        }

        sub _substitute_vars {
            foreach my $string ( @_ ) {
                while( $$string =~ /\{\s*([\w-]+)\s*\}/g ) {
                    my $varname = $1;
                    next unless defined $VARS{$varname};
                    $$string =~ s/\{\s*$varname\s*\}/$VARS{$varname}/g;
                }
            }
        }
    }
}

sub append_inc_name {
    return $APPEND_INC_NAME = pop;
}

sub document_root {
    return $DOC_ROOT = pop;
}

sub file_separator {
    return $SEPARATOR = pop;



( run in 0.395 second using v1.01-cache-2.11-cpan-39bf76dae61 )