Passed
Pull Request — master (#675)
by Songda
01:50
created

ContainerServiceProvider::thinkphpApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
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 LaravelApplication;
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 ThinkPHPApplication;
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
/**
21
 * @codeCoverageIgnore
22
 */
23
class ContainerServiceProvider implements ServiceProviderInterface
24
{
25
    private $detectApplication = [
26
        'laravel' => LaravelApplication::class,
27
        'hyperf' => HyperfApplication::class,
28
        'thinkphp' => ThinkPHPApplication::class,
29
    ];
30
31
    /**
32
     * @throws \Yansongda\Pay\Exception\ContainerException
33
     */
34
    public function register($data = null): void
35
    {
36
        if ($data instanceof ContainerInterface || $data instanceof Closure) {
37
            Pay::setContainer($data);
38
39
            return;
40
        }
41
42
        if (Pay::hasContainer()) {
43
            return;
44
        }
45
46
        foreach ($this->detectApplication as $framework => $application) {
47
            $method = $framework.'Application';
48
49
            if (class_exists($application) && method_exists($this, $method) && $this->{$method}()) {
50
                return;
51
            }
52
        }
53
54
        $this->defaultApplication();
55
    }
56
57
    /**
58
     * @throws \Yansongda\Pay\Exception\ContainerException
59
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
60
     */
61
    protected function laravelApplication(): bool
62
    {
63
        Pay::setContainer(static fn () => LaravelApplication::getInstance());
64
65
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, LaravelApplication::getInstance());
66
67
        if (!Pay::has(ContainerInterface::class)) {
68
            Pay::set(ContainerInterface::class, LaravelApplication::getInstance());
69
        }
70
71
        return true;
72
    }
73
74
    /**
75
     * @throws \Yansongda\Pay\Exception\ContainerException
76
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
77
     */
78
    protected function hyperfApplication(): bool
79
    {
80
        if (!HyperfApplication::hasContainer()) {
81
            return false;
82
        }
83
84
        Pay::setContainer(static fn () => HyperfApplication::getContainer());
85
86
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, HyperfApplication::getContainer());
87
88
        if (!Pay::has(ContainerInterface::class)) {
89
            Pay::set(ContainerInterface::class, HyperfApplication::getInstance());
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * @throws \Yansongda\Pay\Exception\ContainerException
97
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
98
     */
99
    protected function thinkphpApplication(): bool
100
    {
101
        Pay::setContainer(static fn () => ThinkPHPApplication::getInstance());
102
103
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, ThinkPHPApplication::getInstance());
104
105
        if (!Pay::has(ContainerInterface::class)) {
106
            Pay::set(ContainerInterface::class, ThinkPHPApplication::getInstance());
107
        }
108
109
        return true;
110
    }
111
112
    /**
113
     * @throws \Yansongda\Pay\Exception\ContainerException
114
     */
115
    protected function defaultApplication(): void
116
    {
117
        if (!class_exists(ContainerBuilder::class)) {
118
            throw new ContainerNotFoundException('Init failed! Maybe you should install `php-di/php-di` first', Exception::CONTAINER_NOT_FOUND);
119
        }
120
121
        $builder = new ContainerBuilder();
122
123
        try {
124
            $container = $builder->build();
125
            $container->set(ContainerInterface::class, $container);
126
            $container->set(\Yansongda\Pay\Contract\ContainerInterface::class, $container);
127
128
            Pay::setContainer($container);
129
        } catch (Throwable $e) {
130
            throw new ContainerException($e->getMessage());
131
        }
132
    }
133
}
134