This text is from my arcives. It may be out of date, but I thought it should be preserved.
This tool loads all files required by a project (including binaries) into one file.All unused functions will be removed, all function and variable names will be replaced
by shorter (and unreadable) ones, all not needed whitespaces will be removed, debugging code will be removed, ...
You'll get informed when a global or local variable is not used.
The resulting file is still a regular php-file. (No extra softeate is required!) Requirements: PHP 4.3.0 and up, CLI or CGI (developed under 5.0 beta)
You have to use it from a shell and it requires much memory. (The coding of twister [60kB code = 2k5 lines] needs about 8MB).
history |
|
|||||||||||||||
example |
Here is an basic example output. (currently n/a) Here is an complex -r2 example output. (currently n/a) |
|||||||||||||||
demo version |
Get version 0.9 tar.gz (about 20 KB) (Note: in the demo -n, -r2, -d4, -t, -S and -F are disabled) Please mail me, and tell me what do you think about it. |
|||||||||||||||
basics |
Usage: php twister.php <options...> <file> If <file> will be obmitted stdin will be used as source. All options have the syntax: -X value or -Xvalue. Some options are bit masks. Example usage: -X <1,2,4> you have to add the number of all features you want to use. For 1 and 4 use -X 5. To diable all -X 0. If a option is omitted the default will be used. (Either builtin or from a config-file, see -f.) Some functions are controlled by comments, you have to use exact that comment. (But you can add whitespaces outside of the comment like you want.) |
|||||||||||||||
quick |
Here is a quick overview about the options. -i <1>: include other code, 1=on, default=1 -d <1,2,4>: replace defines, 1=user, 2=system, 4=target, default=3 -t <target>: selects the specified target for -d4 -r <1,2>: remove unused code, 1=on, 2=optimize if, default=3 -p <1,2,4>: packit/fileloader, 1=on, 2=diable gzip, 4=disable base64, default=3 -n <1,2>: name substituation, 1=on, 2=string, default=3 -z <1,2>: compress output, 1=enable gzip, 2=enable base64, default=0 -c <1,2>: replace variable constants, 1=originals, 2=dummy, default=1 -f <config.conf>: read options from config.conf -I <path[:path[...]]>: define the include path, default=./ -o <out.php>: write final to out.php -1 <stage1.php>, -2 <stage2.php>, -3 <stage3.php>, -4 <stage4.php>, -S <scan.php>, -F <files.php>: debugging output |
|||||||||||||||
config |
The config-file contains the defaults for all options in the config: opts section. All other sections are described later. In the config: opts section is now a way to specify the input file. |
opts | ||||||||||||||
manual |
What do the colors mean? Command line options, References to the config-file, Example code, Things that don't work yet In the left column is the described option noted, in the right column the config-file. |
|||||||||||||||
Here is a detailed description, what the tool will do in which order, and what options can be used: | ||||||||||||||||
-f | First option: -f <config.conf> to load the default configuration from the file config.conf instead of using the builtin. (The file default.conf of the distribution contains the builtin configuration.) All options can be overwritten by the command line. | |||||||||||||||
The main script will be load and parsed. | ||||||||||||||||
-i |
Option: -i <1>. Now all files included by the main file will be also parsed and included. You have to use the command include, include_once, require, require_once with exactly one string as parameter, a trailing semicolon and NO whitespaces or comments. Example: include('file.php'); will work, include($file.'.php'); will not be replaced, cause the tool don't know what's in $file. To disable it for one include you may use include('file.php' );. See -F for debug. |
|||||||||||||||
-I |
Option: -i <path[:path:[...]]>. With this option you can set the include path which is used for the including -i. Multiple paths can be entered, seperated by a colon. The default path is ./, where ./ is the path of the file to twist, which must not be the actual directory. In case of your are using stdin the current directory will used. |
|||||||||||||||
-1 | If -1 <stage1.php> is used, the actual state will be dumped to stage1.php. | |||||||||||||||
-d |
The option -d <1,2,4> allows you to replace defines with their definitions.
|
target | ||||||||||||||
-c |
With the option -c <1,2> you can switch the replacement of variable constants on. Variable constants are: __FILE__, __LINE__, __FUNCTION__, __CLASS__, __METHOD__.
|
|||||||||||||||
-p |
The option -p <1,2,4> selects the packit functionality. A part of the code will be evaluated now and replaced by a packed version. Only strings and pure constant arrays can be packed. (For arrays (un)serialize will be used.) A gzip compression will be used if it decreases the length. At least base64 encoding is used, so it's a plain ASCII text. Code: $x = /*PACKIT*/ array(0=>'big array', 1=>'really big') will be replaced by $x = P(1, 'XXXX...') The name of the function is PHP_CODE_TWISTER_PACKIT_FUNCTION, not P, which will be created automatically. So the code is less readable.
|
|||||||||||||||
-p |
A second feature wich is also enabled by -p1 is the fileloader function. Code like $x = /*FILELOADER*/ file_get_contents('file.gif') will be replaced by the contents of the file, like packit: $x = P(1, 'XXXX...'). So now the file is IN your php program. Note: only a single constant string as parameter for file_get_contents is supported. Alternative fileloader usage: $x = /*PACKIT*/ array( filemtime('file.gif'), file_get_contents('file.gif') ) will be replaced with something like this: $x = array(105523325, P(1, 'XXXX...')). The switches 2 and 4 are also used by the fileloader. |
|||||||||||||||
-p | With /*PACKIT:X*/ and /*FILELOADER:X*/ you can set the -p options to X for this call. The option 1 is ignored for now, but should be set. Example: $x = /*PACKIT:7*/ 'Hello'." ".'World'."\n" will be packed, but neither gzip nor base64 encoded, the result is a single string. | |||||||||||||||
-2 | Now a second stage can be written on disk with -2 <stage2.php>. | |||||||||||||||
The following loop will remove unused code. | ||||||||||||||||
-r |
|
function class method member |
||||||||||||||
-3 | After finishing this loop a third stage can be written on disk with -3 <stage3.php>. | |||||||||||||||
-n |
|
superglobals globals local function class method member language |
||||||||||||||
-4 | Now a fourth stage can be written on disk with -4 <stage4.php>. | |||||||||||||||
-o -z |
Use -o <out.php> to write the resulting code without any comment and only a minimum of whitespaces to disk. The switch -z <1,2> contols the encoding of the output. The option 1 enables gzip compression, which makes the code much smaller. It requires gzip decompression in the php which should interpret your code. And it's not longer ASCII source, which may have some pitfalls. To make it ASCII compatible again, you may want to use option 2 to enable base64 encoding. | |||||||||||||||
Now we're trough. At last you can get two debugging files: | ||||||||||||||||
-F | With -F <files.php> will write a php code which contains an array of all files which are included or loaded while fileloader and a number which shows how often the file was loaded. | |||||||||||||||
-S | With -S <scan.php> will write a php code which contains an array of all name replacements which are made. |
0 Responses to “Code Twister”