Removed PLUGIN_PATH constant

This commit is contained in:
Dylan K. Taylor 2020-12-09 20:48:50 +00:00
parent ff6672ba85
commit d39348929f
4 changed files with 26 additions and 12 deletions

View File

@ -198,7 +198,8 @@ namespace pocketmine {
$opts = getopt("", ["data:", "plugins:", "no-wizard", "enable-ansi", "disable-ansi"]);
$dataPath = isset($opts["data"]) ? $opts["data"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR;
define('pocketmine\PLUGIN_PATH', isset($opts["plugins"]) ? $opts["plugins"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR);
$pluginPath = isset($opts["plugins"]) ? $opts["plugins"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR;
Filesystem::addCleanedPath($pluginPath, Filesystem::CLEAN_PATH_PLUGINS_PREFIX);
if(!file_exists($dataPath)){
mkdir($dataPath, 0777, true);
@ -243,7 +244,7 @@ namespace pocketmine {
$autoloader = new \BaseClassLoader();
$autoloader->register(false);
new Server($autoloader, $logger, $dataPath, \pocketmine\PLUGIN_PATH);
new Server($autoloader, $logger, $dataPath, $pluginPath);
$logger->info("Stopping other threads");

View File

@ -40,7 +40,9 @@ use function rtrim;
use function scandir;
use function str_replace;
use function stream_get_contents;
use function strlen;
use function strpos;
use function uasort;
use function unlink;
use const DIRECTORY_SEPARATOR;
use const LOCK_EX;
@ -52,6 +54,13 @@ use const SCANDIR_SORT_NONE;
final class Filesystem{
/** @var resource[] */
private static $lockFileHandles = [];
/**
* @var string[]
* @phpstan-var array<string, string>
*/
private static $cleanedPaths = [
\pocketmine\PATH => self::CLEAN_PATH_SRC_PREFIX
];
public const CLEAN_PATH_SRC_PREFIX = "pmsrc";
public const CLEAN_PATH_PLUGINS_PREFIX = "plugins";
@ -79,6 +88,19 @@ final class Filesystem{
}
}
public static function addCleanedPath(string $path, string $replacement) : void{
self::$cleanedPaths[$path] = $replacement;
uksort(self::$cleanedPaths, function(string $str1, string $str2) : int{
return strlen($str2) <=> strlen($str1); //longest first
});
}
/**
* @return string[]
* @phpstan-return array<string, string>
*/
public static function getCleanedPaths() : array{ return self::$cleanedPaths; }
/**
* @param string $path
*
@ -88,12 +110,7 @@ final class Filesystem{
$result = str_replace([DIRECTORY_SEPARATOR, ".php", "phar://"], ["/", "", ""], $path);
//remove relative paths
//TODO: make these paths dynamic so they can be unit-tested against
static $cleanPaths = [
\pocketmine\PLUGIN_PATH => self::CLEAN_PATH_PLUGINS_PREFIX, //this has to come BEFORE \pocketmine\PATH because it's inside that by default on src installations
\pocketmine\PATH => self::CLEAN_PATH_SRC_PREFIX
];
foreach($cleanPaths as $cleanPath => $replacement){
foreach(self::$cleanedPaths as $cleanPath => $replacement){
$cleanPath = rtrim(str_replace([DIRECTORY_SEPARATOR, "phar://"], ["/", ""], $cleanPath), "/");
if(strpos($result, $cleanPath) === 0){
$result = ltrim(str_replace($cleanPath, $replacement, $result), "/");

View File

@ -33,4 +33,3 @@ if(!extension_loaded('libdeflate')){
//TODO: these need to be defined properly or removed
define('pocketmine\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 2) . '/vendor/autoload.php');
define('pocketmine\PLUGIN_PATH', '');

View File

@ -33,9 +33,6 @@ class UtilsTest extends TestCase{
if(!defined('pocketmine\PATH')){
define('pocketmine\PATH', 'dummy');
}
if(!defined('pocketmine\PLUGIN_PATH')){
define('pocketmine\PLUGIN_PATH', 'dummy');
}
}
/**