forked from ircmaxell/PHPPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php.php
53 lines (42 loc) · 1.28 KB
/
php.php
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
<?php
error_reporting(E_ALL | E_STRICT);
require_once __DIR__ . '/vendor/autoload.php';
$php = new PHPPHP\PHP;
$php->registerExtensionByName('Shim'); // This *MUST* be the last core extension to be loaded!!!
list($options, $args) = parseCliArgs($argv);
if (isset($options['v'])) {
echo "PHPPHP - Dev Master\n";
} elseif (isset($options['f'])) {
$php->executeFile(realpath($options['f']));
} elseif (isset($options['r'])) {
$php->setCWD(getcwd());
$php->execute('<?php ' . $options['r']);
} elseif (isset($args[0])) {
$php->executeFile(realpath($args[0]));
} else {
echo "Invalid arguments\n";
}
function parseCliArgs(array $args) {
// first element is script name
array_shift($args);
$options = array();
$arguments = array();
$currentOption = null;
foreach ($args as $arg) {
if (strlen($arg) == 2 && $arg[0] == '-') {
if ($currentOption) {
$options[$currentOption] = '';
}
$currentOption = $arg[1];
} elseif ($currentOption) {
$options[$currentOption] = $arg;
$currentOption = null;
} else {
$arguments[] = $arg;
}
}
if ($currentOption) {
$options[$currentOption] = '';
}
return array($options, $arguments);
}