WithApplication   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 35
c 1
b 1
f 0
dl 0
loc 142
rs 10
wmc 17

10 Methods

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