summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--concat/concat.php77
-rwxr-xr-xconcat/test.sh2
-rw-r--r--concat/testfiles/a.php1
-rw-r--r--concat/testfiles/b.php5
-rw-r--r--concat/testfiles/c.php2
-rw-r--r--concat/testfiles/result.php12
6 files changed, 99 insertions, 0 deletions
diff --git a/concat/concat.php b/concat/concat.php
new file mode 100644
index 0000000..6594fd9
--- /dev/null
+++ b/concat/concat.php
@@ -0,0 +1,77 @@
+<?php
+define('DEBUG_TOKENS', false);
+$deferred_close_tag = '';
+for ($argi=1; $argi<$argc; $argi++) {
+ // Parse current arg file's content
+ $filepath = $argv[$argi];
+ $src = file_get_contents($filepath) or die("Can't read '$filepath'".PHP_EOL);
+ $tokens = token_get_all($src) or die("Can't parse '$filepath'".PHP_EOL);
+ $tcount = count($tokens);
+
+ // Process all tokens for the current file
+ if ( DEBUG_TOKENS ) fwrite(STDERR, "file $filepath ($tcount)".PHP_EOL);
+ $in_php = false;
+ $file_first_open_tag = true;
+ $token_to_add_after = '';
+ $token_to_add_before = $deferred_close_tag;
+ $deferred_close_tag = '';
+ for ($i=0; $i<$tcount; $i++) {
+ $token = $tokens[$i];
+ if ( DEBUG_TOKENS ) fwrite(STDERR, (is_array($token)?token_name($token[0])." $token[1]":"LITTERAL $token").PHP_EOL);
+ // Manipulate tokens
+ switch ( is_array($token)?$token[0]:$token ) {
+ case T_CLOSE_TAG:
+ $in_php = false;
+ // if close tag as last token, defer it, will be processed at first token of next file
+ if ( $i == ($tcount-1) ) {
+ $deferred_close_tag = $token;
+ $token = '';
+ }
+ break;
+ case T_OPEN_TAG:
+ case T_OPEN_TAG_WITH_ECHO:
+ $in_php = true;
+ // insert a "#line" annotation in output at first php block of each source file
+ if ( $file_first_open_tag ) {
+ $line = $token[2];
+ // insert carriage return after this open tag if not already there
+ if ( strrpos($token[1], PHP_EOL) !== (strlen($token[1])-1) ) {
+ $comment = PHP_EOL;
+ } else {
+ $comment = '';
+ $line++;
+ }
+ $comment .= "#line $line $filepath".PHP_EOL;
+ $token_to_add_after = [ T_COMMENT, $comment, $line ];
+ $file_first_open_tag = false;
+ }
+ // if first token in current file is T_OPEN_TAG and a previous T_CLOSE_TAG is pending
+ if ( ( $i == 0 ) && is_array($token_to_add_before) && $token_to_add_before[0]==T_CLOSE_TAG ) {
+ // Strip previous file T_CLOSE_TAG
+ $token_to_add_before = '';
+ // and current file T_OPEN_TAG
+ $token = '';
+ }
+ break;
+ } /* switch */
+
+ // Output current token with additions and reset them
+ foreach ( [$token_to_add_before, $token, $token_to_add_after] as $t ) {
+ echo is_array($t)?$t[1]:$t;
+ }
+ $token_to_add_before = '';
+ $token_to_add_after = '';
+
+ } /* foreach $tokens */
+
+ // Add implicit T_CLOSE_TAG at end of file and if we are still $in_php
+ if ( $in_php ) {
+ $deferred_close_tag = [ T_CLOSE_TAG, '?>'.PHP_EOL, 0 ];
+ }
+
+} /* foreach args */
+
+/* Optionnal : output T_CLOSE_TAG if was deferred and it's the last file
+if ( is_array($deferred_close_tag) ) {
+ echo $deferred_close_tag[1];
+}*/
diff --git a/concat/test.sh b/concat/test.sh
new file mode 100755
index 0000000..7aab13f
--- /dev/null
+++ b/concat/test.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+diff -sy <(php concat.php testfiles/?.php) testfiles/result.php
diff --git a/concat/testfiles/a.php b/concat/testfiles/a.php
new file mode 100644
index 0000000..068821b
--- /dev/null
+++ b/concat/testfiles/a.php
@@ -0,0 +1 @@
+<?=print_r($_GET,true);
diff --git a/concat/testfiles/b.php b/concat/testfiles/b.php
new file mode 100644
index 0000000..fdb397e
--- /dev/null
+++ b/concat/testfiles/b.php
@@ -0,0 +1,5 @@
+HTML
+<?php
+ echo "hello"; ?>HTML2
+HTML3
+<?php
diff --git a/concat/testfiles/c.php b/concat/testfiles/c.php
new file mode 100644
index 0000000..c222699
--- /dev/null
+++ b/concat/testfiles/c.php
@@ -0,0 +1,2 @@
+<?php
+echo "test\n";
diff --git a/concat/testfiles/result.php b/concat/testfiles/result.php
new file mode 100644
index 0000000..1f2cea4
--- /dev/null
+++ b/concat/testfiles/result.php
@@ -0,0 +1,12 @@
+<?=
+#line 1 testfiles/a.php
+print_r($_GET,true);
+?>
+HTML
+<?php
+#line 3 testfiles/b.php
+ echo "hello"; ?>HTML2
+HTML3
+<?php
+#line 2 testfiles/c.php
+echo "test\n";