Skip to content

Commit

Permalink
Fix CS
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Oct 1, 2023
1 parent 1ee16c4 commit ec82f9a
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 114 deletions.
13 changes: 8 additions & 5 deletions src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\omp\lang\OMPLanguage;
use thebigcrafter\Sulfur\Path;
use function is_dir;
use function is_file;
use function mkdir;

final class OhMyPMMP extends PluginBase
{
use SingletonTrait;

private OMPLanguage $language;

public function onLoad(): void
public function onLoad() : void
{
self::setInstance($this);
}

public function onEnable(): void
public function onEnable() : void
{
$this->saveDefaultConfig();
$this->loadLanguage();

Fluorine::run();
}

public function loadLanguage(): void
public function loadLanguage() : void
{
$langFolder = Path::join($this->getDataFolder(), "lang");
/** @var string $selectedLanguage */
Expand All @@ -58,7 +61,7 @@ public function loadLanguage(): void
$this->language = new OMPLanguage($selectedLanguage);
}

public function getLanguage(): OMPLanguage {
public function getLanguage() : OMPLanguage {
return $this->language;
}
}
}
13 changes: 11 additions & 2 deletions src/thebigcrafter/omp/Vars.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp;

final class Vars {
public const POGGIT_REPO_URL = "https://poggit.pmmp.io/releases.min.json?fields=name,version,artifact_url,html_url,license,downloads,score,api,deps,description_url,icon_url,tagline";
}
public const POGGIT_REPO_URL = "https://poggit.pmmp.io/releases.min.json?fields=name,version,artifact_url,html_url,license,downloads,score,api,deps,description_url,icon_url,tagline";
}
9 changes: 8 additions & 1 deletion src/thebigcrafter/omp/commands/OMPCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\commands;

class OMPCommand {}
class OMPCommand {}
93 changes: 51 additions & 42 deletions src/thebigcrafter/omp/lang/OMPLanguage.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\lang;

use Exception;
use thebigcrafter\Sulfur\Filesystem;
use function array_key_exists;
use function file_exists;
use function json_decode;

class OMPLanguage
{
/** @var array<string, string> $languages */
private static array $languages;

private string $default;

public static function loadLanguageFile(string $name, string $path): void
{
if (!file_exists($path)) {
throw new Exception('File not found.');
}
self::$languages[$name] = json_decode(\thebigcrafter\omp\utils\Filesystem::readFile($path)->await() ?: '', true); /** @phpstan-ignore-line */
}

public function __construct(string $default)
{
if (!array_key_exists($default, self::$languages)) {
throw new Exception('Language not found');
}

$this->default = $default;
}

public function setDefaultLanguage(string $name): void
{
if (!array_key_exists($name, self::$languages)) {
throw new Exception('Language not found');
}

$this->default = $name;
}

public function get(string $key): string
{
if (!array_key_exists($key, self::$languages[$this->default])) { /** @phpstan-ignore-line */
throw new Exception("$key not found");
}

return self::$languages[$this->default][$key]; /** @phpstan-ignore-line */
}
}
/** @var array<string, string> $languages */
private static array $languages;

private string $default;

public static function loadLanguageFile(string $name, string $path) : void
{
if (!file_exists($path)) {
throw new Exception('File not found.');
}

self::$languages[$name] = json_decode(\thebigcrafter\omp\utils\Filesystem::readFile($path)->await() ?: '', true); /** @phpstan-ignore-line */
}

public function __construct(string $default)
{
if (!array_key_exists($default, self::$languages)) {
throw new Exception('Language not found');
}

$this->default = $default;
}

public function setDefaultLanguage(string $name) : void
{
if (!array_key_exists($name, self::$languages)) {
throw new Exception('Language not found');
}

$this->default = $name;
}

public function get(string $key) : string
{
if (!array_key_exists($key, self::$languages[$this->default])) { /** @phpstan-ignore-line */
throw new Exception("$key not found");
}

return self::$languages[$this->default][$key]; /** @phpstan-ignore-line */
}
}
38 changes: 22 additions & 16 deletions src/thebigcrafter/omp/utils/Filesystem.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\utils;

use Exception;
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\Iodine\DeferredFuture;
use thebigcrafter\Iodine\Future;
use thebigcrafter\Iodine\Iodine;
use thebigcrafter\Sulfur\Path;
use function file_exists;
use function file_get_contents;

class Filesystem
{
/**
* @return Future<mixed>
*/
public static function readFile(string $path): Future
{
if (!file_exists($path)) {
throw new Exception("File not found");
}
/**
* @return Future<mixed>
*/
public static function readFile(string $path) : Future
{
if (!file_exists($path)) {
throw new Exception("File not found");
}

return Iodine::async(function () use ($path) {
return file_get_contents($path);
});
}
}
return Iodine::async(function () use ($path) {
return file_get_contents($path);
});
}
}
104 changes: 56 additions & 48 deletions src/thebigcrafter/omp/utils/Internet.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\utils;

use pocketmine\utils\InternetException;
use pocketmine\utils\InternetRequestResult;
use thebigcrafter\Fluorine\Fluorine;
use thebigcrafter\Iodine\DeferredFuture;
use thebigcrafter\Iodine\Future;
use thebigcrafter\Iodine\Iodine;
use function get_headers;
use function is_numeric;
use function round;

final class Internet
{
/**
* @return Future<mixed>
*/
public static function fetch(string $url): Future
{
return Iodine::async(function () use ($url) {
try {
$res = \pocketmine\utils\Internet::getURL($url);

if ($res instanceof InternetRequestResult) {
return $res->getBody();
}
} catch (InternetException $e) {
throw $e;
}
});
}

/**
* @return Future<mixed>
*/
public static function getRemoteFileSize(string $url): Future
{
return Iodine::async(function () use ($url) {
$headers = get_headers($url, true);

if (isset($headers['Content-Length']) && is_numeric($headers['Content-Length'])) {
$bytes = (int) $headers['Content-Length'];
return self::formatFileSize($bytes);
}

return 'Unknown'; // Default value if Content-Length header is not present or not numeric
});
}

private static function formatFileSize(int $bytes): string
{
if ($bytes < 1024) {
return $bytes . ' B';
} elseif ($bytes < 1048576) {
return round($bytes / 1024, 2) . ' KB';
} else {
return round($bytes / 1024 / 1024, 2) . ' MB';
}
}
}
/**
* @return Future<mixed>
*/
public static function fetch(string $url) : Future
{
return Iodine::async(function () use ($url) {
try {
$res = \pocketmine\utils\Internet::getURL($url);

if ($res instanceof InternetRequestResult) {
return $res->getBody();
}
} catch (InternetException $e) {
throw $e;
}
});
}

/**
* @return Future<mixed>
*/
public static function getRemoteFileSize(string $url) : Future
{
return Iodine::async(function () use ($url) {
$headers = get_headers($url, true);

if (isset($headers['Content-Length']) && is_numeric($headers['Content-Length'])) {
$bytes = (int) $headers['Content-Length'];
return self::formatFileSize($bytes);
}

return 'Unknown'; // Default value if Content-Length header is not present or not numeric
});
}

private static function formatFileSize(int $bytes) : string
{
if ($bytes < 1024) {
return $bytes . ' B';
} elseif ($bytes < 1048576) {
return round($bytes / 1024, 2) . ' KB';
} else {
return round($bytes / 1024 / 1024, 2) . ' MB';
}
}
}

0 comments on commit ec82f9a

Please sign in to comment.