dimanche 22 septembre 2019

How to use Laravel facades (Cache, Log, Storage) in package outside Laravel

Please point out any naivete or incorrect assumptions I'm making about Laravel, Composer, PHPUnit, etc.

I had a class called SpeechToTextHelper that was inside a Laravel project, and it used facades like this:

use Cache;
use Log;
use Storage;

Then, since I wanted to share it between multiple Laravel projects, I moved it into a separate repo and required it (into the first project) as a dependency via Composer.

The code all seems to run fine.

My question is different from Using Laravel Facades outside Laravel

What I want to know is:

  1. Now that I also want to write PHPUnit tests for SpeechToTextHelper in my new tools repo, I see errors like RuntimeException: A facade root has not been set. and Error: Class 'Log' not found, presumably because this tools repo has no awareness of Laravel. I guess this means my production code has been working just by side-effect.
  2. In my new tools repo (where my SpeechToTextHelper now is), how am I supposed to indicate (maybe somewhere in composer.json?) that the code will only work if Laravel's facades exist and are initiated properly?

How can I fix my separate repo's code so that its tests can run and also so that it ensures that it can only be "required" by a Laravel project?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire