aternos/modrinth-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aternos/modrinth-api

Composer 安装命令:

composer require aternos/modrinth-api

包简介

PHP Client for the Modrinth API. This client is based on the openapi spec.

README 文档

README

An API client for the Modrinth API written in PHP. This client is a combination of code generated by OpenAPI Generator and some wrappers around it to improve the usability.

The generated code can be found in lib/Api and lib/Model. It is recommended to use the Wrappers in lib/Client instead of the generated code.

Installation

Install the package via composer:

composer require aternos/modrinth-api

Usage

The main entry point for the API is the ModrinthAPIClient class.

<?php
use Aternos\ModrinthApi\Client\ModrinthAPIClient;

// create an API client. This is the main entry point for the API
$modrinthClient = new ModrinthAPIClient();

// set a user agent (recommended)
$modrinthClient->setUserAgent('aternos/php-modrinth-api-example');

// set an api token (optional)
$modrinthClient->setApiToken("api-token");

The API Token is only required for non-public requests but if it is provided, it will be used for all requests.

Paginated Lists

Some methods return a paginated list which contains a list of results on the current page and methods to navigate to the next and previous page. The paginated list implements Iterator, ArrayAccess and Countable so you can use it like an array. It also has a getResults() method which returns the underlying array of results.

Searching for Projects

$projects = $modrinthClient->searchProjects();

foreach ($project as $project) {
    // like most other methods, this method returns a wrapper
    // you can use the getData() method to get the project data
    echo $project->getData()->getTitle() . PHP_EOL;
}

$projects = $projects->getNextPage();

foreach ($projects as $project) {
    echo $project->getData()->getTitle() . PHP_EOL;
}

Search for Projects with Options

You can apply filters and change the sort order when searching for projects. All options are optional and can be combined.

use \Aternos\ModrinthApi\Client\Options\ProjectSearchOptions;
use \Aternos\ModrinthApi\Client\Options\SearchIndex;

$options = new ProjectSearchOptions();
$options->setQuery("mclogs");
$options->setSearchIndex(SearchIndex::UPDATED);
$projects = $modrinthClient->getProjects($options);

Facets

One way to filter search results are facets. Facets specify which loader, category, game version or license to filter for. You need to provide exactly one AND group which can consist of multiple OR groups:

use \Aternos\ModrinthApi\Client\Options\Facets\Facet;
use \Aternos\ModrinthApi\Client\Options\Facets\FacetType;
use \Aternos\ModrinthApi\Client\Options\Facets\FacetORGroup;
use \Aternos\ModrinthApi\Client\Options\Facets\FacetANDGroup;

$facetOrGroup = new FacetORGroup();
$facetOrGroup->addFacet(new Facet(FacetType::VERSIONS, "1.20.1"))
             ->addFacet(new Facet(FacetType::VERSIONS, "1.20"));

// or add multiple facets at once:
$facetOrGroup->addFacets(
    new Facet(FacetType::VERSIONS, "1.19.4"),
    new Facet(FacetType::VERSIONS, "1.19.3")
);

// alternatively if all facets have the same type:
$facetOrGroup->addFacets(FacetType::VERSIONS, "1.19.2", "1.19.1", "1.19");

$options->setFacets($facetOrGroup);

// if you want to combine multiple OR groups, you can use an AND group:
$facetAndGroup = new FacetANDGroup();
$facetAndGroup->addORGroup($facetOrGroup)
              ->addORGroup(new FacetORGroup(new Facet(FacetType::LICENSE, "MIT")));
              
// or
$facetAndGroup = $facetOrGroup->toANDGroup()
    ->addORGroup(new FacetORGroup(new Facet(FacetType::LICENSE, "MIT")));

$options->setFacets($facetAndGroup);

$projects = $modrinthClient->getProjects($options);

By default facets are checked using the equals operator. There are several other operators available:

use \Aternos\ModrinthApi\Client\Options\Facets\FacetOperator;

