ContainerServiceProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay\Service;
  4. use Closure;
  5. use Hyperf\Pimple\ContainerFactory as DefaultContainer;
  6. use Hyperf\Utils\ApplicationContext as HyperfContainer;
  7. use Illuminate\Container\Container as LaravelContainer;
  8. use Psr\Container\ContainerInterface;
  9. use Yansongda\Pay\Contract\ServiceProviderInterface;
  10. use Yansongda\Pay\Exception\ContainerException;
  11. use Yansongda\Pay\Exception\ContainerNotFoundException;
  12. use Yansongda\Pay\Exception\Exception;
  13. use Yansongda\Pay\Pay;
  14. /**
  15. * @codeCoverageIgnore
  16. */
  17. class ContainerServiceProvider implements ServiceProviderInterface
  18. {
  19. private array $detectApplication = [
  20. 'laravel' => LaravelContainer::class,
  21. 'hyperf' => HyperfContainer::class,
  22. ];
  23. /**
  24. * @param mixed $data
  25. *
  26. * @throws ContainerException
  27. */
  28. public function register($data = null): void
  29. {
  30. if ($data instanceof ContainerInterface || $data instanceof Closure) {
  31. Pay::setContainer($data);
  32. return;
  33. }
  34. if (Pay::hasContainer()) {
  35. return;
  36. }
  37. foreach ($this->detectApplication as $framework => $application) {
  38. $method = $framework.'Application';
  39. if (class_exists($application) && method_exists($this, $method) && $this->{$method}()) {
  40. return;
  41. }
  42. }
  43. $this->defaultApplication();
  44. }
  45. /**
  46. * @throws ContainerException
  47. * @throws ContainerNotFoundException
  48. */
  49. protected function laravelApplication(): bool
  50. {
  51. Pay::setContainer(static fn () => LaravelContainer::getInstance());
  52. Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, LaravelContainer::getInstance());
  53. if (!Pay::has(ContainerInterface::class)) {
  54. Pay::set(ContainerInterface::class, LaravelContainer::getInstance());
  55. }
  56. return true;
  57. }
  58. /**
  59. * @throws ContainerException
  60. * @throws ContainerNotFoundException
  61. */
  62. protected function hyperfApplication(): bool
  63. {
  64. if (!HyperfContainer::hasContainer()) {
  65. return false;
  66. }
  67. Pay::setContainer(static fn () => HyperfContainer::getContainer());
  68. Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, HyperfContainer::getContainer());
  69. if (!Pay::has(ContainerInterface::class)) {
  70. Pay::set(ContainerInterface::class, HyperfContainer::getContainer());
  71. }
  72. return true;
  73. }
  74. /**
  75. * @throws ContainerNotFoundException
  76. */
  77. protected function defaultApplication(): void
  78. {
  79. if (!class_exists(DefaultContainer::class)) {
  80. throw new ContainerNotFoundException('Init failed! Maybe you should install `hyperf/pimple` first', Exception::CONTAINER_NOT_FOUND);
  81. }
  82. $container = (new DefaultContainer())();
  83. Pay::setContainer($container);
  84. }
  85. }