Completed
Branch master (9dff9d)
by Albert
05:30
created

WithApplication   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 129
rs 10
c 0
b 0
f 0
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 7 3
A setBasePath() 0 3 2
A getBootstrappers() 0 14 1
A bootstrap() 0 10 3
1
<?php
2
3
namespace SwooleTW\Http\Concerns;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Http\Kernel;
7
use Illuminate\Support\Facades\Facade;
8
use Illuminate\Contracts\Container\Container;
9
use Laravel\Lumen\Application as LumenApplication;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
use Symfony\Component\HttpFoundation\BinaryFileResponse;
12
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
13
14
trait WithApplication
15
{
16
    /**
17
     * Laravel|Lumen Application.
18
     *
19
     * @var \Illuminate\Contracts\Container\Container
20
     */
21
    protected $app;
22
23
    /**
24
     * Bootstrap framework.
25
     */
26
    protected function bootstrap()
27
    {
28
        if ($this->framework === 'laravel') {
29
            $bootstrappers = $this->getBootstrappers();
30
            $this->app->bootstrapWith($bootstrappers);
0 ignored issues
show
Bug introduced by
The method bootstrapWith() 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

30
            $this->app->/** @scrutinizer ignore-call */ 
31
                        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...
31
        } elseif (is_null(Facade::getFacadeApplication())) {
32
            $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

32
            $this->app->/** @scrutinizer ignore-call */ 
33
                        withFacades();
Loading history...
33
        }
34
35
        $this->preResolveInstances();
36
    }
37
38
    /**
39
     * Load application.
40
     *
41
     * @return \Illuminate\Contracts\Foundation\Application
42
     */
43
    protected function loadApplication()
44
    {
45
        return require $this->basePath . '/bootstrap/app.php';
46
    }
47
48
    /**
49
     * @return \Illuminate\Contracts\Container\Container
50
     */
51
    public function getApplication()
52
    {
53
        if (! $this->app instanceof Container) {
0 ignored issues
show
introduced by
$this->app is always a sub-type of Illuminate\Contracts\Container\Container. If $this->app can have other possible types, add them to src/Concerns/WithApplication.php:19.
Loading history...
54
            $this->app = $this->loadApplication();
55
            $this->bootstrap();
56
        }
57
58
        return $this->app;
59
    }
60
61
    /**
62
     * Set laravel application.
63
     */
64
    public function setApplication(Container $app)
65
    {
66
        $this->app = $app;
67
    }
68
69
    /**
70
     * Get bootstrappers.
71
     *
72
     * @return array
73
     */
74
    protected function getBootstrappers()
75
    {
76
        $kernel = $this->getApplication()->make(Kernel::class);
77
78
        $reflection = new \ReflectionObject($kernel);
79
80
        $bootstrappersMethod = $reflection->getMethod('bootstrappers');
81
        $bootstrappersMethod->setAccessible(true);
82
83
        $bootstrappers = $bootstrappersMethod->invoke($kernel);
84
85
        array_splice($bootstrappers, -2, 0, ['Illuminate\Foundation\Bootstrap\SetRequestForConsole']);
86
87
        return $bootstrappers;
88
    }
89
90
    /**
91
     * Set framework.
92
     *
93
     * @param string $framework
94
     * @throws \Exception
95
     */
96
    protected function setFramework($framework)
97
    {
98
        $framework = strtolower($framework);
99
100
        if (! in_array($framework, ['laravel', 'lumen'])) {
101
            throw new \Exception(sprintf('Not support framework "%s".', $framework));
102
        }
103
104
        $this->framework = $framework;
0 ignored issues
show
Bug Best Practice introduced by
The property framework does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
105
    }
106
107
    /**
108
     * Get framework.
109
     */
110
    public function getFramework()
111
    {
112
        return $this->framework;
113
    }
114
115
    /**
116
     * Set base path.
117
     *
118
     * @param string $basePath
119
     */
120
    protected function setBasePath($basePath)
121
    {
122
        $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...
123
    }
124
125
    /**
126
     * Get basepath.
127
     */
128
    public function getBasePath()
129
    {
130
        return $this->basePath;
131
    }
132
133
    /**
134
     * Reslove some instances before request.
135
     */
136
    protected function preResolveInstances()
137
    {
138
        $resolves = $this->container['config']->get('swoole_http.pre_resolved', []);
139
140
        foreach ($resolves as $abstract) {
141
            if ($this->getApplication()->offsetExists($abstract)) {
142
                $this->getApplication()->make($abstract);
143
            }
144
        }
145
    }
146
}
147