Passed
Pull Request — master (#579)
by Songda
03:10
created

ContainerServiceProvider::hyperfApplication()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Service;
6
7
use Closure;
8
use DI\ContainerBuilder;
0 ignored issues
show
Bug introduced by
The type DI\ContainerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Hyperf\Utils\ApplicationContext as HyperfApplication;
0 ignored issues
show
Bug introduced by
The type Hyperf\Utils\ApplicationContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Container\Container as LaravelContainer;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Container\ContainerInterface;
12
use think\Container as ThinkContainer;
0 ignored issues
show
Bug introduced by
The type think\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Throwable;
14
use Yansongda\Pay\Contract\ServiceProviderInterface;
15
use Yansongda\Pay\Exception\ContainerException;
16
use Yansongda\Pay\Exception\ContainerNotFoundException;
17
use Yansongda\Pay\Exception\Exception;
18
use Yansongda\Pay\Pay;
19
20
class ContainerServiceProvider implements ServiceProviderInterface
21
{
22
    private $detectApplication = [
23
        'laravel' => LaravelContainer::class,
24
        'think' => ThinkContainer::class,
25
        'hyperf' => HyperfApplication::class,
26
    ];
27
28
    /**
29
     * @throws \Yansongda\Pay\Exception\ContainerException
30
     */
31
    public function register($data = null): void
32
    {
33
        if ($data instanceof ContainerInterface || $data instanceof Closure) {
34
            Pay::setContainer($data);
35
36
            return;
37
        }
38
39
        foreach ($this->detectApplication as $framework => $application) {
40
            if (class_exists($application) && $this->{$framework.'Application'}()) {
41
                return;
42
            }
43
        }
44
45
        $this->defaultApplication();
46
    }
47
48
    /**
49
     * @throws \Yansongda\Pay\Exception\ContainerException
50
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
51
     */
52
    protected function laravelApplication(): bool
53
    {
54
        Pay::setContainer(static function () {
55
            return LaravelContainer::getInstance();
56
        });
57
58
        if (!Pay::has(ContainerInterface::class)) {
59
            Pay::set(ContainerInterface::class, LaravelContainer::getInstance());
60
        }
61
62
        return true;
63
    }
64
65
    /**
66
     * @throws \Yansongda\Pay\Exception\ContainerException
67
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
68
     */
69
    protected function thinkApplication(): bool
70
    {
71
        Pay::setContainer(static function () {
72
            return ThinkContainer::getInstance();
73
        });
74
75
        if (!Pay::has(ContainerInterface::class)) {
76
            Pay::set(ContainerInterface::class, ThinkContainer::getInstance());
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * @throws \Yansongda\Pay\Exception\ContainerException
84
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
85
     */
86
    protected function hyperfApplication(): bool
87
    {
88
        if (!HyperfApplication::hasContainer()) {
89
            return false;
90
        }
91
92
        Pay::setContainer(static function () {
93
            return HyperfApplication::getContainer();
94
        });
95
96
        if (!Pay::has(ContainerInterface::class)) {
97
            Pay::set(ContainerInterface::class, HyperfApplication::getInstance());
98
        }
99
100
        return true;
101
    }
102
103
    /**
104
     * @throws \Yansongda\Pay\Exception\ContainerException
105
     */
106
    protected function defaultApplication(): void
107
    {
108
        if (!class_exists(ContainerBuilder::class)) {
109
            throw new ContainerNotFoundException('Init failed! Maybe you should install `php-di/php-di` first', Exception::CONTAINER_NOT_FOUND);
110
        }
111
112
        $builder = new ContainerBuilder();
113
114
        try {
115
            $container = $builder->build();
116
            $container->set(ContainerInterface::class, $container);
117
            $container->set(\Yansongda\Pay\Contract\ContainerInterface::class, $container);
118
119
            Pay::setContainer($container);
120
        } catch (Throwable $e) {
121
            throw new ContainerException($e->getMessage());
122
        }
123
    }
124
}
125