bytic /
router
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Router\ServiceProvider\Traits; |
||
| 4 | |||
| 5 | use Nip\Http\Request; |
||
| 6 | use Nip\Router\RequestContext; |
||
| 7 | use Nip\Router\Router; |
||
| 8 | use Symfony\Component\Routing\RouterInterface; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Trait RouterTrait |
||
| 12 | * @package Nip\Router\ServiceProvider\Traits |
||
| 13 | */ |
||
| 14 | trait RouterTrait |
||
| 15 | { |
||
| 16 | 2 | public function registerRouter() |
|
| 17 | { |
||
| 18 | $this->getContainer()->share('router', function () { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 19 | 2 | return $this->newRouter(); |
|
| 20 | 2 | }); |
|
| 21 | |||
| 22 | 2 | $this->getContainer()->add(RouterInterface::class, Router::class); |
|
| 23 | |||
| 24 | $this->getContainer()->share(Router::class, function () { |
||
| 25 | return $this->getContainer()->get('router'); |
||
| 26 | 2 | }); |
|
| 27 | 2 | } |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @return Router |
||
| 31 | */ |
||
| 32 | 2 | public function newRouter() |
|
| 33 | { |
||
| 34 | 2 | $app = $this->getContainer()->get('app'); |
|
| 35 | |||
| 36 | /** @var Router $router */ |
||
| 37 | 2 | $router = $this->getContainer()->make( |
|
| 38 | 2 | RouterInterface::class, |
|
| 39 | [ |
||
| 40 | 2 | 'loader' => $this->getContainer()->get('routing.loader'), |
|
| 41 | 2 | 'resource' => 'routes.php', |
|
| 42 | 'options' => [ |
||
| 43 | 2 | 'cache_dir' => $app->getCachedRoutesPath(), |
|
| 44 | ] |
||
| 45 | ] |
||
| 46 | ); |
||
| 47 | |||
| 48 | 2 | $request = Request::instance(); |
|
| 49 | 2 | if ($request instanceof Request) { |
|
|
0 ignored issues
–
show
|
|||
| 50 | 2 | $router->setContext((new RequestContext())->fromRequest($request)); |
|
| 51 | } |
||
| 52 | 2 | return $router; |
|
| 53 | } |
||
| 54 | } |
||
| 55 |