RoutingServiceProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 19
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerUrlGenerator() 0 27 1
A registerRedirector() 0 15 2
1
<?php
2
3
namespace Endeavors\Components\Routing;
4
5
use Illuminate\Routing\RoutingServiceProvider as OriginalRoutingServiceProvider;
6
use Illuminate\Routing\UrlGenerator as OriginalUrlGenerator;
7
use Illuminate\Routing\Redirector;
8
use Illuminate\Support\Facades\URL;
9
use Illuminate\Http\Request;
10
11
class RoutingServiceProvider extends OriginalRoutingServiceProvider
12
{
13
    /**
14
     * Register the URL generator service.
15
     *
16
     * @return void
17
     */
18
    protected function registerUrlGenerator()
19
    {
20
        $this->app['url'] = $this->app->share(function($app) {
21
            $routes = $app['router']->getRoutes();
22
            // The URL generator needs the route collection that exists on the router.
23
            // Keep in mind this is an object, so we're passing by references here
24
            // and all the registered routes will be available to the generator.
25
            $app->instance('routes', $routes);
26
            $url = new UrlGenerator(new OriginalUrlGenerator(
27
                $routes, $app->rebinding(
28
                    'request', $this->requestRebinder()
29
                )
30
            ));
31
            $url->setSessionResolver(function() {
32
                return $this->app['session'];
33
            });
34
35
            $url->setKeyResolver(function() {
36
                return $this->app->make('config')->get('app.key');
37
            });
38
            // If the route collection is "rebound", for example, when the routes stay
39
            // cached for the application, we will need to rebind the routes on the
40
            // URL generator instance so it has the latest version of the routes.
41
            $app->rebinding('routes', function($app, $routes) {
42
                $app['url']->setRoutes($routes);
43
            });
44
            return $url;
45
        });        
46
    }
47
    
48
    /**
49
     * Register the Redirector service.
50
     *
51
     * @return void
52
     */
53
    protected function registerRedirector()
54
    {
55
        $this->app['redirect'] = $this->app->share(function($app)
56
        {
57
            $redirector = new Redirector($app['url']->getOriginalUrlGenerator());
58
59
            // If the session is set on the application instance, we'll inject it into
60
            // the redirector instance. This allows the redirect responses to allow
61
            // for the quite convenient "with" methods that flash to the session.
62
            if (isset($app['session.store']))
63
            {
64
                $redirector->setSession($app['session.store']);
65
            }
66
67
            return $redirector;
68
        });
69
    }
70
}
71