Skip to content

Commit bf87277

Browse files
committed
Initial commit
1 parent 8d5f2af commit bf87277

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

.env-sample

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
COMBINE_BASEDIR=/app/public/
2+
COMBINE_DISABLE_FILE_EXISTS=false
3+
COMBINE_DISABLE_MINIFY=false

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# General
2+
__MACOSX
3+
**.DS_Store*
4+
ehthumbs.db
5+
Thumbs.db
6+
*~
7+
._*
8+
*.un~
9+
*.bak
10+
*.swp
11+
*.log
12+
.env
13+
.idea
14+
15+
# Development
16+
*.phprj
17+
*.scssc
18+
vendor/
19+
yarn.lock
20+
composer.lock
21+
package-lock.json
22+
23+
# VS Code
24+
launch.json
25+
*.code-workspace

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# PHP Combine Scripts
2+
3+
:construction: This is a work-in-progress. Inspired by [jsDelivr](https://www.jsdelivr.com/features#combine), it can be used to combine scripts passed to it via URI.

combine.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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;

composer.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "dmhendricks/php-combine-scripts",
3+
"description": "A simple PHP script to combine script assets",
4+
"keywords": [ "combine", "css", "js", "scripts" ],
5+
"homepage": "https://github.com/dmhendricks/php-combine-scripts",
6+
"license": "Apache-2.0",
7+
"require": {
8+
"matthiasmullie/minify": "^1.3",
9+
"vlucas/phpdotenv": "^3.4",
10+
"composer/installers": "~1.0.6"
11+
},
12+
"scripts": {
13+
"pre-autoload-dump": [
14+
"find ./vendor -name '.git*' -exec rm -rf {} +"
15+
]
16+
}
17+
}

0 commit comments

Comments
 (0)