Passed
Push — master ( ecbb01...bd6415 )
by Albert
04:49 queued 02:14
created

src/Concerns/WithApplication.php (2 issues)

Labels
Severity
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
            $bootstrappers = $this->getBootstrappers();
32
            $this->app->bootstrapWith($bootstrappers);
0 ignored issues
show
The method bootstrapWith() does not exist on Illuminate\Contracts\Container\Container. It seems like you code against a sub-type of Illuminate\Contracts\Container\Container such as Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

32
            $this->app->/** @scrutinizer ignore-call */ 
33
                        bootstrapWith($bootstrappers);
Loading history...
The method bootstrapWith() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean bootstrapWith()? ( Ignorable by Annotation )

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

32
            $this->app->/** @scrutinizer ignore-call */ 
33
                        bootstrapWith($bootstrappers);

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...
33
        } else {
34
            // for Lumen 5.7
35
            // https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837
36
            if (method_exists($this->app, 'boot')) {
37
                $this->app->boot();
38
            }
39
            if (is_null(Facade::getFacadeApplication())) {
40
                $this->app->withFacades();
41
            }
42
        }
43
44
        $this->preResolveInstances();
45
    }
46
47
    /**
48
     * Load application.
49
     *
50
     * @return \Illuminate\Contracts\Foundation\Application
51
     */
52
    protected function loadApplication()
53
    {
54
        return require "{$this->basePath}/bootstrap/app.php";
55
    }
56
57
    /**
58
     * @return \Illuminate\Contracts\Container\Container|mixed
59
     * @throws \ReflectionException
60
     */
61
    public function getApplication()
62
    {
63
        if (! $this->app instanceof Container) {
64
            $this->app = $this->loadApplication();
65
            $this->bootstrap();
66
        }
67
68
        return $this->app;
69
    }
70
71
    /**
72
     * Set laravel application.
73
     *
74
     * @param \Illuminate\Contracts\Container\Container $app
75
     */
76
    public function setApplication(Container $app)
77
    {
78
        $this->app = $app;
79
    }
80
81
    /**
82
     * Set framework.
83
     *
84
     * @param string $framework
85
     *
86
     * @throws \Exception
87
     */
88
    protected function setFramework($framework)
89
    {
90
        $framework = strtolower($framework);
91
92
        if (! in_array($framework, ['laravel', 'lumen'])) {
93
            throw new FrameworkNotSupportException($framework);
94
        }
95
96
        $this->framework = $framework;
97
    }
98
99
    /**
100
     * Get framework.
101
     */
102
    public function getFramework()
103
    {
104
        return $this->framework;
105
    }
106
107
    /**
108
     * Set base path.
109
     *
110
     * @param string $basePath
111
     */
112
    protected function setBasePath($basePath)
113
    {
114
        $this->basePath = is_null($basePath) ? base_path() : $basePath;
115
    }
116
117
    /**
118
     * Get basepath.
119
     */
120
    public function getBasePath()
121
    {
122
        return $this->basePath;
123
    }
124
125
    /**
126
     * Reslove some instances before request.
127
     *
128
     * @throws \ReflectionException
129
     */
130
    protected function preResolveInstances()
131
    {
132
        $resolves = $this->container->make('config')
133
            ->get('swoole_http.pre_resolved', []);
134
135
        foreach ($resolves as $abstract) {
136
            if ($this->getApplication()->offsetExists($abstract)) {
137
                $this->getApplication()->make($abstract);
138
            }
139
        }
140
    }
141
142
    /**
143
     * Get bootstrappers.
144
     *
145
     * @return array
146
     * @throws \ReflectionException
147
     */
148
    protected function getBootstrappers()
149
    {
150
        $kernel = $this->getApplication()->make(Kernel::class);
151
152
        $reflection = new \ReflectionObject($kernel);
153
        $bootstrappersMethod = $reflection->getMethod('bootstrappers');
154
        $bootstrappersMethod->setAccessible(true);
155
        $bootstrappers = $bootstrappersMethod->invoke($kernel);
156
157
        array_splice($bootstrappers, -2, 0, ['Illuminate\Foundation\Bootstrap\SetRequestForConsole']);
158
159
        return $bootstrappers;
160
    }
161
}
162