$facet = new Facet(FacetType::DOWNLOADS, "1.20.1", FacetOperator::GREATER_THAN);

Getting Additional Project Data

The Project wrapper provides methods to fetch additional data about the project.

// get a specific project
$project = $modrinthClient->getProject("mclogs");

// get versions of the project
$versions = $project->getVersions();

// get a specific version
$version = $project->getVersion("2.6.2");

// get the members of the project
$members = $project->getMembers();

Fetching Projects

You can also fetch individual projects by their id or slug:

$project = $modrinthClient->getProject("mclogs");

or multiple projects by their ids:

$projects = $modrinthClient->getProjects(["6DdCzpTL", "VPo0otUH"]);

It seems like fetching by slugs is also supported here but this is not documented.

Check if a slug/id is used

$projectId = $modrinthClient->checkProjectValidity("mclogs");

if ($projectId) {
    echo "project exists, id:" . $projectId;
} else {
    echo "project does not exist";
}

Project Dependencies

Get dependencies of a project

$dependencies = $project->getDependencies();

// on modrinth you can depend on a project or directly on a version
$projects = $dependencies->getProjects();
$versions = $dependencies->getVersions();

Versions

// get versions of a project by name
$versions = $modrinthClient->getProjectVersions("mclogs");

// get the versions from a project
$versions = $project->getVersions();

// get a specific version by its id
$version = $modrinthClient->getVersion("xzRGr4AC");

// get multiple versions by their ids
$versions = $modrinthClient->getVersions(["xzRGr4AC", "EwNN8uNA"]);

Hashes

The modrinth API also allows you to find a version by its SHA1 or SHA512 hash:

use \Aternos\ModrinthApi\Client\HashAlgorithm;

$hash = "5952253d61e199e82eb852c5824c3981b29b209d";

// returns the modrinth version for the given hash
$version = $modrinthClient->getVersionByHash($hash, HashAlgorithm::SHA1);

// fetch multiple versions at once
$versions = $modrinthClient->getVersionsByHashes([$hash], HashAlgorithm::SHA1);

// returns the latest version for a specific loader and version
$version = $modrinthClient->getLatestVersionByHash($hash, ["spigot"], ["1.19.4"], HashAlgorithm::SHA1);

// fetch multiple versions at once
$versions = $modrinthClient->getLatestVersionsByHashes([$hash], ["spigot"], ["1.19.4"], HashAlgorithm::SHA1);

Users

// get a user
$user = $modrinthClient->getUser("matthias");

// get projects of a user
$projects = $user->getProjects();

// get notifications (requires authentication)
$notifications = $user->getNotifications();

// get followed projects (requires authentication)
$projects = $user->getFollowedProjects();

// get payout history (requires authentication)
$history = $user->getPayoutHistory();

Teams

// get the members of a team
$members = $modrinthClient->getTeamMembers("ThaUQrOs");

// get the members of multiple teams at once
$teams = $modrinthClient->getTeams(["ThaUQrOs"]);

Tags

You can fetch all available categories, loaders, game versions and licenses from the API. Our library provides methods to search projects by these directly:

$categories = $modrinthClient->getCategories();
$categories[0]->searchProjects();
$loaders = $modrinthClient->getLoaders();
$loaders[0]->searchProjects();
$gameVersions = $modrinthClient->getGameVersions();
$gameVersions[0]->searchProjects();
$licenses = $modrinthClient->getLicenses();
$licenses[0]->searchProjects();

You can also fetch donation platforms and report types:

$reportTypes = $modrinthClient->getReportTypes();
$donationPlatforms = $modrinthClient->getDonationPlatforms();

Updating the generated code

The generated code can be updated by installing the openapi generator running the following command:

openapi-generator-cli generate -c config.yaml

aternos/modrinth-api 适用场景与选型建议

aternos/modrinth-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.97k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「api」 「plugins」 「minecraft」 「mods」 「modrinth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aternos/modrinth-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aternos/modrinth-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 aternos/modrinth-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-27