Make bootstrap a require_once... for now.
[htmlpurifier/bfroehle.git] / maintenance / generate-includes.php
blobaeb863bfd574d5aa894781f913b644bd4e6daeb5
1 #!/usr/bin/php
2 <?php
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 require_once '../tests/path2class.func.php';
7 require_once '../library/HTMLPurifier/Bootstrap.php';
8 assertCli();
10 /**
11 * @file
12 * Generates an include stub for users who do not want to use the autoloader.
15 chdir(dirname(__FILE__) . '/../library/');
16 $FS = new FSTools();
18 $exclude_dirs = array(
19 'HTMLPurifier/Language/',
20 'HTMLPurifier/Filter/',
21 'HTMLPurifier/ConfigDef/', // specially handled, remove this once fixed!
23 $exclude_files = array(
24 'HTMLPurifier/Lexer/PEARSax3.php',
25 'HTMLPurifier/Lexer/PH5P.php',
26 'HTMLPurifier/Bootstrap.php',
27 'HTMLPurifier/ConfigDef.php', // specially handled, remove this once fixed!
30 // Determine what files need to be included:
31 $raw_files = $FS->globr('.', '*.php');
32 $files = array();
33 foreach ($raw_files as $file) {
34 $file = substr($file, 2); // rm leading './'
35 if (strncmp('standalone/', $file, 11) === 0) continue; // rm generated files
36 if (substr_count($file, '.') > 1) continue; // rm meta files
37 $ok = true;
38 foreach ($exclude_dirs as $dir) {
39 if (strncmp($dir, $file, strlen($dir)) === 0) {
40 $ok = false;
41 break;
44 if (!$ok) continue; // rm excluded directories
45 if (in_array($file, $exclude_files)) continue; // rm excluded files
46 $files[] = $file;
49 // Reorder list so that dependencies are included first:
51 /**
52 * Returns a lookup array of dependencies for a file.
54 * @note This function expects that format $name extends $parent on one line
56 * @param $file
57 * File to check dependencies of.
58 * @return
59 * Lookup array of files the file is dependent on, sorted accordingly.
61 function get_dependency_lookup($file) {
62 static $cache = array();
63 if (isset($cache[$file])) return $cache[$file];
64 $fh = fopen($file, 'r');
65 $deps = array();
66 while (!feof($fh)) {
67 $line = fgets($fh);
68 if (strncmp('HTMLPurifier_ConfigSchema', $line, 25) === 0) {
69 $deps['HTMLPurifier/ConfigSchema.php'] = true;
71 if (strncmp('class', $line, 5) === 0) {
72 // The implementation here is fragile and will break if we attempt
73 // to use interfaces. Beware!
74 list(, $parent) = explode(' extends ', trim($line, ' {'."\n\r"), 2);
75 if (empty($parent)) break;
76 $dep_file = HTMLPurifier_Bootstrap::getPath($parent);
77 if (!$dep_file) break;
78 $deps[$dep_file] = true;
79 break;
82 fclose($fh);
83 foreach (array_keys($deps) as $file) {
84 // Extra dependencies must come *before* base dependencies
85 $deps = get_dependency_lookup($file) + $deps;
87 $cache[$file] = $deps;
88 return $deps;
91 /**
92 * Sorts files based on dependencies. This function is lazy and will not
93 * group files with dependencies together; it will merely ensure that a file
94 * is never included before its dependencies are.
96 * @param $files
97 * Files array to sort.
98 * @return
99 * Sorted array ($files is not modified by reference!)
101 function dep_sort($files) {
102 $ret = array();
103 $cache = array();
104 foreach ($files as $file) {
105 if (isset($cache[$file])) continue;
106 $deps = get_dependency_lookup($file);
107 foreach (array_keys($deps) as $dep) {
108 if (!isset($cache[$dep])) {
109 $ret[] = $dep;
110 $cache[$dep] = true;
113 $cache[$file] = true;
114 $ret[] = $file;
116 return $ret;
119 $files = dep_sort($files);
121 // Build the actual include stub:
123 $version = trim(file_get_contents('../VERSION'));
124 $php = "<?php
127 * @file
128 * This file was auto-generated by generate-includes.php and includes all of
129 * the core files required by HTML Purifier. Use this if performance is a
130 * primary concern and you are using an opcode cache. PLEASE DO NOT EDIT THIS
131 * FILE, changes will be overwritten the next time the script is run.
133 * @version $version
135 * @warning
136 * You must *not* include any other HTML Purifier files before this file,
137 * because 'require' not 'require_once' is used.
139 * @warning
140 * This file requires that the include path contains the HTML Purifier
141 * library directory; this is not auto-set.
144 // Treat this file specially, as it is detached from the rest of the library
145 require_once 'HTMLPurifier/Bootstrap.php';
149 foreach ($files as $file) {
150 $php .= "require '$file';" . PHP_EOL;
153 file_put_contents('HTMLPurifier.includes.php', $php);