centralbank/exchangerates 问题修复 & 功能扩展

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

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

centralbank/exchangerates

Composer 安装命令:

composer require centralbank/exchangerates

包简介

Enhanced Central Bank of Myanmar Exchange Rates Laravel Package with caching, error handling, and service-based architecture.

README 文档

README

#SaveMyanmar

Enhanced Central Bank of Myanmar Exchange Rates Laravel Package with caching, error handling, and service-based architecture.

Version - 2.0.0

Contact me - heinthuya12@gmail.com

Buy me a coffee - KBZ Pay (Hein Thu Ya) - 09978966317

Features

  • Service-Based Architecture - Clean service class for easy integration
  • Caching Support - Reduces API calls with configurable cache duration
  • Error Handling - Robust error handling and validation
  • Dynamic Currency Support - Automatically supports all CBM currencies
  • Cache Management - Manual cache refresh capability
  • Environment Configuration - Configurable via environment variables
  • Dependency Injection - Proper Laravel service container integration

Requirements

  • PHP 7.4 or Higher
  • Laravel 8.0 or Higher (supports Laravel 8.x, 9.x, 10.x, 11.x)

Installation

composer require centralbank/exchangerates

Auto-Discovery

Laravel will automatically discover the service provider. No manual registration needed.

Manual Registration (Optional)

If auto-discovery is disabled, add the ServiceProvider in config/app.php:

'providers' => [
    Centralbank\Exchangerates\ExchangeRatesServiceProvider::class
]

Publish Configuration

php artisan vendor:publish --tag=exchangerates-config

This creates config/exchangerates-api.php for customization.

Configuration

The package can be configured via environment variables:

# API URLs (optional - defaults to CBM official endpoints)
CBM_RATES_API=https://forex.cbm.gov.mm/api/latest
CBM_CURRENCIES_API=https://forex.cbm.gov.mm/api/currencies

# Cache settings
CBM_CACHE_DURATION=3600  # Cache duration in seconds (default: 1 hour)
CBM_CACHE_KEY=cbm_exchange_rates  # Cache key prefix

# API timeout
CBM_API_TIMEOUT=30  # Request timeout in seconds

Usage Examples

Using the Service Class

In Controllers

use Centralbank\Exchangerates\Services\ExchangeRateService;

class YourController extends Controller
{
    protected $exchangeRateService;

    public function __construct(ExchangeRateService $exchangeRateService)
    {
        $this->exchangeRateService = $exchangeRateService;
    }

    public function getRates()
    {
        // Get all rates
        $allRates = $this->exchangeRateService->getAllRates();
        
        // Get specific currency
        $usdRate = $this->exchangeRateService->getRate('USD');
        
        // Get multiple currencies
        $rates = $this->exchangeRateService->getRates(['USD', 'EUR', 'GBP']);
        
        // Refresh cache
        $freshRates = $this->exchangeRateService->refreshCache();
        
        return response()->json($allRates);
    }
}

In Artisan Commands

use Centralbank\Exchangerates\Services\ExchangeRateService;

class UpdateExchangeRatesCommand extends Command
{
    protected $signature = 'exchange:update';
    
    public function handle(ExchangeRateService $exchangeRateService)
    {
        $rates = $exchangeRateService->refreshCache();
        
        if ($rates) {
            $this->info('Exchange rates updated successfully!');
        } else {
            $this->error('Failed to update exchange rates.');
        }
    }
}

Using Facades (Optional)

You can create your own facade or use dependency injection as shown above.

Service Methods

getAllRates()

Returns all available exchange rates with caching.

getRate(string $currencyCode)

Returns exchange rate for a specific currency (e.g., 'USD', 'EUR').

getRates(array $currencyCodes)

Returns exchange rates for multiple currencies.

refreshCache()

Clears cache and fetches fresh data from CBM API.

clearCache()

Clears the cached exchange rates.

Response Format

All Rates Response

{
    "info": "...",
    "description": "...",
    "timestamp": "...",
    "rates": [
        {
            "rate": 2100.00,
            "currencies": "US Dollar",
            "symbol": "USD"
        }
    ]
}

Single Currency Response

{
    "rate": 2100.00,
    "currencies": "US Dollar", 
    "symbol": "USD"
}

Changelog

v2.0.0

  • Removed all API routes and endpoints
  • Implemented comprehensive error handling
  • Added support for specific currency queries
  • Added multiple currency endpoint
  • Added cache refresh functionality
  • Dynamic currency support (no hardcoded currencies)
  • Environment-based configuration
  • Converted to service-only architecture
  • Package now provides only the ExchangeRateService class
  • Developers must create their own controllers/routes if needed
  • Cleaner, more flexible integration approach
  • Added proper dependency injection
  • Enhanced documentation

v1.0.0

  • Initial release with basic functionality

centralbank/exchangerates 适用场景与选型建议

centralbank/exchangerates 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 centralbank/exchangerates 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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