0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 17:01:51 +00:00
htmlpurifier/maintenance/generate-includes.php
Edward Z. Yang 522c8ed7c2 [3.1.0] The bulk of autoload support added
- Add FSTools:globr()
- require_once removed from all files
- HTMLPurifier.autoload.php added to register autoload handler
- Removed redundant chdir in maintenance script
- Modified standalone to use HTMLPurifier.includes.php for including stuff
- Added maintenance script remove-require-once.php which we used once and should never use again

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1516 48356398-32a2-884e-a903-53898d9a118a
2008-01-27 01:54:41 +00:00

150 lines
4.3 KiB
PHP

#!/usr/bin/php
<?php
chdir(dirname(__FILE__));
require_once 'common.php';
require_once '../tests/path2class.func.php';
require_once '../library/HTMLPurifier/Bootstrap.php';
assertCli();
/**
* @file
* Generates an include stub for users who do not want to use the autoloader.
*/
chdir(dirname(__FILE__) . '/../library/');
$FS = new FSTools();
$exclude_dirs = array(
'HTMLPurifier/Language/',
'HTMLPurifier/Filter/',
'HTMLPurifier/ConfigDef/', // specially handled, remove this once fixed!
);
$exclude_files = array(
'HTMLPurifier/Lexer/PEARSax3.php',
'HTMLPurifier/Lexer/PH5P.php',
'HTMLPurifier/ConfigDef.php', // specially handled, remove this once fixed!
);
// Determine what files need to be included:
$raw_files = $FS->globr('.', '*.php');
$files = array();
foreach ($raw_files as $file) {
$file = substr($file, 2); // rm leading './'
if (strncmp('standalone/', $file, 11) === 0) continue; // rm generated files
if (substr_count($file, '.') > 1) continue; // rm meta files
$ok = true;
foreach ($exclude_dirs as $dir) {
if (strncmp($dir, $file, strlen($dir)) === 0) {
$ok = false;
break;
}
}
if (!$ok) continue; // rm excluded directories
if (in_array($file, $exclude_files)) continue; // rm excluded files
$files[] = $file;
}
// Reorder list so that dependencies are included first:
/**
* Returns a lookup array of dependencies for a file.
*
* @note This function expects that format $name extends $parent on one line
*
* @param $file
* File to check dependencies of.
* @return
* Lookup array of files the file is dependent on, sorted accordingly.
*/
function get_dependency_lookup($file) {
static $cache = array();
if (isset($cache[$file])) return $cache[$file];
$fh = fopen($file, 'r');
$deps = array();
while (!feof($fh)) {
$line = fgets($fh);
if (strncmp('HTMLPurifier_ConfigSchema', $line, 25) === 0) {
$deps['HTMLPurifier/ConfigSchema.php'] = true;
}
if (strncmp('class', $line, 5) === 0) {
// The implementation here is fragile and will break if we attempt
// to use interfaces. Beware!
list(, $parent) = explode(' extends ', trim($line, ' {'."\n\r"), 2);
if (empty($parent)) break;
$dep_file = HTMLPurifier_Bootstrap::getPath($parent);
if (!$dep_file) break;
$deps[$dep_file] = true;
break;
}
}
fclose($fh);
foreach (array_keys($deps) as $file) {
// Extra dependencies must come *before* base dependencies
$deps = get_dependency_lookup($file) + $deps;
}
$cache[$file] = $deps;
return $deps;
}
/**
* Sorts files based on dependencies. This function is lazy and will not
* group files with dependencies together; it will merely ensure that a file
* is never included before its dependencies are.
*
* @param $files
* Files array to sort.
* @return
* Sorted array ($files is not modified by reference!)
*/
function dep_sort($files) {
$ret = array();
$cache = array();
foreach ($files as $file) {
if (isset($cache[$file])) continue;
$deps = get_dependency_lookup($file);
foreach (array_keys($deps) as $dep) {
if (!isset($cache[$dep])) {
$ret[] = $dep;
$cache[$dep] = true;
}
}
$cache[$file] = true;
$ret[] = $file;
}
return $ret;
}
$files = dep_sort($files);
// Build the actual include stub:
$version = trim(file_get_contents('../VERSION'));
$php = "<?php
/**
* @file
* This file was auto-generated by generate-includes.php and includes all of
* the core files required by HTML Purifier. Use this if performance is a
* primary concern and you are using an opcode cache. PLEASE DO NOT EDIT THIS
* FILE, changes will be overwritten the next time the script is run.
*
* @version $version
*
* @warning
* You must *not* include any other HTML Purifier files before this file,
* because 'require' not 'require_once' is used.
*
* @warning
* This file requires that the include path contains the HTML Purifier
* library directory; this is not auto-set.
*/
";
foreach ($files as $file) {
$php .= "require '$file';" . PHP_EOL;
}
file_put_contents('HTMLPurifier.includes.php', $php);