|
| 1 | +<?php |
| 2 | + |
| 3 | +include dirname(__FILE__) . "/../lib/php/autoload.php"; |
| 4 | + |
| 5 | +use SDK\Config; |
| 6 | +use SDK\Exception; |
| 7 | +use SDK\Build\PGO\Controller; |
| 8 | + |
| 9 | +$sopt = "itudhs:fr"; |
| 10 | +$lopt = array("init", "train", "up", "down", "help", "scenario:", "force", "ready"); |
| 11 | + |
| 12 | +$cmd = NULL; |
| 13 | +/* TODO For now we simply check the current php build, this could be extended to take arbitrary binaries. */ |
| 14 | +$deps_root = NULL; |
| 15 | +$php_root = NULL; |
| 16 | +$scenario = NULL; |
| 17 | +$force = false; |
| 18 | + |
| 19 | +try { |
| 20 | + $opt = getopt($sopt, $lopt); |
| 21 | + foreach ($opt as $name => $val) { |
| 22 | + switch ($name) { |
| 23 | + case "i": |
| 24 | + case "init": |
| 25 | + $cmd = "init"; |
| 26 | + break; |
| 27 | + case "ready": |
| 28 | + $cmd = "check_init"; |
| 29 | + break; |
| 30 | + case "t": |
| 31 | + case "train": |
| 32 | + $cmd = "train"; |
| 33 | + break; |
| 34 | + case "u": |
| 35 | + case "up": |
| 36 | + $cmd = "up"; |
| 37 | + break; |
| 38 | + case "d": |
| 39 | + case "down": |
| 40 | + $cmd = "down"; |
| 41 | + break; |
| 42 | + case "s": |
| 43 | + case "scenario": |
| 44 | + $scenario = $val; |
| 45 | + break; |
| 46 | + case "f": |
| 47 | + case "force": |
| 48 | + $force = true; |
| 49 | + break; |
| 50 | + case "h": case "help": |
| 51 | + usage(0); |
| 52 | + break; |
| 53 | + |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (NULL === $cmd) { |
| 58 | + usage(); |
| 59 | + } |
| 60 | + |
| 61 | + $deps_root = Config::getDepsLocalPath(); |
| 62 | + |
| 63 | + if ("check_init" != $cmd) { |
| 64 | + /* XXX Need these checks for more safety, as long as the dist zipballs are not supported. */ |
| 65 | + if (!file_exists("Makefile")) { |
| 66 | + throw new Exception("Makefile not found. Arbitrary php snapshots are not supported yet, switch to the php source dir."); |
| 67 | + } |
| 68 | + if (preg_match(",BUILD_DIR=(.+),", file_get_contents("Makefile"), $m)) { |
| 69 | + $php_root = trim($m[1]); |
| 70 | + } |
| 71 | + if (!$php_root || !file_exists($php_root)) { |
| 72 | + throw new Exception("Invalid php root dir encountered '$php_root'."); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + $controller = new Controller($cmd, $scenario); |
| 77 | + $controller->handle($force); |
| 78 | + |
| 79 | + if ("check_init" == $cmd) { |
| 80 | + /* 0 for success, fail otherwise. */ |
| 81 | + $ret = ($controller->isInitialized() === false); |
| 82 | + exit((int)$ret); |
| 83 | + } |
| 84 | + |
| 85 | + /*$env = getenv(); |
| 86 | + $env["PATH"] = $deps_root . DIRECTORY_SEPARATOR . "bin;" . $env["PATH"]; |
| 87 | +
|
| 88 | + $php = $php_root . DIRECTORY_SEPARATOR . "php.exe"; |
| 89 | + $php = $php_root . DIRECTORY_SEPARATOR . "php.exe";*/ |
| 90 | + |
| 91 | +} catch (Throwable $e) { |
| 92 | + throw $e; |
| 93 | + exit(3); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +function usage(int $code = -1) |
| 98 | +{ |
| 99 | + echo "PHP SDK PGO training tool.", PHP_EOL; |
| 100 | + echo "Usage: ", PHP_EOL, PHP_EOL; |
| 101 | + echo "Commands:", PHP_EOL; |
| 102 | + echo " -i --init Initialize training environment.", PHP_EOL; |
| 103 | + echo " -t --train Run training. This involves startup, training and shutdown.", PHP_EOL; |
| 104 | + echo " -u --up Startup training environment.", PHP_EOL; |
| 105 | + echo " -d --down Shutdown training environment.", PHP_EOL; |
| 106 | + echo " -f --force Force requested operation. Not every option can be forced.", PHP_EOL; |
| 107 | + echo " -s --scenario Run training with a specified scenario.", PHP_EOL; |
| 108 | + |
| 109 | + /*echo " -p --php-root PHP binary to train.", PHP_EOL;*/ |
| 110 | + |
| 111 | + $code = -1 == $code ? 0 : $code; |
| 112 | + exit($code); |
| 113 | +} |
| 114 | + |
| 115 | +function msg(string $s, int $code = 0) { |
| 116 | + echo $s, PHP_EOL; |
| 117 | + exit($code); |
| 118 | +} |
| 119 | + |
| 120 | +/* |
| 121 | + * Local variables: |
| 122 | + * tab-width: 4 |
| 123 | + * c-basic-offset: 4 |
| 124 | + * End: |
| 125 | + * vim600: sw=4 ts=4 fdm=marker |
| 126 | + * vim<600: sw=4 ts=4 |
| 127 | + */ |
0 commit comments