|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Name: Combine Scripts |
| 4 | + * Description: A simple PHP script to combine script assets |
| 5 | + * Version: 0.1.0 |
| 6 | + * Author: Daniel M. Hendricks |
| 7 | + * GitHub: https://github.com/dmhendricks/php-combine-scripts |
| 8 | + */ |
| 9 | +use MatthiasMullie\Minify; |
| 10 | +$version = '0.1.0'; |
| 11 | + |
| 12 | +require( __DIR__ . '/vendor/autoload.php' ); |
| 13 | + |
| 14 | +// Load environmental configuration |
| 15 | +if( file_exists( '.env' ) ) { |
| 16 | + $dotenv = \Dotenv\Dotenv::create(__DIR__); |
| 17 | + $dotenv->load(); |
| 18 | +} |
| 19 | + |
| 20 | +// Set variables |
| 21 | +$basedir = rtrim( getenv( 'COMBINE_BASEDIR' ) ?: __DIR__, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR; |
| 22 | +$disable_file_check = filter_var( getenv( 'COMBINE_DISABLE_FILE_EXISTS' ), FILTER_VALIDATE_BOOLEAN ); |
| 23 | +$disable_minify = filter_var( getenv( 'COMBINE_DISABLE_MINIFY' ), FILTER_VALIDATE_BOOLEAN ); |
| 24 | + |
| 25 | +// Get scripts from querystring |
| 26 | +$scripts = isset( $_GET['scripts'] ) ? $_GET['scripts'] : false; |
| 27 | +if( !$scripts ) { |
| 28 | + header( 'HTTP/1.1 400 Bad Request', true, 400 ); |
| 29 | + die( '400 Bad Request' ); |
| 30 | +} else { |
| 31 | + $scripts = explode( ',', $scripts ); |
| 32 | +} |
| 33 | + |
| 34 | +// Sanitize input |
| 35 | +$error = false; |
| 36 | +$files = []; |
| 37 | +$type = null; |
| 38 | + |
| 39 | +foreach( $scripts as $script ) { |
| 40 | + $script = trim( $script, './' ); |
| 41 | + $ext = explode( '.', strtolower( $script ) ); |
| 42 | + if( $type !== null && $type != end( $ext ) ) { |
| 43 | + $error = true; |
| 44 | + } else { |
| 45 | + $type = end( $ext ); |
| 46 | + } |
| 47 | + if( !$disable_file_check && !file_exists( $basedir . $script ) ) $error = true; |
| 48 | + $files[] = $basedir . $script; |
| 49 | +} |
| 50 | + |
| 51 | +if( $error ) { |
| 52 | + header( 'HTTP/1.1 400 Bad Request', true, 400 ); |
| 53 | + die( '400 Bad Request' ); |
| 54 | +} |
| 55 | + |
| 56 | +// Combine scripts |
| 57 | +$output = ''; |
| 58 | +foreach( $files as $file ) { |
| 59 | + |
| 60 | + $output .= file_get_contents( $file ) . "\n"; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +// Minify output |
| 65 | +if( !$disable_minify ) { |
| 66 | + |
| 67 | + $minifier = null; |
| 68 | + if( $type = 'js' ) { |
| 69 | + $minifier = new Minify\JS(); |
| 70 | + } else if( $type = 'css' ) { |
| 71 | + $minifier = new Minify\CSS(); |
| 72 | + } |
| 73 | + |
| 74 | + if( $minifier ) { |
| 75 | + $minifier->add( $output ); |
| 76 | + $output = $minifier->minify(); |
| 77 | + } |
| 78 | + |
| 79 | + $output = "/**\n * Combined by php-combine-scripts v{$version}.\n * More information: https://github.com/dmhendricks/php-combine-scripts\n */\n" . $output; |
| 80 | + |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +// Send to browser |
| 85 | +$content_type = 'text/plain'; |
| 86 | +if( in_array( $type, [ 'js', 'css' ] ) ) { |
| 87 | + $content_type = $type == 'js' ? 'text/javascript' : 'text/css'; |
| 88 | +} |
| 89 | + |
| 90 | +header( "Content-Type: {$content_type}" ); |
| 91 | +echo $output; |
0 commit comments