Passed
Pull Request — master (#675)
by Songda
02:04
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;
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
66
        return true;
67
    }
68
69
    /**
70
     * @throws \Yansongda\Pay\Exception\ContainerException
71
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
72
     */
73
    protected function hyperfApplication(): bool
74
    {
75
        if (!HyperfApplication::hasContainer()) {
76
            return false;
77
        }
78
79
        Pay::setContainer(static fn () => HyperfApplication::getContainer());
80
81
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, HyperfApplication::getContainer());
82
        Pay::set(ContainerInterface::class, HyperfApplication::getContainer());
83
84
        return true;
85
    }
86
87
    /**
88
     * @throws \Yansongda\Pay\Exception\ContainerException
89
     * @throws \Yansongda\Pay\Exception\ContainerNotFoundException
90
     */
91
    protected function thinkphpApplication(): bool
92
    {
93
        Pay::setContainer(static fn () => ThinkPHPApplication::getInstance());
94
95
        Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, ThinkPHPApplication::getInstance());
96
        Pay::set(ContainerInterface::class, ThinkPHPApplication::getInstance());
97
98
        return true;
99
    }
100
101
    /**
102
     * @throws \Yansongda\Pay\Exception\ContainerException
103
     */
104
    protected function defaultApplication(): void
105
    {
106
        if (!class_exists(ContainerBuilder::class)) {
107
            throw new ContainerNotFoundException('Init failed! Maybe you should install `php-di/php-di` first', Exception::CONTAINER_NOT_FOUND);
108
        }
109
110
        $builder = new ContainerBuilder();
111
112
        try {
113
            $container = $builder->build();
114
            $container->set(ContainerInterface::class, $container);
115
            $container->set(\Yansongda\Pay\Contract\ContainerInterface::class, $container);
116
117
            Pay::setContainer($container);
118
        } catch (Throwable $e) {
119
            throw new ContainerException($e->getMessage());
120
        }
121
    }
122
}
123