feat: Des indication de date dans le debug, et classe pour initialiser le phar

pull/35/head
Matthieu Marcillaud 6 months ago
parent 5c8febbb5f
commit 3991d515ba

@ -44,6 +44,8 @@ class Build {
buildDirectory: $this->buildDirectory,
debug: $this->debug,
);
$this->stats['Git Version'] = $git->getFullVersion();
$this->stats['Git Date'] = $git->getVersionDate()->format('Y-m-d H:i:s');
$n = $phar->build(
$git->getVersion(),
$git->getFullVersion(),
@ -108,6 +110,8 @@ class Build {
private function start() {
$this->stats = [
'Version' => 'No version file',
'Git Version' => '',
'Git Date' => '',
'Time' => '',
'Memory' => '',
'Filesize' => 'No archive file',

@ -13,8 +13,8 @@ class Git {
$this->directory = $directory;
}
public function run(string $command): string {
$git = 'git -C ' . $this->directory;
public function run(string $command, $env = null): string {
$git = ($env ? $env . ' ' : '') . 'git -C ' . $this->directory;
return trim(exec($git . ' ' . $command));
}
@ -27,9 +27,8 @@ class Git {
}
public function getVersionDate(): DateTimeImmutable {
$date = $this->run('log -n1 --pretty=%ci HEAD');
$date = new DateTimeImmutable($date ?: null);
$date->setTimezone(new DateTimeZone('UTC'));
$date = $this->run('show --quiet --date=local --format="%cd" HEAD', 'TZ=UTC0');
$date = new DateTimeImmutable($date ?: 'now', new DateTimeZone('UTC'));
return $date;
}
}

@ -60,7 +60,7 @@ class PharArchive {
$phar = new \Phar($destFile, 0, $this->filename);
$phar->setSignatureAlgorithm(\Phar::SHA512);
$phar->startBuffering();
$phar->setStub($this->getStub($private_version));
$phar->setStub($this->getStub($private_version, $versionDate));
$phar['/version.php'] = "<?php return '$public_version';";
$phar['/index.php'] = "<?php require __DIR__ . '/public/index.php';";
@ -92,14 +92,22 @@ class PharArchive {
rename($this->buildDirectory . '/' . self::FILENAME, $this->buildDirectory . '/' . $filename);
}
private function getStub(string $version): string {
private function getStub(string $version, \DateTimeInterface $date): 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);
$stub = $this->replaceConst($stub, 'VERSION', '', $version);
$stub = $this->replaceConst($stub, 'DATE', '', $date->format('Y-m-d H:i:s'));
$stub = $this->replaceConst($stub, 'COMPILATION_DATE', '', (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format('Y-m-d H:i:s'));
$stub = $this->replaceConst($stub, 'DEBUG', false, $this->debug);
return $stub;
}
private function replaceConst($stub, $name, $default, $value) {
$search = sprintf('const %s = %s;', $name, var_export($default, true));
$replace = sprintf('const %s = %s;', $name, var_export($value, true));
return str_replace($search, $replace, $stub);
}
/**
* Removes whitespace from a PHP source string while preserving line numbers.
*

@ -10,7 +10,7 @@ require __DIR__ . '/../vendor/autoload.php';
$request = Request::createFromGlobals();
$config = new Config(
new Internal(__FILE__, $request, true),
new Internal($request, true),
new Custom()
);

@ -2,53 +2,76 @@
use Spip\Loader\Debug;
$debug = false;
final class Spip_Loader_Phar_Bootstrap {
const VERSION = '';
const DATE = '';
const COMPILATION_DATE = '';
const DEBUG = false;
try {
Phar::mapPhar('spip_loader.phar');
try {
require 'phar://spip_loader.phar/vendor/autoload.php';
} catch (\Error $e) {
// @note \Error is PHP7+… (quid de PHP 5.6 ?)
if (!(
function_exists('opcache_get_status')
const NAME = 'spip_loader.phar';
const AUTOLOADER = 'phar://spip_loader.phar/vendor/autoload.php';
public function init() {
Phar::mapPhar(self::NAME);
$this->start_autoloader();
Debug::add('VERSION', self::VERSION);
Debug::add('DATE', self::DATE);
Debug::add('COMPILATION_DATE', self::COMPILATION_DATE);
}
/** @return bool */
private function is_opcache_enabled() {
return function_exists('opcache_get_status')
&& opcache_get_status()
&& (opcache_get_status()['opcache_enabled'])
&& false !== strpos($e->getMessage(), sprintf('Failed opening required \'%s\'', 'phar://spip_loader.phar/vendor/autoload.php'))
)) {
throw $e;
}
&& (opcache_get_status()['opcache_enabled']);
}
private function has_failed_autoloader(\Error $e) {
return false !== strpos($e->getMessage(), sprintf('Failed opening required \'%s\'', self::AUTOLOADER));
}
// some hosting failed re-opening inside phar files :/
@ini_set('opcache.enable', 0);
private function start_autoloader() {
try {
require 'phar://spip_loader.phar/vendor/autoload.php';
Debug::add('Disable opcache', 'Script disable opcache to start the Loader');
require self::AUTOLOADER;
} catch (\Error $e) {
if (!(
function_exists('opcache_reset')
&& false !== strpos($e->getMessage(), sprintf('Failed opening required \'%s\'', 'phar://spip_loader.phar/vendor/autoload.php'))
)) {
if ($this->is_opcache_enabled() && $this->has_failed_autoloader($e)) {
// some hosting failed re-opening inside phar files :/
@ini_set('opcache.enable', 0);
try {
require 'phar://spip_loader.phar/vendor/autoload.php';
Debug::add('Disable opcache', 'Script disable opcache to start the Loader');
} catch (\Error $e) {
if (function_exists('opcache_reset') && $this->has_failed_autoloader($e)) {
opcache_reset(); // brutality mode :/
require 'phar://spip_loader.phar/vendor/autoload.php';
Debug::add('Reset opcache', 'Script reset opcache to start the Loader (really bad thing 😢)');
} else {
throw $e;
}
}
} else {
throw $e;
}
// brutality mode :/
opcache_reset();
require 'phar://spip_loader.phar/vendor/autoload.php';
Debug::add('Reset opcache', 'Script reset opcache to start the Loader (really bad thing 😢)');
}
}
}
try {
$bootstrap = new Spip_Loader_Phar_Bootstrap();
$bootstrap->init();
if (!http_response_code()) {
require 'phar://spip_loader.phar/bin/console.php';
exit(0);
}
if ($debug) {
if (self::DEBUG) {
Debug::enable();
}
function spip_loader_phar_path($path) {
Phar::webPhar('spip_loader.phar', null, null, ['svg' => 'image/svg+xml'], function ($incomming_path) {
$path = $incomming_path;
// Gestion des assets (spip_loader.php/?file=assets/...)
if (isset($_GET['file'])) {
return '/' . ltrim($_GET['file'], '/');
@ -63,12 +86,6 @@ try {
);
}
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);
Debug::add('incomming path', $incomming_path);
Debug::add('calculated path', $path);
@ -91,9 +108,9 @@ try {
} catch (\Exception $e) {
// @note use \Throwable when PHP 7+
if ($debug) { echo $e->getMessage(); } else { throw $e; };
if (Spip_Loader_Phar_Bootstrap::DEBUG) { echo $e->getMessage(); } else { throw $e; };
} catch (\Error $e) {
if ($debug) { echo $e->getMessage(); } else { throw $e; };
if (Spip_Loader_Phar_Bootstrap::DEBUG) { echo $e->getMessage(); } else { throw $e; };
}
__HALT_COMPILER();

Loading…
Cancel
Save