cli_compiler.php 6.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#!/usr/bin/php

Welcome to the interactive Doctrine Compiler 0.0.1 (Beta).

WARNING: You're on your own - this script modifies your
filesystem and has not been tested on Windows (yet).
<?php
if (in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>

Usage:
  <?php echo $argv[0]; ?> [path]

  [path] is the path to Doctrine's lib directory. This
  is normally the directory containing the core
  Doctrine class, 'Doctrine.php'.

While the program is running:

  Default responses to questions are capitalized. Just
  hit enter to provide a question's default response.

  You can quit the program at any time by pressing 'q'.

<?php
    exit;
}

if ($argc > 1) {
    $doctrinePath = $argv[1];
}

$drivers = array(
    'Db2',
    'Firebird',
    'Informix',
    'Mssql',
    'Mysql',
    'Oracle',
    'Pgsql',
    'Sqlite'
);

function addIncludePath($path) {
    set_include_path(
        $path . PATH_SEPARATOR . get_include_path()
    );
}

function showMessage($message) {
    echo "\n$message\n";
}

function quit($message = 'Program Aborted.') {
    if ($message) {
        showMessage($message);
    }
    exit;
}

function autoQuit($answer) {
    if (trim(strtolower($answer)) == 'q') {
        quit();
    }
}

function ask(
    $question,
    $defaultOption = 'y',
    $displayOptions = array('yes', 'no', 'quit'),
    $autoValidate = true)
{
    $prompt = "\n$question";
    if ($displayOptions) {
        $optionCount = count($displayOptions);
        if ($defaultOption) {
            for ($i = 0; $i < $optionCount; $i++) {
                if ($displayOptions[$i][0] == $defaultOption) {
                    $displayOptions[$i] = ucfirst($displayOptions[$i]);
                    break;
                }
            }
        }
        if ($autoValidate) {
            for ($i = 0; $i < $optionCount; $i++) {
                $validation[] = strtolower($displayOptions[$i][0]);
            }
        }
        $prompt .= "\n[" . implode('/', $displayOptions) . "] ";
    }
    $done = false;
    while (!$done) {
        echo $prompt;
        $input = trim(fgets(STDIN));
        if (!$input && $defaultOption) {
            $input = $defaultOption;
        }
        if (isset($validation)) {
            if ($input) {
                $input = strtolower($input[0]);
            }
            if (array_search($input, $validation) !== false) {
                return $input;
            } else {
                echo "\n" . 'The answer "' . $input . '" is invalid.  It must be one of "' . implode('", "', $validation) . '".' . "\n";
            }
        } else {
            return $input;
        }
    }
}

// Enable error reporting
if (($answer = ask("Would you like to turn on error reporting?", 'n')) == 'y') {
    error_reporting(E_ALL);
} else {
    error_reporting(0);
}
autoQuit($answer);

// Get Doctrine's library path
$usablePath = false;
while (!$usablePath) {
    if (!isset($doctrinePath)) {
        $doctrinePath = dirname(__FILE__);
    }
    $path = ask("Please enter the path to Doctrine's lib directory:", $doctrinePath, array($doctrinePath), false);
    autoQuit($path);
    try {
        addIncludePath($path);
        include_once 'Doctrine.php';
        spl_autoload_register(array('Doctrine', 'autoload'));
        $usablePath = true;
    } catch (Exception $e) {
        showMessage("The path '$path' does not seem to contain the expected Doctrine class ('Doctrine.php').");
        if (($answer = ask("Would you like to specify another path?")) != 'y') {
            quit();
        }
    }
}

$includeDrivers = array();
$excludeDrivers = array();

// Determine driver support
foreach ($drivers as $driver) {
    if (($answer = ask("Would you like to enable support for $driver?")) == 'y') {
        $includeDrivers[] = $driver;
    } else {
        $excludeDrivers[] = $driver;
    }
    autoQuit($answer);
}

// Verify driver support
if (!count($includeDrivers)) {
    showMessage("You have not selected any drivers. Normally this is not a good idea, unless you know what you're doing.");
    if (($answer = ask("Are you sure you want to compile without any drivers?")) != 'y') {
        quit();
    }
    autoQuit($answer);
}

// 'Compile' Doctrine...
showMessage("Trying to set the PHP memory limit to 18MB...");
ini_set('memory_limit', '18M');
if (ini_get('memory_limit') != '18M') {
    showMessage("WARNING: The program could not set the PHP memory limit to 18MB. The compilation might fail.");
    if (($answer = ask("Do you still want to continue?")) != 'y') {
        quit;
    }
} else {
    showMessage("PHP memory limit adjusted successfully.");
}


$target = './Doctrine.compiled.php';

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);

foreach ($it as $file) {
    $e = explode('.', $file->getFileName());

    // we don't want to require versioning files
    if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
        require_once $file->getPathName();
    }
}

$classes = array_merge(get_declared_classes(), get_declared_interfaces());

$ret     = array();

foreach ($classes as $class) {
    $e = explode('_', $class);

    if ($e[0] !== 'Doctrine') {
        continue;
    }

    // Skip excluded drivers
    $skipClass = false;
    foreach ($excludeDrivers as $excludeDriver) {
        if (in_array($excludeDriver, $e)) {
            $skipClass = true;
            break;
        }
    }
    if ($skipClass) {
        echo "\nExcluding -> $class";
        continue;
    }

    $refl  = new ReflectionClass($class);
    $file  = $refl->getFileName();

    echo "\nIncluding -> $file";

    $lines = file($file);

    $start = $refl->getStartLine() - 1;
    $end   = $refl->getEndLine();

    $ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
}

if ($target == null) {
    $target = $path . DIRECTORY_SEPARATOR . 'Doctrine.compiled.php';
}

// first write the 'compiled' data to a text file, so
// that we can use php_strip_whitespace (which only works on files)
$fp = @fopen($target, 'w');

if ($fp === false) {
    throw new Exception("Couldn't write compiled data. Failed to open $target");
}
fwrite($fp, "<?php ". implode('', $ret));
fclose($fp);

$stripped = php_strip_whitespace($target);
$fp = @fopen($target, 'w');
if ($fp === false) {
    throw new Exception("Couldn't write compiled data. Failed to open $file");
}
fwrite($fp, $stripped);
fclose($fp);

// Say bye...
showMessage("\nCompilation Finished.");
showMessage("Thank you for using the interactive Doctrine Compiler. Have fun following the Doctrine :)\n");


?>