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

ContainerServiceProvider::thinkphpApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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