1
0
Fork 0
mirror of https://github.com/archtechx/money.git synced 2025-12-12 03:14:03 +00:00

Allow customization of default CurrencyManager

This commit is contained in:
Mikael Dalholm 2021-12-28 18:05:09 +01:00
parent 4474c2b115
commit 35304109e5
5 changed files with 71 additions and 1 deletions

View file

@ -525,6 +525,26 @@ currencies()->add(cache()->remember('currencies', 3600, function () {
Where the DB call returns an array of array currencies following the [format mentioned above](#creating-a-currency).
## Customization
Optionally you could publish the config and replace the `CurrencyManager::class` with your own.
```bash
php artisan vendor:publish --tag="money-config"
```
```php
// config/money.php
<?php
return [
//...
'manager' => \ArchTech\Money\CurrencyManager::class, // replace with your own
];
```
## Development & contributing
Run all checks locally:

13
config/config.php Normal file
View file

@ -0,0 +1,13 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Currency Manager
|--------------------------------------------------------------------------
| You can customize CurrencyManager as you wish.
| Be sure to extend the CurrencyManager class as you do so.
|
*/
'manager' => \ArchTech\Money\CurrencyManager::class,
];

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace ArchTech\Money\Exceptions;
use ArchTech\Money\CurrencyManager;
use Exception;
class MissingCurrencyManagerExtensionException extends Exception
{
public function __construct(string $className)
{
parent::__construct("Missing extension: The {$className} must extend ".CurrencyManager::class.".");
}
}

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ArchTech\Money;
use ArchTech\Money\Exceptions\MissingCurrencyManagerExtensionException;
use Illuminate\Support\ServiceProvider;
class MoneyServiceProvider extends ServiceProvider
@ -11,5 +12,25 @@ class MoneyServiceProvider extends ServiceProvider
public function register(): void
{
$this->app->singleton(CurrencyManager::class);
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'money');
$class = $this->app->make(config('money.manager'));
/**
* Make sure that the CurrencyManager is loaded as expected
*/
if (!is_a($class, CurrencyManager::class)) {
throw new MissingCurrencyManagerExtensionException($class::class);
}
$this->app->singleton($class::class);
}
public function boot()
{
$this->publishes([
__DIR__ . '/../config/config.php' => config_path('money.php'),
], 'money-config');
}
}

View file

@ -32,6 +32,6 @@ if (! function_exists('currencies')) {
/** Get the CurrencyManager instance. */
function currencies(): CurrencyManager
{
return app(CurrencyManager::class);
return app(config('money.manager'));
}
}