Remove ResourceProvider cruft

this had no obvious reason for existing, and with #5958 looming, this will become altogether useless anyway.
This commit is contained in:
Dylan K. Taylor 2023-08-07 16:14:01 +01:00
parent 9997b614bc
commit e0ad39b70a
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 29 additions and 129 deletions

View File

@ -1,83 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\plugin;
use pocketmine\utils\AssumptionFailedError;
use function file_exists;
use function fopen;
use function is_dir;
use function rtrim;
use function str_replace;
use function strlen;
use function substr;
use const DIRECTORY_SEPARATOR;
/**
* Provides resources from the given plugin directory on disk. The path may be prefixed with a specific access protocol
* to enable special types of access.
*/
class DiskResourceProvider implements ResourceProvider{
private string $file;
public function __construct(string $path){
$this->file = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $path), "/") . "/";
}
/**
* Gets an embedded resource on the plugin file.
* WARNING: You must close the resource given using fclose()
*
* @return null|resource Resource data, or null
*/
public function getResource(string $filename){
$filename = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $filename), "/");
if(file_exists($this->file . $filename)){
$resource = fopen($this->file . $filename, "rb");
if($resource === false) throw new AssumptionFailedError("fopen() should not fail on a file which exists");
return $resource;
}
return null;
}
/**
* Returns all the resources packaged with the plugin in the form ["path/in/resources" => SplFileInfo]
*
* @return \SplFileInfo[]
*/
public function getResources() : array{
$resources = [];
if(is_dir($this->file)){
/** @var \SplFileInfo $resource */
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file)) as $resource){
if($resource->isFile()){
$path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->file)));
$resources[$path] = $resource;
}
}
}
return $resources;
}
}

View File

@ -34,7 +34,7 @@ use pocketmine\Server;
*/
interface Plugin{
public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file, ResourceProvider $resourceProvider);
public function __construct(PluginLoader $loader, Server $server, PluginDescription $description, string $dataFolder, string $file, string $resourceFolder);
public function isEnabled() : bool;

View File

@ -39,11 +39,15 @@ use function dirname;
use function fclose;
use function file_exists;
use function fopen;
use function is_dir;
use function mkdir;
use function rtrim;
use function str_contains;
use function str_replace;
use function stream_copy_to_stream;
use function strlen;
use function strtolower;
use function substr;
use function trim;
use const DIRECTORY_SEPARATOR;
@ -62,11 +66,13 @@ abstract class PluginBase implements Plugin, CommandExecutor{
private PluginDescription $description,
private string $dataFolder,
private string $file,
private ResourceProvider $resourceProvider
private string $resourceFolder,
){
$this->dataFolder = rtrim($dataFolder, "/" . DIRECTORY_SEPARATOR) . "/";
//TODO: this is accessed externally via reflection, not unused
$this->file = rtrim($file, "/" . DIRECTORY_SEPARATOR) . "/";
$this->resourceFolder = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $resourceFolder), "/") . "/";
$this->configFile = Path::join($this->dataFolder, "config.yml");
$prefix = $this->getDescription()->getPrefix();
@ -215,7 +221,14 @@ abstract class PluginBase implements Plugin, CommandExecutor{
* @return null|resource Resource data, or null
*/
public function getResource(string $filename){
return $this->resourceProvider->getResource($filename);
$filename = rtrim(str_replace(DIRECTORY_SEPARATOR, "/", $filename), "/");
if(file_exists($this->resourceFolder . $filename)){
$resource = fopen($this->resourceFolder . $filename, "rb");
if($resource === false) throw new AssumptionFailedError("fopen() should not fail on a file which exists");
return $resource;
}
return null;
}
/**
@ -254,7 +267,18 @@ abstract class PluginBase implements Plugin, CommandExecutor{
* @return \SplFileInfo[]
*/
public function getResources() : array{
return $this->resourceProvider->getResources();
$resources = [];
if(is_dir($this->resourceFolder)){
/** @var \SplFileInfo $resource */
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resourceFolder)) as $resource){
if($resource->isFile()){
$path = str_replace(DIRECTORY_SEPARATOR, "/", substr((string) $resource, strlen($this->resourceFolder)));
$resources[$path] = $resource;
}
}
}
return $resources;
}
public function getConfig() : Config{

View File

@ -213,7 +213,7 @@ class PluginManager{
* @var Plugin $plugin
* @see Plugin::__construct()
*/
$plugin = new $mainClass($loader, $this->server, $description, $dataFolder, $prefixed, new DiskResourceProvider($prefixed . "/resources/"));
$plugin = new $mainClass($loader, $this->server, $description, $dataFolder, $prefixed, $prefixed . "/resources/");
$this->plugins[$plugin->getDescription()->getName()] = $plugin;
return $plugin;

View File

@ -1,41 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\plugin;
interface ResourceProvider{
/**
* Gets an embedded resource on the plugin file.
* WARNING: You must close the resource given using fclose()
*
* @return null|resource Resource data, or null
*/
public function getResource(string $filename);
/**
* Returns all the resources packaged with the plugin in the form ["path/in/resources" => SplFileInfo]
*
* @return \SplFileInfo[]
*/
public function getResources() : array;
}