diff --git a/compile b/compile index 26c94bc..b23eb21 100755 --- a/compile +++ b/compile @@ -33,9 +33,9 @@ if ($config['debug']) { $build = new Build( sourceDirectory: __DIR__ . '/loader', buildDirectory: __DIR__ . '/build', - pharFilename: 'spip_loader.php' + pharFilename: 'spip_loader.php', + debug: $config['debug'], ); - $build->prepare(); $build->run(); $build->showResults(); diff --git a/compiler/Build.php b/compiler/Build.php index 94f51b0..ac3f373 100644 --- a/compiler/Build.php +++ b/compiler/Build.php @@ -15,6 +15,7 @@ class Build { private string $buildDirectory, private string $pharFilename, private Timer $timer = new Timer(), + private bool $debug = false, ) { } @@ -41,6 +42,7 @@ class Build { $phar = new PharArchive( sourceDirectory: $this->sourceDirectory, buildDirectory: $this->buildDirectory, + debug: $this->debug, ); $n = $phar->build( $git->getVersion(), diff --git a/compiler/PharArchive.php b/compiler/PharArchive.php index 170fb9c..3750753 100644 --- a/compiler/PharArchive.php +++ b/compiler/PharArchive.php @@ -13,7 +13,8 @@ class PharArchive { public function __construct( private string $sourceDirectory, private string $buildDirectory, - private string $filename = self::FILENAME + private string $filename = self::FILENAME, + private bool $debug = false, ) { } @@ -59,10 +60,7 @@ class PharArchive { $phar = new \Phar($destFile, 0, $this->filename); $phar->setSignatureAlgorithm(\Phar::SHA512); $phar->startBuffering(); - $stub = file_get_contents($this->sourceDirectory . '/phar/stub.php'); - $stub = $this->stripWhitespacePhpFile($stub); - $stub = str_replace("setStub($stub); + $phar->setStub($this->getStub($private_version)); $phar['/version.php'] = "buildDirectory . '/' . self::FILENAME, $this->buildDirectory . '/' . $filename); } + private function getStub(string $version): string { + $stub = file_get_contents($this->sourceDirectory . '/phar/stub.php'); + #$stub = $this->stripWhitespacePhpFile($stub); + $stub = str_replace("debug, true) . ";\n", $stub); + return $stub; + } + /** * Removes whitespace from a PHP source string while preserving line numbers. * diff --git a/loader/phar/stub.php b/loader/phar/stub.php index 9d360b0..84052b7 100644 --- a/loader/phar/stub.php +++ b/loader/phar/stub.php @@ -1,25 +1,41 @@ 'image/svg+xml' - ], - function ($path) { - # dump([ '$path' => $path, 'PATH_INFO' => $_SERVER['PATH_INFO'], 'REQUEST_URI' => $_SERVER['REQUEST_URI'], 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], 'PHP_SELF' => $_SERVER['PHP_SELF'], 'HTTP_HOST' => $_SERVER['HTTP_HOST'], 'Phar::running(false)' => \Phar::running(false) ]); + if (!http_response_code()) { + require_once 'phar://spip_loader.phar/bin/console.php'; + exit(0); + } + + function spip_loader_phar_path($path) { if (!$path) { $path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; } + if (!$path) { + $path = isset($_SERVER['REQUEST_URI']) && isset($_SERVER['PHP_SELF']) + ? substr($_SERVER['REQUEST_URI'], strlen($_SERVER['PHP_SELF'])) + : ''; + } + return $path; + } + + Phar::webPhar('spip_loader.phar', null, null, ['svg' => 'image/svg+xml'], function ($incomming_path) use ($debug) { + $path = spip_loader_phar_path($incomming_path); + if ($debug) { + register_shutdown_function(function() use ($incomming_path, $path) { + $debug = new Spip\Loader\Debug(); + echo $debug->table($debug->phar_rewrite_debug($incomming_path, $path)); + }); + } $extension = pathinfo($path, \PATHINFO_EXTENSION); if (in_array($extension, ['css', 'js', 'jpg', 'png', 'ico', 'svg'])) { @@ -35,7 +51,12 @@ Phar::webPhar( return $path; } return false; - } -); + }); + +} catch (\Exception $e) { + if ($debug) { echo $e->getMessage(); } else { throw $e; }; +} catch (\Error $e) { + if ($debug) { echo $e->getMessage(); } else { throw $e; }; +} __HALT_COMPILER(); diff --git a/loader/src/Config/Internal.php b/loader/src/Config/Internal.php index 7744309..3cf225f 100644 --- a/loader/src/Config/Internal.php +++ b/loader/src/Config/Internal.php @@ -73,6 +73,9 @@ final class Internal extends AbstractConfig { if (!$is_cli) { $script_name = $request->server('SCRIPT_NAME'); $path_info = $request->server('PATH_INFO'); + if (!$path_info) { + $path_info = substr($request->server('REQUEST_URI'), strlen($request->server('PHP_SELF'))); + } $self = $script_name . $path_info; $script_url = "//{$request->server('HTTP_HOST')}$self"; } diff --git a/loader/src/Debug.php b/loader/src/Debug.php new file mode 100644 index 0000000..6c4887a --- /dev/null +++ b/loader/src/Debug.php @@ -0,0 +1,29 @@ + $incomming_path, + 'calculated $path' => $calculated_path, + 'PATH_INFO' => $_SERVER['PATH_INFO'], + 'REQUEST_URI' => $_SERVER['REQUEST_URI'], + 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], + 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], + 'PHP_SELF' => $_SERVER['PHP_SELF'], + 'HTTP_HOST' => $_SERVER['HTTP_HOST'], + 'Phar::running(false)' => \Phar::running(false) + ]; + } + + public function table(array $data) { + $box = "
"; + foreach ($data as $key => $val) { + $box .= "\n"; + } + $box .= "
$key" . var_export($val, true) . "
\n"; + return $box; + } +}