refactor: class Stub, et pas besoin de créer un fichier version.php

pull/35/head
Matthieu Marcillaud 6 months ago
parent 86191c9ee6
commit c79e7a7499

@ -60,8 +60,7 @@ class PharArchive {
$phar = new \Phar($destFile, 0, $this->filename);
$phar->setSignatureAlgorithm(\Phar::SHA512);
$phar->startBuffering();
$phar->setStub($this->getStub($private_version, $versionDate));
$phar['/version.php'] = "<?php return '$public_version';";
$phar->setStub($this->getStub($public_version, $private_version, $versionDate));
$phar['/index.php'] = "<?php require __DIR__ . '/public/index.php';";
foreach ($this->getPhpFiles() as $file) {
@ -92,10 +91,11 @@ class PharArchive {
rename($this->buildDirectory . '/' . self::FILENAME, $this->buildDirectory . '/' . $filename);
}
private function getStub(string $version, \DateTimeInterface $date): string {
private function getStub(string $version, string $full_version, \DateTimeInterface $date): string {
$stub = file_get_contents($this->sourceDirectory . '/phar/stub.php');
#$stub = $this->stripWhitespacePhpFile($stub);
$stub = $this->replaceConst($stub, 'VERSION', '', $version);
$stub = $this->replaceConst($stub, 'FULL_VERSION', '', $full_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);

@ -1,9 +1,11 @@
<?php
namespace Spip\Loader;
use Spip\Loader\Debug;
use Phar;
final class Spip_Loader_Phar_Bootstrap {
final class Stub {
const VERSION = '';
const FULL_VERSION = '';
const DATE = '';
const COMPILATION_DATE = '';
const DEBUG = false;
@ -13,34 +15,87 @@ final class Spip_Loader_Phar_Bootstrap {
public function init() {
Phar::mapPhar(self::NAME);
$this->start_autoloader();
$this->warmup();
Debug::add('VERSION', self::VERSION);
if ($this->is_opcache_enabled()) {
// some hosting failed re-opening inside phar files :/
// and some weird cache occurs sometimes into phar content files
@ini_set('opcache.enable', 0);
}
require self::AUTOLOADER;
Debug::add('FULL_VERSION', self::VERSION);
Debug::add('DATE', self::DATE);
Debug::add('COMPILATION_DATE', self::COMPILATION_DATE);
}
/** @param \Throwable $e */
public static function show_if_debug($e) {
if (!self::DEBUG) {
throw $e;
public function handle() {
if ($this->is_route('warmup')) {
// Route `spip_loader.php?warmup` that could help on some hosting and some opcache configs.
// Please dont use this abusively as it clears cache for all files in the fpm instance (not only this website)
opcache_reset();
Debug::add('Reset Opcache', 'Script reset Opcache.');
}
if (!http_response_code()) {
$this->handle_cli();
} else {
$this->handle_web();
}
echo $e->getMessage() . ' on file ' . $e->getFile() . ' on line ' . $e->getLine();
}
private function warmup() {
if (
!empty($_SERVER['QUERY_STRING'])
&& ($_SERVER['QUERY_STRING'] === 'warmup')
&& function_exists('opcache_reset')
) {
if (function_exists('opcache_reset')) {
opcache_reset();
Debug::add('Reset Opcache', 'Script reset Opcache.');
}
public function handle_cli() {
require 'phar://spip_loader.phar/bin/console.php';
exit(0);
}
public function handle_web() {
if (self::DEBUG) {
Debug::enable();
}
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'])) {
$path = '/' . ltrim($_GET['file'], '/');
}
if (!$path) {
$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
}
if (!$path) {
$path = substr(
parse_url(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '', PHP_URL_PATH),
strlen($_SERVER['PHP_SELF'])
);
}
Debug::add('incomming path', $incomming_path);
Debug::add('calculated path', $path);
$extension = pathinfo($path, \PATHINFO_EXTENSION);
if (in_array($extension, ['css', 'js', 'jpg', 'png', 'ico', 'svg'])) {
# Éviter Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR (http/2)
# https://serverfault.com/questions/844526/apache-2-4-7-ignores-response-header-content-encoding-identity-instead-respect
header('Content-Encoding: none');
header('Expires: ' . \date('r', time() + 86400 * 7));
}
if ($extension && $extension !== 'php') {
return '/public' . $path;
}
if ($path === '') {
return '/index.php';
}
return false;
});
}
/** @param \Throwable $e */
public static function show_on_debug($e) {
if (!self::DEBUG) {
throw $e;
}
echo $e->getMessage() . ' on file ' . $e->getFile() . ' on line ' . $e->getLine();
}
/** @return bool */
private function is_opcache_enabled() {
@ -49,71 +104,25 @@ final class Spip_Loader_Phar_Bootstrap {
&& (opcache_get_status()['opcache_enabled']);
}
private function start_autoloader() {
if ($this->is_opcache_enabled()) {
// some hosting failed re-opening inside phar files :/
// and some weird cache occurs sometimes into phar content files
@ini_set('opcache.enable', 0);
}
require self::AUTOLOADER;
/**
* Simple route `spip_loader.php?{route}`
* @param string $route
* @return bool
*/
private function is_route($route) {
return !empty($_SERVER['QUERY_STRING']) && ($_SERVER['QUERY_STRING'] === $route);
}
}
try {
$bootstrap = new Spip_Loader_Phar_Bootstrap();
$bootstrap->init();
if (!http_response_code()) {
require 'phar://spip_loader.phar/bin/console.php';
exit(0);
}
if ($bootstrap::DEBUG) {
Debug::enable();
}
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'])) {
$path = '/' . ltrim($_GET['file'], '/');
}
if (!$path) {
$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
}
if (!$path) {
$path = substr(
parse_url(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '', PHP_URL_PATH),
strlen($_SERVER['PHP_SELF'])
);
}
Debug::add('incomming path', $incomming_path);
Debug::add('calculated path', $path);
$extension = pathinfo($path, \PATHINFO_EXTENSION);
if (in_array($extension, ['css', 'js', 'jpg', 'png', 'ico', 'svg'])) {
# Éviter Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR (http/2)
# https://serverfault.com/questions/844526/apache-2-4-7-ignores-response-header-content-encoding-identity-instead-respect
header('Content-Encoding: none');
header('Expires: ' . \date('r', time() + 86400 * 7));
}
if ($extension && $extension !== 'php') {
return '/public' . $path;
}
if ($path === '') {
return '/index.php';
}
return false;
});
$stub = new Stub();
$stub->init();
$stub->handle();
} catch (\Exception $e) {
// @note use \Throwable when PHP 7+
Spip_Loader_Phar_Bootstrap::show_if_debug($e);
Stub::show_on_debug($e);
} catch (\Error $e) {
Spip_Loader_Phar_Bootstrap::show_if_debug($e);
Stub::show_on_debug($e);
}
__HALT_COMPILER();

@ -72,10 +72,6 @@ class Application {
$this->config
);
$this->page->setAppVersion($this->getVersion());
if (!defined('PCLZIP_TEMPORARY_DIR')) {
define('PCLZIP_TEMPORARY_DIR', $this->config->get('directory.tmp'));
}
}
/** @return bool */
@ -171,11 +167,9 @@ class Application {
* @return void
*/
private function define_version() {
$root = $this->config->get('directory.root');
if (file_exists($root . '/version.php')) {
$this->version = require $root . '/version.php';
}
if (!\Phar::running() && function_exists('exec')) {
if (\Phar::running()) {
$this->version = Stub::VERSION;
} elseif (function_exists('exec')) {
$cmd = sprintf('/usr/bin/env git -C %s describe --tags --abbrev=0 2>/dev/null', __DIR__);
if ($version = @exec($cmd)) {
$this->version = $version;

Loading…
Cancel
Save