Passed
Push — master ( 391e16...92d93f )
by Albert
04:12 queued 02:12
created

WithApplication::getBootstrappers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A WithApplication::setFramework() 0 9 2
1
<?php
2
3
namespace SwooleTW\Http\Concerns;
4
5
use Illuminate\Contracts\Http\Kernel;
6
use Illuminate\Support\Facades\Facade;
7
use Illuminate\Contracts\Container\Container;
8
use SwooleTW\Http\Exceptions\FrameworkNotSupportException;
9
10
/**
11
 * Trait WithApplication
12
 *
13
 * @property Container $container
14
 * @property string $framework
15
 */
16
trait WithApplication
17
{
18
    /**
19
     * Laravel|Lumen Application.
20
     *
21
     * @var \Illuminate\Contracts\Container\Container|mixed
22
     */
23
    protected $app;
24
25
    /**
26
     * Bootstrap framework.
27
     */
28
    protected function bootstrap()
29
    {
30
        if ($this->framework === 'laravel') {
31
            $this->app->bootstrap();
0 ignored issues
show
Bug introduced by
The method bootstrap() does not exist on Illuminate\Contracts\Container\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            $this->app->/** @scrutinizer ignore-call */ 
32
                        bootstrap();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
        } else {
33
            // for Lumen 5.7
34
            // https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837
35
            if (method_exists($this->app, 'boot')) {
36
                $this->app->boot();
37
            }
38
            if (is_null(Facade::getFacadeApplication())) {
39
                $this->app->withFacades();
0 ignored issues
show
Bug introduced by
The method withFacades() does not exist on Illuminate\Contracts\Container\Container. It seems like you code against a sub-type of Illuminate\Contracts\Container\Container such as Laravel\Lumen\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
                $this->app->/** @scrutinizer ignore-call */ 
40
                            withFacades();
Loading history...
40
            }
41
        }
42
43
        $this->preResolveInstances();
44
    }
45
46
    /**
47
     * Load application.
48
     *
49
     * @return \Illuminate\Contracts\Foundation\Application
50
     */
51
    protected function loadApplication()
52
    {
53
        return require "{$this->basePath}/bootstrap/app.php";
54
    }
55
56
    /**
57
     * @return \Illuminate\Contracts\Container\Container|mixed
58
     * @throws \ReflectionException
59
     */
60
    public function getApplication()
61
    {
62
        if (! $this->app instanceof Container) {
63
            $this->app = $this->loadApplication();
64
            $this->bootstrap();
65
        }
66
67
        return $this->app;
68
    }
69
70
    /**
71
     * Set laravel application.
72
     *
73
     * @param \Illuminate\Contracts\Container\Container $app
74
     */
75
    public function setApplication(Container $app)
76
    {
77
        $this->app = $app;
78
    }
79
80
    /**
81
     * Set framework.
82
     *
83
     * @param string $framework
84
     *
85
     * @throws \Exception
86
     */
87
    protected function setFramework($framework)
88
    {
89
        $framework = strtolower($framework);
90
91
        if (! in_array($framework, ['laravel', 'lumen'])) {
92
            throw new FrameworkNotSupportException($framework);
93
        }
94
95
        $this->framework = $framework;
96
    }
97
98
    /**
99
     * Get framework.
100
     */
101
    public function getFramework()
102
    {
103
        return $this->framework;
104
    }
105
106
    /**
107
     * Set base path.
108
     *
109
     * @param string $basePath
110
     */
111
    protected function setBasePath($basePath)
112
    {
113
        $this->basePath = is_null($basePath) ? base_path() : $basePath;
0 ignored issues
show
introduced by
The condition is_null($basePath) is always false.
Loading history...
Bug Best Practice introduced by
The property basePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
    }
115
116
    /**
117
     * Get basepath.
118
     */
119
    public function getBasePath()
120
    {
121
        return $this->basePath;
122
    }
123
124
    /**
125
     * Reslove some instances before request.
126
     *
127
     * @throws \ReflectionException
128
     */
129
    protected function preResolveInstances()
130
    {
131
        $resolves = $this->container->make('config')
132
            ->get('swoole_http.pre_resolved', []);
133
134
        foreach ($resolves as $abstract) {
135
            if ($this->getApplication()->offsetExists($abstract)) {
136
                $this->getApplication()->make($abstract);
137
            }
138
        }
139
    }
140
}
141