Before answering, I recommend to fork the package, do your modifications and use your fork. Because if something change in the package, your override may not work anymore.
Let's take a look at the package.
You have 3 important files :
The currency that you want to extend:
https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/Currency.php
The facade that you want to use :
https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/Facades/Currency.php
And finally, the ServiceProvider that register the class you want to extends:
https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/CurrencyServiceProvider.php#L60
The service provider will register the class
Currency as a singleton with the alias of currency
Then, when you call the facade
Currency, it will look for the alias currency
and return a the instance of the class
Currency.
Implement your own Currency
To use your own Currency class
, you will need to register your own implementation of the Currency class
in a service provider that will replace the service provider of the package.
Create your own serviceProvider
$ php artisan make:provider ExtendedCurrencyServiceProvider
In your file app/config/app.php
,
Replace Casinelli\Currency\CurrencyServiceProvider::class,
with App\Providers\ExtendedCurrencyServiceProvider::class,
- In your new service provider change to this
<?php
namespace App\Providers;
use Casinelli\Currency\CurrencyServiceProvider;
class ExtendedCurrencyServiceProvider extends CurrencyServiceProvider
{
/**
* Register currency provider.
*/
public function registerCurrency()
{
$this->app->singleton('currency', function ($app) {
return new App\Yournamespace\CurrencyClass($app);
});
}
}
- Laravel 5.5+ In your
composer.json
remove the service provider from the autodiscovery
"extra": {
"laravel": {
"dont-discover": [
"Casinelli\\Currency\\CurrencyServiceProvider"
]
}
},
Now, when you will call \Currency::rounded()
it will call your own implementation of the currency.
You don't need to change the Facade.