florianv / exchanger

:office: Currency exchange rates framework for PHP

Home Page:https://florianv.github.io/exchanger/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RuntimeException: The service "currency_converter" does not exist (using laravel-swap)

alies-dev opened this issue · comments

Note that this error happened on using of currently undocumented functionality (florianv/laravel-swap v1.3.0 docs doesn't contain any info about currency_converter service but its dependency florianv/exchanger v1.3.0 includes this service). Thus, this issue is not critical right now

dependency tree:

  1. florianv/laravel-swap: ~1.0 (1.3.0 installed, v1.3.0 is the latest available)
    1. florianv/swap: "^3.4", (3.5.0 installed, 3.5.0 is the latest available)
      1. florianv/exchanger: "^1.1" (1.3.0 installed, 1.3.0 is the latest available)

I know, it's not a laravel-swap package repo, but this issue should be fixed on this repo (IMO).

Stacktrace

RuntimeException: The service "currency_converter" does not exist. in ../vendor/florianv/laravel-swap/src/SwapServiceProvider.php:105
Stack trace:
#0 ../vendor/florianv/laravel-swap/src/SwapServiceProvider.php(128): Swap\Laravel\SwapServiceProvider->registerServices(Object(Illuminate\Foundation\Application))
#1 ../vendor/laravel/framework/src/Illuminate/Container/Container.php(776): Swap\Laravel\SwapServiceProvider->Swap\Laravel\{closure}(Object(Illuminate\Foundation\Application), Array)
...

Cause of the problem

There is a getServiceClass method from the laravel-swap package https://github.com/florianv/laravel-swap/blob/1.3.0/src/SwapServiceProvider.php#L201-L210 that trying to find a class using its alias. This method can't resolve the classname because alias is currency_converter but classname is CurrencyConverterApi

Solution(s)

  1. Easy/quick-fix and (almost)non-breaking: Rename CurrencyConverterApi to CurrencyConverter so getServiceClass() will be able to resolve class
  2. Easy and breaking: rename currency_converter to currency_converter_api
  3. Comprehensive: refactor alias to FQCN mapping to make it more flexible
  4. Ugly and 100% non-breaking: add a new if condition to getServiceClass()

I think 1st option is the best one because:

  1. I have enough time to fix it right now
  2. It's (almost)non-breaking (laravel-swap v1.3 doesn't contain docs about CurrencyConverterApi service)

Todo

  1. Add test contact to instantiate all available services by their aliases
  2. Try to find a time to refactor laravel-swap and symfony-swap to have more flexible way to add new services (and add custom services to ANY place in the chain (now it's possible to add them at the top of the chain (at least for laravel-swap)))

I am closing the issue since the PR was merged in the master branch (will be released soon).

Thanks for your work @lptn 👍