TestableInstant.php 1013 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Traits;
  3. use App\Services\LogService;
  4. use Illuminate\Contracts\Foundation\Application;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. trait TestableInstant
  8. {
  9. /**
  10. * 实例化一个service的入口,为了测试方便做出来的,这样测试时可以代入假的柱件替换service的实现
  11. * 应当在一般性用app的地方换成本方法才可以做到使用柱件
  12. * @param $targetService
  13. * @param $abstractClassName
  14. * @return Application|mixed|string
  15. */
  16. public function instant(&$targetService, $abstractClassName){
  17. if(!is_string($targetService)){
  18. if(empty($targetService))
  19. $targetService=app($abstractClassName);
  20. return $targetService;
  21. }
  22. if(!is_string($abstractClassName)){
  23. $this->$targetService=$abstractClassName;
  24. }else{
  25. $this->$targetService=app($abstractClassName);
  26. }
  27. return $targetService;
  28. }
  29. }