mirror of
https://github.com/archtechx/money.git
synced 2025-12-12 11:24:03 +00:00
Allow customization of default CurrencyManager
This commit is contained in:
parent
4474c2b115
commit
35304109e5
5 changed files with 71 additions and 1 deletions
20
README.md
20
README.md
|
|
@ -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).
|
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
|
## Development & contributing
|
||||||
|
|
||||||
Run all checks locally:
|
Run all checks locally:
|
||||||
|
|
|
||||||
13
config/config.php
Normal file
13
config/config.php
Normal 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,
|
||||||
|
];
|
||||||
16
src/Exceptions/MissingCurrencyManagerExtensionException.php
Normal file
16
src/Exceptions/MissingCurrencyManagerExtensionException.php
Normal 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.".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ArchTech\Money;
|
namespace ArchTech\Money;
|
||||||
|
|
||||||
|
use ArchTech\Money\Exceptions\MissingCurrencyManagerExtensionException;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class MoneyServiceProvider extends ServiceProvider
|
class MoneyServiceProvider extends ServiceProvider
|
||||||
|
|
@ -11,5 +12,25 @@ class MoneyServiceProvider extends ServiceProvider
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(CurrencyManager::class);
|
$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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,6 @@ if (! function_exists('currencies')) {
|
||||||
/** Get the CurrencyManager instance. */
|
/** Get the CurrencyManager instance. */
|
||||||
function currencies(): CurrencyManager
|
function currencies(): CurrencyManager
|
||||||
{
|
{
|
||||||
return app(CurrencyManager::class);
|
return app(config('money.manager'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue