From 8fd4aa7e2d7e6b9bd201ac5f0717438740e5725d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 9 Jul 2023 18:42:42 +0200 Subject: [PATCH] Improve pure phar installs --- src/Ipc/Runner/RunnerAbstract.php | 7 +++++ tools/makephar.php | 43 ++++++++++++++++++++++++++++++- tools/phar.php | 8 ++---- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/Ipc/Runner/RunnerAbstract.php b/src/Ipc/Runner/RunnerAbstract.php index 04c3145bd..b716f5b20 100644 --- a/src/Ipc/Runner/RunnerAbstract.php +++ b/src/Ipc/Runner/RunnerAbstract.php @@ -6,6 +6,7 @@ namespace danog\MadelineProto\Ipc\Runner; use Phar; +use const MADELINE_PHAR; use const MADELINE_PHP; /** @@ -29,6 +30,12 @@ abstract class RunnerAbstract if (\defined('MADELINE_PHP')) { return MADELINE_PHP; } + /** + * If using madeline.phar, simply return madeline.phar path. + */ + if (\defined('MADELINE_PHAR')) { + return MADELINE_PHAR; + } // Write process runner to external file if inside a PHAR different from madeline.phar, // because PHP can't open files inside a PHAR directly except for the stub. if (\strpos(self::SCRIPT_PATH, 'phar://') === 0) { diff --git a/tools/makephar.php b/tools/makephar.php index 211cbf894..c3770c823 100755 --- a/tools/makephar.php +++ b/tools/makephar.php @@ -25,12 +25,53 @@ $p->addFromString('.git/refs/heads/v8', $argv[3]); $p->setStub('= 2) { + \define(\MADELINE_WORKER_TYPE::class, \array_shift($arguments)); + \define(\MADELINE_WORKER_ARGS::class, $arguments); + } else { + die("MadelineProto loader: you must include this file in another PHP script, see https://docs.madelineproto.xyz for more info.".PHP_EOL); + } + \define("MADELINE_REAL_ROOT", \dirname($backtrace[0]["file"])); + } } Phar::interceptFileFuncs(); Phar::mapPhar("'.$argv[2].'"); -return require_once "phar://'.$argv[2].'/vendor/autoload.php"; +$result = require_once "phar://'.$argv[2].'/vendor/autoload.php"; + +if (\defined("MADELINE_WORKER_TYPE") && \constant("MADELINE_WORKER_TYPE") === "madeline-ipc") { + require_once "phar://'.$argv[2].'/vendor/danog/madelineproto/src/Ipc/Runner/entry.php"; +} + +return $result; __HALT_COMPILER(); ?>'); diff --git a/tools/phar.php b/tools/phar.php index 3febca1b3..b260d5a67 100644 --- a/tools/phar.php +++ b/tools/phar.php @@ -7,7 +7,7 @@ if (\defined('MADELINE_PHP')) { } if (!\defined('MADELINE_ALLOW_COMPOSER') && \class_exists(\Composer\Autoload\ClassLoader::class)) { - throw new \Exception('Composer autoloader detected: madeline.php is incompatible with Composer, please require \'danog/madelineproto:^7\' using composer.'); + throw new \Exception('Composer autoloader detected: madeline.php is incompatible with Composer, please install MadelineProto using composer: https://docs.madelineproto.xyz/docs/INSTALLATION.html#composer-from-existing-project'); } \define('MADELINE_PHP', __FILE__); @@ -169,11 +169,7 @@ class Installer self::$lock = \fopen("$phar.lock", 'c'); } \flock(self::$lock, LOCK_SH); - $result = require_once $phar; - if (\defined('MADELINE_WORKER_TYPE') && \constant('MADELINE_WORKER_TYPE') === 'madeline-ipc') { - require_once "phar://$phar/vendor/danog/madelineproto/src/Ipc/Runner/entry.php"; - } - return $result; + return require_once $phar; } /**