Completed
Push — master ( 8b2162...780334 )
by Şəhriyar
145:12 queued 141:19
created

LocalizedRouter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 53.13%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 19
c 4
b 1
f 1
lcom 1
cbo 5
dl 0
loc 151
ccs 34
cts 64
cp 0.5313
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addRoute() 0 7 1
A __construct() 0 6 1
A resource() 0 10 3
B localizeUris() 0 27 6
B controller() 0 34 6
A addFallthroughRoute() 0 9 2
1
<?php namespace App\Services\Routing;
2
3
use Illuminate\Container\Container;
4
use Illuminate\Contracts\Events\Dispatcher;
5
use Illuminate\Routing\ControllerInspector;
6
use Illuminate\Routing\Router;
7
8
class LocalizedRouter extends Router
9
{
10
    /**
11
     * Create a new Router instance.
12
     *
13
     * @param  \Illuminate\Contracts\Events\Dispatcher $events
14
     * @param  \Illuminate\Container\Container         $container
15
     */
16 31
    public function __construct(Dispatcher $events, Container $container = null)
17
    {
18 31
        parent::__construct($events, $container);
19
20 31
        $this->routes = new LocalizedRouteCollection;
21 31
    }
22
23
    /**
24
     * Add a route to the underlying route collection.
25
     *
26
     * @param  array|string          $methods
27
     * @param  string                $uri
28
     * @param  \Closure|array|string $action
29
     *
30
     * @return \Illuminate\Routing\Route
31
     */
32 31
    protected function addRoute($methods, $uri, $action)
33
    {
34
        // Now we apply our Localization modifications.
35 31
        $uri = $this->localizeUris($uri);
36
37 31
        return parent::addRoute($methods, $uri, $action);
38
    }
39
40
    /**
41
     * Route a controller to a URI with wildcard routing.
42
     *
43
     * @param  string $uri
44
     * @param  string $controller
45
     * @param  array  $names
46
     *
47
     * @return void
48
     *
49
     * @deprecated since version 5.2.
50
     */
51
    public function controller($uri, $controller, $names = [])
52
    {
53
        $prepended = $controller;
54
55
        // First, we will check to see if a controller prefix has been registered in
56
        // the route group. If it has, we will need to prefix it before trying to
57
        // reflect into the class instance and pull out the method for routing.
58
        if (!empty($this->groupStack)) {
59
            $prepended = $this->prependGroupUses($controller);
60
        }
61
62
        $controllerInspector = new ControllerInspector;
0 ignored issues
show
Deprecated Code introduced by
The class Illuminate\Routing\ControllerInspector has been deprecated with message: since version 5.2.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
63
        $routable = $controllerInspector->getRoutable($prepended, $uri);
64
65
        // Now we apply our Localization modifications.
66
        foreach ($routable as &$routes) {
67
            foreach ($routes as &$route) {
68
                $route['plain'] = $this->localizeUris($route['plain']);
69
                unset($route);
70
            }
71
            unset($routes);
72
        }
73
74
        // When a controller is routed using this method, we use Reflection to parse
75
        // out all of the routable methods for the controller, then register each
76
        // route explicitly for the developers, so reverse routing is possible.
77
        foreach ($routable as $method => $routes) {
78
            foreach ($routes as $route) {
79
                $this->registerInspected($route, $controller, $method, $names);
0 ignored issues
show
Deprecated Code introduced by
The method Illuminate\Routing\Router::registerInspected() has been deprecated with message: since version 5.2.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
            }
81
        }
82
83
        $this->addFallthroughRoute($controller, $uri);
0 ignored issues
show
Deprecated Code introduced by
The method App\Services\Routing\Loc...::addFallthroughRoute() has been deprecated with message: since version 5.2.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
    }
85
86
    /**
87
     * Route a resource to a controller.
88
     *
89
     * @param  string $name
90
     * @param  string $controller
91
     * @param  array  $options
92
     *
93
     * @return void
94
     */
95 31
    public function resource($name, $controller, array $options = [])
96
    {
97 31
        if ($this->container && $this->container->bound(LocalizedResourceRegistrar::class)) {
98
            $registrar = $this->container->make(LocalizedResourceRegistrar::class);
99
        } else {
100 31
            $registrar = new LocalizedResourceRegistrar($this);
101
        }
102
103 31
        $registrar->register($name, $controller, $options);
104 31
    }
105
106
    /**
107
     * Add a fallthrough route for a controller.
108
     *
109
     * @param  string $controller
110
     * @param  string $uri
111
     *
112
     * @return void
113
     *
114
     * @deprecated since version 5.2.
115
     */
116
    protected function addFallthroughRoute($controller, $uri)
117
    {
118
        $localizedUri = app('translator')->get('routes.' . $uri . '.');
119
        if (false !== strpos($localizedUri, '.')) {
120
            $localizedUri = $uri;
121
        }
122
123
        parent::addFallthroughRoute($controller, $localizedUri);
0 ignored issues
show
Deprecated Code introduced by
The method Illuminate\Routing\Router::addFallthroughRoute() has been deprecated with message: since version 5.2.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
124
    }
125
126
    /**
127
     * @param string $uri
128
     *
129
     * @return string
130
     */
131 31
    private function localizeUris($uri)
132
    {
133 31
        $uriExploded = explode('/', trim($uri, '/'));
134 31
        $localizedUriTranslationBitParts = [];
135 31
        while (list($level, $bitName) = each($uriExploded)) {
136 31
            if ($level == 0) {
137 31
                $localizedUriTranslationBitParts[$level] = 'routes.' . $bitName . '.';
138 31
            } else {
139 31
                $localizedUriTranslationBitParts[$level] = trim($localizedUriTranslationBitParts[$level - 1], '.') . '.' . $bitName;
140
            }
141 31
        }
142 31
        foreach ($localizedUriTranslationBitParts as $level => &$translationBitPart) {
143 31
            $phraseToGetTranslationFor = $translationBitPart;
144 31
            if (preg_match('#(?<!routes)\.\{[^\}]+\}\.#', $translationBitPart)) { // For lower-level paths, in order not to hit 'routes.' index.
145 31
                $phraseToGetTranslationFor = preg_replace('#\{[^\}]+\}\.?#', '', $translationBitPart);
146 31
            }
147 31
            $translatedPhrase = app('translator')->get($phraseToGetTranslationFor);
148 31
            if (false !== strpos($translatedPhrase, '.')) {
149 31
                $translationBitPart = $uriExploded[$level];
150 31
            } else {
151 31
                $translationBitPart = $translatedPhrase;
152
            }
153 31
            unset($translationBitPart); // Delete the reference (won't delete the original).
154 31
        }
155
156 31
        return implode('/', $localizedUriTranslationBitParts);
157
    }
158
}
159