From 35304109e52e9398e450ecb983f10cf96df8ab43 Mon Sep 17 00:00:00 2001 From: Mikael Dalholm Date: Tue, 28 Dec 2021 18:05:09 +0100 Subject: [PATCH] Allow customization of default CurrencyManager --- README.md | 20 ++++++++++++++++++ config/config.php | 13 ++++++++++++ ...ssingCurrencyManagerExtensionException.php | 16 ++++++++++++++ src/MoneyServiceProvider.php | 21 +++++++++++++++++++ src/helpers.php | 2 +- 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 config/config.php create mode 100644 src/Exceptions/MissingCurrencyManagerExtensionException.php diff --git a/README.md b/README.md index 7a76b0c..51eb6dd 100644 --- a/README.md +++ b/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). + +## 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 + \ArchTech\Money\CurrencyManager::class, // replace with your own +]; + +``` + + ## Development & contributing Run all checks locally: diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..57f677a --- /dev/null +++ b/config/config.php @@ -0,0 +1,13 @@ + \ArchTech\Money\CurrencyManager::class, +]; \ No newline at end of file diff --git a/src/Exceptions/MissingCurrencyManagerExtensionException.php b/src/Exceptions/MissingCurrencyManagerExtensionException.php new file mode 100644 index 0000000..0a6b1d5 --- /dev/null +++ b/src/Exceptions/MissingCurrencyManagerExtensionException.php @@ -0,0 +1,16 @@ +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'); } } diff --git a/src/helpers.php b/src/helpers.php index 9fa6f61..acfbba5 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,6 +32,6 @@ if (! function_exists('currencies')) { /** Get the CurrencyManager instance. */ function currencies(): CurrencyManager { - return app(CurrencyManager::class); + return app(config('money.manager')); } }