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

RebindRouterContainer::handle()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 3
nop 2
dl 0
loc 33
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Server\Resetters;
4
5
use SwooleTW\Http\Server\Sandbox;
6
use Illuminate\Contracts\Container\Container;
7
use SwooleTW\Http\Server\Resetters\ResetterContract;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
class RebindRouterContainer implements ResetterContract
11
{
12
    /**
13
     * "handle" function for resetting app.
14
     *
15
     * @param \Illuminate\Contracts\Container\Container $app
16
     * @param \SwooleTW\Http\Server\Sandbox $sandbox
17
     */
18
    public function handle(Container $app, Sandbox $sandbox)
19
    {
20
        if ($sandbox->isLaravel()) {
21
            $router = $app->make('router');
22
            $request = $sandbox->getRequest();
23
            $closure = function () use ($app, $request) {
24
                $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...
25
                if (is_null($request)) {
26
                    return;
27
                }
28
                try {
29
                    $route = $this->routes->match($request);
0 ignored issues
show
Bug Best Practice introduced by
The property routes does not exist on SwooleTW\Http\Server\Res...s\RebindRouterContainer. Did you maybe forget to declare it?
Loading history...
30
                    // clear resolved controller
31
                    if (property_exists($route, 'container')) {
32
                        $route->controller = null;
33
                    }
34
                    // rebind matched route's container
35
                    $route->setContainer($app);
36
                } catch (NotFoundHttpException $e) {
37
                    // do nothing
38
                }
39
            };
40
41
            $resetRouter = $closure->bindTo($router, $router);
42
            $resetRouter();
43
        } else {
44
            // lumen router only exists after lumen 5.5
45
            if (property_exists($app, 'router')) {
46
                $app->router->app = $app;
47
            }
48
        }
49
50
        return $app;
51
    }
52
}
53