forked from elijaa/phpmemcachedadmin
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild-phar.php
More file actions
108 lines (86 loc) · 3.32 KB
/
build-phar.php
File metadata and controls
108 lines (86 loc) · 3.32 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
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
<?php
$pharName = 'phpmemcachedadmin.phar';
$pharFile = __DIR__ . '/' . $pharName;
if (file_exists($pharFile)) {
unlink($pharFile);
}
$phar = new Phar($pharFile);
$phar->startBuffering();
// Add files from src, vendor, and other necessary files
// Excluding the phar script itself, git files, etc.
$phar->buildFromDirectory(__DIR__, '/^((?!build-phar\.php|build\.php|node_modules|\.git|\.idea|tests|docker).)*$/');
// Custom stub to handle web requests and static assets
$stub = <<<'EOT'
<?php
Phar::mapPhar();
$pharPath = 'phar://' . __FILE__;
// Basic mime type detection
function getMimeType($filename) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$mimes = [
'css' => 'text/css',
'js' => 'application/javascript',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
'ico' => 'image/x-icon',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'ttf' => 'font/ttf',
'eot' => 'application/vnd.ms-fontobject',
'html' => 'text/html',
];
return isset($mimes[$ext]) ? $mimes[$ext] : 'application/octet-stream';
}
// Handle web request
if (php_sapi_name() !== 'cli') {
$requestUri = $_SERVER['REQUEST_URI'] ?? '/';
// Strip query string
if (($pos = strpos($requestUri, '?')) !== false) {
$requestUri = substr($requestUri, 0, $pos);
}
// If the script is running as /phpmemcachedadmin.phar/some/path, we need to extract /some/path
$scriptName = $_SERVER['SCRIPT_NAME'];
if (strpos($requestUri, $scriptName) === 0) {
$path = substr($requestUri, strlen($scriptName));
} else {
$path = $requestUri;
}
// normalize path, so there always no start and end slashes
$path = trim($path, '/');
$fileName = basename($path);
if (!str_contains($fileName, '.')) {
// we can serve only files. If there is no dot in the path last segment, this is probably a directory. Let's
// assume the directory should contain an index.php file
$path .= '/index.php';
}
// Map to src/public
$internalPath = "src/public/$path";
$absolutePath = "$pharPath/$internalPath";
if (is_file($absolutePath)) {
// If it's a PHP file, include it (mainly for index.php)
if (pathinfo($absolutePath, PATHINFO_EXTENSION) === 'php') {
require $absolutePath;
} else {
// Serve static asset
header('Content-Type: ' . getMimeType($absolutePath));
readfile($absolutePath);
}
exit;
}
// Fallback to index.php for routing if file not found (optional, depending on app logic)
// But usually static assets should be found. If not, maybe it's a route handled by index.php?
// The original index.php seems to handle query params (?show=...), not path routing.
// So if we are here, it might be a 404 or a request for index.php with params.
require "$pharPath/src/public/index.php";
exit;
}
// CLI mode
require "$pharPath/src/public/index.php";
__HALT_COMPILER();
EOT;
$phar->setStub($stub);
$phar->stopBuffering();
echo "PHAR archive created successfully: $pharName\n";