|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Builds sci-profiler.phar from the src/ directory. |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * php -d phar.readonly=0 bin/build-phar.php |
| 10 | + * |
| 11 | + * The resulting phar can be used directly with auto_prepend_file: |
| 12 | + * php -d auto_prepend_file=bin/sci-profiler.phar -S localhost:8000 |
| 13 | + */ |
| 14 | + |
| 15 | +$projectRoot = dirname(__DIR__); |
| 16 | +$pharFile = $projectRoot . '/bin/sci-profiler.phar'; |
| 17 | +$srcDir = $projectRoot . '/src'; |
| 18 | +$configDir = $projectRoot . '/config'; |
| 19 | + |
| 20 | +// Clean previous build |
| 21 | +if (file_exists($pharFile)) { |
| 22 | + unlink($pharFile); |
| 23 | + echo "Removed previous build.\n"; |
| 24 | +} |
| 25 | + |
| 26 | +echo "Building sci-profiler.phar...\n"; |
| 27 | + |
| 28 | +try { |
| 29 | + $phar = new Phar($pharFile, 0, 'sci-profiler.phar'); |
| 30 | + $phar->startBuffering(); |
| 31 | + |
| 32 | + // Add all src/ files |
| 33 | + $srcIterator = new RecursiveIteratorIterator( |
| 34 | + new RecursiveDirectoryIterator($srcDir, FilesystemIterator::SKIP_DOTS), |
| 35 | + ); |
| 36 | + |
| 37 | + $count = 0; |
| 38 | + foreach ($srcIterator as $file) { |
| 39 | + if ($file->getExtension() !== 'php') { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + $relativePath = 'src/' . ltrim( |
| 44 | + str_replace($srcDir, '', $file->getPathname()), |
| 45 | + DIRECTORY_SEPARATOR, |
| 46 | + ); |
| 47 | + |
| 48 | + $phar->addFile($file->getPathname(), $relativePath); |
| 49 | + echo " + {$relativePath}\n"; |
| 50 | + $count++; |
| 51 | + } |
| 52 | + |
| 53 | + // Add default config |
| 54 | + $phar->addFile($configDir . '/sci-profiler.php', 'config/sci-profiler.php'); |
| 55 | + echo " + config/sci-profiler.php\n"; |
| 56 | + $count++; |
| 57 | + |
| 58 | + // Create the stub: when used as auto_prepend_file, it includes the bootstrap |
| 59 | + $stub = <<<'STUB' |
| 60 | +<?php |
| 61 | +/** |
| 62 | + * SCI Profiler PHP — phar stub. |
| 63 | + * |
| 64 | + * When loaded via auto_prepend_file, this registers the phar's autoloader |
| 65 | + * and executes the bootstrap to start profiling. |
| 66 | + */ |
| 67 | +Phar::mapPhar('sci-profiler.phar'); |
| 68 | +
|
| 69 | +// Register PSR-4 autoloader for classes inside the phar |
| 70 | +spl_autoload_register(static function (string $class): void { |
| 71 | + $prefix = 'SciProfiler\\'; |
| 72 | + if (strncmp($class, $prefix, strlen($prefix)) !== 0) { |
| 73 | + return; |
| 74 | + } |
| 75 | +
|
| 76 | + $relativeClass = substr($class, strlen($prefix)); |
| 77 | + $file = 'phar://sci-profiler.phar/src/' . str_replace('\\', '/', $relativeClass) . '.php'; |
| 78 | +
|
| 79 | + if (file_exists($file)) { |
| 80 | + require $file; |
| 81 | + } |
| 82 | +}); |
| 83 | +
|
| 84 | +// Load configuration (same logic as bootstrap.php, adapted for phar paths) |
| 85 | +$__sciConfig = (static function (): \SciProfiler\Config { |
| 86 | + // 1. Explicit config file via env |
| 87 | + $configFile = getenv('SCI_PROFILER_CONFIG_FILE'); |
| 88 | + if ($configFile !== false && is_file($configFile)) { |
| 89 | + return \SciProfiler\Config::fromFile($configFile); |
| 90 | + } |
| 91 | +
|
| 92 | + // 2. Config file next to the phar (bin/sci-profiler.local.php) |
| 93 | + $pharDir = dirname(Phar::running(false)); |
| 94 | + $localConfig = $pharDir . '/sci-profiler.local.php'; |
| 95 | + if ($pharDir !== '' && is_file($localConfig)) { |
| 96 | + return \SciProfiler\Config::fromFile($localConfig); |
| 97 | + } |
| 98 | +
|
| 99 | + // 3. Environment variables |
| 100 | + if (getenv('SCI_PROFILER_ENABLED') !== false) { |
| 101 | + return \SciProfiler\Config::fromEnvironment(); |
| 102 | + } |
| 103 | +
|
| 104 | + // 4. Default config bundled in the phar |
| 105 | + $pharConfig = 'phar://sci-profiler.phar/config/sci-profiler.php'; |
| 106 | + if (file_exists($pharConfig)) { |
| 107 | + return \SciProfiler\Config::fromFile($pharConfig); |
| 108 | + } |
| 109 | +
|
| 110 | + // 5. Built-in defaults |
| 111 | + return new \SciProfiler\Config(); |
| 112 | +})(); |
| 113 | +
|
| 114 | +if (!$__sciConfig->isEnabled()) { |
| 115 | + return; |
| 116 | +} |
| 117 | +
|
| 118 | +// Build and start profiler |
| 119 | +$__sciProfiler = new \SciProfiler\SciProfiler($__sciConfig); |
| 120 | +$__sciProfiler->addCollector(new \SciProfiler\Collector\TimeCollector()); |
| 121 | +$__sciProfiler->addCollector(new \SciProfiler\Collector\MemoryCollector()); |
| 122 | +$__sciProfiler->addCollector(new \SciProfiler\Collector\RequestCollector()); |
| 123 | +
|
| 124 | +$__sciReporterMap = [ |
| 125 | + 'json' => static fn () => new \SciProfiler\Reporter\JsonReporter(), |
| 126 | + 'log' => static fn () => new \SciProfiler\Reporter\LogReporter(), |
| 127 | + 'html' => static fn () => new \SciProfiler\Reporter\HtmlReporter(), |
| 128 | +]; |
| 129 | +foreach ($__sciConfig->getReporters() as $__rName) { |
| 130 | + $__rName = trim($__rName); |
| 131 | + if (isset($__sciReporterMap[$__rName])) { |
| 132 | + $__sciProfiler->addReporter($__sciReporterMap[$__rName]()); |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | +$__sciProfiler->start(); |
| 137 | +
|
| 138 | +register_shutdown_function(static function () use ($__sciProfiler): void { |
| 139 | + if ($__sciProfiler->isStarted()) { |
| 140 | + $__sciProfiler->stop(); |
| 141 | + } |
| 142 | +}); |
| 143 | +
|
| 144 | +// Cleanup variables from global scope |
| 145 | +unset($__sciConfig, $__sciReporterMap, $__rName); |
| 146 | +
|
| 147 | +__HALT_COMPILER(); |
| 148 | +STUB; |
| 149 | + |
| 150 | + $phar->setStub($stub); |
| 151 | + $phar->stopBuffering(); |
| 152 | + |
| 153 | + // Make executable |
| 154 | + chmod($pharFile, 0755); |
| 155 | + |
| 156 | + $size = filesize($pharFile); |
| 157 | + $sizeKb = round($size / 1024, 1); |
| 158 | + |
| 159 | + echo "\nBuild complete:\n"; |
| 160 | + echo " File: {$pharFile}\n"; |
| 161 | + echo " Size: {$sizeKb} KB\n"; |
| 162 | + echo " Files: {$count}\n"; |
| 163 | + echo "\nUsage:\n"; |
| 164 | + echo " php -d auto_prepend_file={$pharFile} -S localhost:8000 -t public/\n"; |
| 165 | +} catch (Exception $e) { |
| 166 | + echo "ERROR: " . $e->getMessage() . "\n"; |
| 167 | + exit(1); |
| 168 | +} |
0 commit comments