feat: Afficher des indications lorsqu’on compile avec --debug

pull/33/head
Matthieu Marcillaud 2 months ago
parent a167b19d1a
commit 35a179b7c0

@ -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();

@ -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(),

@ -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("<?php\n", "<?php\n// Version: $private_version\n", $stub);
$phar->setStub($stub);
$phar->setStub($this->getStub($private_version));
$phar['/version.php'] = "<?php return '$public_version';";
$phar['/index.php'] = "<?php require __DIR__ . '/public/index.php';";
@ -94,6 +92,14 @@ class PharArchive {
rename($this->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("<?php\n", "<?php\n// Version: $version\n", $stub);
$stub = str_replace("\$debug = false;\n", "\$debug = " . var_export($this->debug, true) . ";\n", $stub);
return $stub;
}
/**
* Removes whitespace from a PHP source string while preserving line numbers.
*

@ -1,25 +1,41 @@
<?php
# ini_set('display_errors', 1);
# error_reporting(E_ALL ^ E_DEPRECATED);
# require_once 'phar://' . __FILE__ . '/vendor/autoload.php';
if (!http_response_code()) {
require_once 'phar://' . __FILE__ . '/bin/console.php';
exit(0);
}
$debug = false;
try {
if ($debug) {
@ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_DEPRECATED);
}
Phar::mapPhar('spip_loader.phar');
require_once 'phar://spip_loader.phar/vendor/autoload.php';
Phar::webPhar(
'spip_loader.phar',
null,
null,
[
'svg' => '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();

@ -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";
}

@ -0,0 +1,29 @@
<?php
namespace Spip\Loader;
class Debug {
public function phar_rewrite_debug($incomming_path, $calculated_path) {
return [
'incomming $path' => $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 = "<div class='msg-alert notice' style='margin-bottom:0'><table>";
foreach ($data as $key => $val) {
$box .= "<tr><th>$key</th><td><code>" . var_export($val, true) . "</code></td></tr>\n";
}
$box .= "</table></div>\n";
return $box;
}
}
Loading…
Cancel
Save