finller/laravel-forex 问题修复 & 功能扩展

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

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

finller/laravel-forex

Composer 安装命令:

composer require finller/laravel-forex

包简介

Forex for Laravel

README 文档

README

Latest Version on Packagist Total Downloads Tests Laravel Pint PHPStan

Laravel Forex is a simple and flexible package for retrieving the latest and historical foreign exchange rates in your Laravel application.

By default, it uses the free tier from exchangerate-api.com, but you can easily configure it to use any other Forex provider.

Contents

Features

  • Retrieve latest exchange rates for any base currency.
  • Retrieve historical exchange rates for a specific date.
  • Convert Money instances between currencies with high precision using brick/money.
  • Built-in caching and optional rate limiting to keep API usage under control.
  • Swap the default provider with a custom implementation via a simple interface.

Requirements

  • PHP ^8.3
  • Laravel ^13.0

Installation

Install the package via Composer:

composer require elegantly/laravel-forex

Publish the configuration file:

php artisan vendor:publish --tag="forex-config"

Configuration

The published configuration file lives at config/forex.php:

use Brick\Math\RoundingMode;
use Elegantly\Forex\Integrations\ExchangeRateApiFree\ExchangeRateApiFreeConnector;

return [

    /**
     * Rounding mode used when converting money.
     */
    'rounding_mode' => RoundingMode::HalfUp,

    'cache' => [
        'enabled' => true,
        'driver' => env('FOREX_CACHE_DRIVER', env('CACHE_STORE', env('CACHE_DRIVER', 'file'))),
        'expiry_seconds' => 86_400, // 1 day
    ],

    'rate_limit' => [
        'enabled' => false,
        'driver' => env('FOREX_RATE_LIMIT_DRIVER', env('CACHE_STORE', env('CACHE_DRIVER', 'file'))),
        'every_seconds' => 3_600, // 1 hour
    ],

    'client' => ExchangeRateApiFreeConnector::class,

    'clients' => [
        'exchange-rate-api' => [
            'token' => env('EXCHANGE_RATE_API_TOKEN'),
        ],
    ],

];

Environment Variables

Variable Description
EXCHANGE_RATE_API_TOKEN Your API token when using the paid exchangerate-api.com connector.
FOREX_CACHE_DRIVER The cache store used for Forex responses.
FOREX_RATE_LIMIT_DRIVER The cache store used for rate limiting.

Usage

All public methods are available through the Forex facade:

use Elegantly\Forex\Facades\Forex;

Latest Rates

Fetch the latest rates for a given base currency:

$rates = Forex::latest('USD');

$usdToEur = $rates['EUR'];

Historical Rates

Fetch historical rates for a specific date:

use Carbon\Carbon;

$rates = Forex::rates(Carbon::create(2022, 4, 25), 'USD');

$usdToEur = $rates['EUR'];

Converting Money

Convert a Money instance from one currency to another:

use Brick\Math\RoundingMode;
use Brick\Money\Money;
use Elegantly\Forex\Facades\Forex;

$convertedMoney = Forex::convert(
    money: Money::of(100, 'USD'),
    currency: 'EUR',
);

$convertedMoney->__toString(); // (EUR) 88.84

You can also convert against historical rates and override the rounding mode:

use Carbon\Carbon;

$convertedMoney = Forex::convert(
    money: Money::of(100, 'USD'),
    currency: 'EUR',
    roundingMode: RoundingMode::Down,
    date: Carbon::create(2022, 4, 25),
);

Refreshing Rates

By default, rates are cached according to your configuration. To bypass the cache and fetch fresh data:

// Refresh latest rates
Forex::refreshLatest('USD');

// Refresh historical rates
Forex::refreshRates(Carbon::create(2022, 4, 25), 'USD');

Providers

ExchangeRate-Api.com

The package ships with two ready-to-use connectors for exchangerate-api.com:

  • ExchangeRateApiFreeConnector — uses the free public endpoint. Rates are updated once a day and historical data is not supported.
  • ExchangeRateApiConnector — uses the authenticated v6 API. Requires a token and supports historical rates.

To use the paid connector, update your config:

use Elegantly\Forex\Integrations\ExchangeRateApi\ExchangeRateApiConnector;

'client' => ExchangeRateApiConnector::class,

And add your token to .env:

EXCHANGE_RATE_API_TOKEN=your-api-token

Custom Client

Want to use a different provider? Implement the ForexClient interface:

use Carbon\CarbonInterface;
use Elegantly\Forex\ForexClient;

class MyCustomForexClient implements ForexClient
{
    public function latest(string $currency): array
    {
        // Return an associative array of currency code => rate
    }

    public function rates(CarbonInterface $date, string $currency): array
    {
        // Return historical rates for the given date
    }
}

Then set it as the active client:

'client' => \App\Services\MyCustomForexClient::class,

Caching & Rate Limiting

The package uses Saloon's cache and rate-limit plugins to reduce API calls and avoid hitting provider limits.

  • Caching is enabled by default and stores responses for the configured expiry_seconds.
  • Rate limiting is disabled by default. Enable it to throttle requests to one per every_seconds interval.

Both features use the cache store configured in forex.php.

Testing

Run the test suite with:

composer test

Run the static analysis suite with:

composer analyse

Format the code with:

composer format

Changelog

See the CHANGELOG for details on recent updates.

Contributing

Contributions are welcome! Please read the CONTRIBUTING guide for details.

Security

If you discover any security-related issues, please refer to our security policy.

Credits

License

This package is open-source software licensed under the MIT license.

finller/laravel-forex 适用场景与选型建议

finller/laravel-forex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.39k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2024 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 finller/laravel-forex 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-03