Issues (105)

src/RouterAwareTrait.php (1 issue)

1
<?php
2
3
namespace Nip\Router;
4
5
use Nip\Request;
6
7
/**
8
 * Class ConfigAwareTrait
9
 * @package Nip\Router
10
 */
11
trait RouterAwareTrait
12
{
13
    /**
14
     * @var Router|null
15
     */
16
    protected $router = null;
17
18
    /**
19
     * @param bool|Request $request
20
     * @return array
21
     */
22
    public function route($request = false)
23
    {
24
        $request = $request ? $request : $this->getRequest();
0 ignored issues
show
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

24
        $request = $request ? $request : $this->/** @scrutinizer ignore-call */ getRequest();
Loading history...
25
26
        $router = $this->getRouter();
27
        $router->setContext((new RequestContext())->fromRequest($this->getRequest()));
28
29
        $params = $router->route($request);
30
31
        return $params;
32
    }
33
34
    /**
35
     * @return Router
36
     */
37
    public function getRouter()
38
    {
39
        if (!$this->router) {
40
            $this->initRouter();
41
        }
42
43
        return $this->router;
44
    }
45
46
    /**
47
     * @param Router $router
48
     * @return $this
49
     */
50
    public function setRouter($router = false)
51
    {
52
        $this->router = $router;
53
54
        return $this;
55
    }
56
57
    protected function initRouter()
58
    {
59
        $this->setRouter($this->newRouter());
60
    }
61
62
    /**
63
     * @return Router
64
     */
65
    protected function newRouter()
66
    {
67
        return app()->get('router');
68
    }
69
}
70