RouteModuleMacroServiceProvider::registerMacro()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace TheJawker\RouteModuleMacro;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\ServiceProvider;
8
9
class RouteModuleMacroServiceProvider extends ServiceProvider
10
{
11
    public function register()
12
    {
13
        if (!Router::hasMacro('module')) {
14
            $this->registerMacro();
15
        }
16
    }
17
18
    private function registerMacro()
19
    {
20
        Router::macro('module', function ($module, $only = [], $options = []) {
21
            $onlyOptions = count($only) ? ['only' => $only] : [];
22
23
            $controllerNameArray = collect(explode('.', $module))->map(function ($name) {
24
                return Str::studly($name);
25
            });
26
27
            $lastName = $controllerNameArray->pop();
28
29
            $controllerName = $controllerNameArray->map(function ($name) {
30
                return Str::singular($name);
31
            })->push($lastName)->push('Controller')->implode('');
32
33
            Router::resource($module, $controllerName, array_merge($onlyOptions, $options));
0 ignored issues
show
Bug Best Practice introduced by
The method Illuminate\Routing\Router::resource() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
            Router::/** @scrutinizer ignore-call */ 
34
                    resource($module, $controllerName, array_merge($onlyOptions, $options));
Loading history...
34
        });
35
    }
36
}
37