-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRunCommand.php
More file actions
78 lines (59 loc) · 2.5 KB
/
Copy pathRunCommand.php
File metadata and controls
78 lines (59 loc) · 2.5 KB
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
<?php
declare(strict_types=1);
namespace TypePHP\Command;
use TypePHP\TypePHP;
/**
* @internal Executes TypePHP CLI commands.
*/
final class RunCommand implements CommandInterface
{
private const VALID_PHP_EXTENSIONS = ['php', 'phtml', 'php5', 'php7', 'php8', 'phps'];
public function execute(array $args, $outputStream = STDOUT, $errorStream = STDERR): int
{
$c = [CliFormatter::class, 'color'];
$target = null;
$givenTargetCandidate = null;
foreach ($args as $arg) {
if (! str_starts_with($arg, '--') && ! str_starts_with($arg, '-')) {
$givenTargetCandidate = $arg;
$ext = strtolower(pathinfo($arg, PATHINFO_EXTENSION));
if ($ext !== '' && ! \in_array($ext, self::VALID_PHP_EXTENSIONS, strict: true)) {
fwrite($errorStream, "\n " . $c(' TYPEPHP ', 'badge_red') . ' ' . $c('Error', 'bold') . "\n\n");
fwrite($errorStream, ' ' . $c('✗', 'red') . ' Target file ' . $c('"' . $arg . '"', 'bold') . " is not a PHP script file. TypePHP can only execute PHP files.\n\n");
return 1;
}
if (file_exists($arg)) {
$target = $arg;
}
break;
}
}
if ($givenTargetCandidate !== null && $target === null) {
fwrite($errorStream, "\n " . $c(' TYPEPHP ', 'badge_red') . ' ' . $c('Error', 'bold') . "\n\n");
fwrite($errorStream, ' ' . $c('✗', 'red') . ' Target script file ' . $c('"' . $givenTargetCandidate . '"', 'bold') . " does not exist or is not readable.\n\n");
return 1;
}
if ($target === null) {
(new HelpCommand())->execute($args, $outputStream, $errorStream);
return 1;
}
try {
TypePHP::boot();
$realTarget = realpath($target);
if ($realTarget !== false) {
require $realTarget;
}
} catch (\Throwable $e) {
$class = \get_class($e);
$file = $e->getFile();
$line = $e->getLine();
$msg = $e->getMessage();
fwrite($errorStream, "PHP Fatal error: Uncaught {$class}: {$msg} in {$file} on line {$line}\n");
fwrite($errorStream, "Stack trace:\n");
fwrite($errorStream, "#0 {main}\n");
fwrite($errorStream, " thrown in {$file} on line {$line}\n");
return 255;
}
return 0;
}
}