Passed
Branch feature/coroutine_feature (16a200)
by Albert
02:06
created

ResetApplication::clearInstances()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Concerns;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Http\Kernel;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
trait ResetApplication
10
{
11
    /**
12
     * Clear resolved instances.
13
     */
14
    public function clearInstances(Container $app)
15
    {
16
        $instances = $this->config->get('swoole_http.instances', []);
17
        foreach ($instances as $instance) {
18
            $app->forgetInstance($instance);
19
        }
20
    }
21
22
    /**
23
     * Bind illuminate request to laravel/lumen application.
24
     */
25
    public function bindRequest(Container $app)
26
    {
27
        $request = $this->getRequest();
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-call */ 
28
        $request = $this->getRequest();
Loading history...
28
        if ($request instanceof Request) {
0 ignored issues
show
Bug introduced by
The type SwooleTW\Http\Concerns\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
29
            $app->instance('request', $request);
30
        }
31
    }
32
33
    /**
34
     * Re-register and reboot service providers.
35
     */
36
    public function resetProviders(Container $app)
37
    {
38
        foreach ($this->providers as $provider) {
39
            $this->rebindProviderContainer($app, $provider);
40
            if (method_exists($provider, 'register')) {
41
                $provider->register();
42
            }
43
            if (method_exists($provider, 'boot')) {
44
                $app->call([$provider, 'boot']);
45
            }
46
        }
47
    }
48
49
    /**
50
     * Rebind service provider's container.
51
     */
52
    protected function rebindProviderContainer($app, $provider)
53
    {
54
        $closure = function () use ($app) {
55
            $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        };
57
58
        $resetProvider = $closure->bindTo($provider, $provider);
59
        $resetProvider();
60
    }
61
62
    /**
63
     * Reset laravel/lumen's config to initial values.
64
     */
65
    public function resetConfigInstance(Container $app)
66
    {
67
        $app->instance('config', clone $this->config);
68
    }
69
70
    /**
71
     * Reset laravel's session data.
72
     */
73
    public function resetSession(Container $app)
74
    {
75
        if (isset($app['session'])) {
76
            $session = $app->make('session');
77
            $session->flush();
78
            $session->regenerate();
79
        }
80
    }
81
82
    /**
83
     * Reset laravel's cookie.
84
     */
85
    public function resetCookie(Container $app)
86
    {
87
        if (isset($app['cookie'])) {
88
            $cookies = $app->make('cookie');
89
            foreach ($cookies->getQueuedCookies() as $key => $value) {
90
                $cookies->unqueue($key);
91
            }
92
        }
93
    }
94
95
    /**
96
     * Rebind laravel's container in router.
97
     */
98
    public function rebindRouterContainer(Container $app)
99
    {
100
        if ($this->isLaravel()) {
0 ignored issues
show
Bug introduced by
It seems like isLaravel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

100
        if ($this->/** @scrutinizer ignore-call */ isLaravel()) {
Loading history...
101
            $router = $app->make('router');
102
            $request = $this->getRequest();
103
            $closure = function () use ($app, $request) {
104
                $this->container = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
105
                if (is_null($request)) {
106
                    return;
107
                }
108
                try {
109
                    $route = $this->routes->match($request);
110
                    // clear resolved controller
111
                    if (property_exists($route, 'container')) {
112
                        $route->controller = null;
113
                    }
114
                    // rebind matched route's container
115
                    $route->setContainer($app);
116
                } catch (NotFoundHttpException $e) {
117
                    // do nothing
118
                }
119
            };
120
121
            $resetRouter = $closure->bindTo($router, $router);
122
            $resetRouter();
123
        } else {
124
            // lumen router only exists after lumen 5.5
125
            if (property_exists($app, 'router')) {
126
                $app->router->app = $app;
0 ignored issues
show
Documentation Bug introduced by
$app is of type Illuminate\Container\Container&object, but the property $app was declared to be of type Laravel\Lumen\Application. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
127
            }
128
        }
129
    }
130
131
    /**
132
     * Rebind laravel/lumen's container in view.
133
     */
134
    public function rebindViewContainer(Container $app)
135
    {
136
        $view = $app->make('view');
137
138
        $closure = function () use ($app) {
139
            $this->container = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
140
            $this->shared['app'] = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property shared does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
141
        };
142
143
        $resetView = $closure->bindTo($view, $view);
144
        $resetView();
145
    }
146
147
    /**
148
     * Rebind laravel's container in kernel.
149
     */
150
    public function rebindKernelContainer(Container $app)
151
    {
152
        if ($this->isLaravel()) {
153
            $kernel = $app->make(Kernel::class);
154
155
            $closure = function () use ($app) {
156
                $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
157
            };
158
159
            $resetKernel = $closure->bindTo($kernel, $kernel);
160
            $resetKernel();
161
        }
162
    }
163
}
164