Passed
Push — 0.7.0 ( ba8797...2cebcc )
by Alexander
03:45 queued 11s
created

MiddlewareResolver::resolve()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 17
rs 9.2222
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Controller;
24
25
use Closure;
26
27
/**
28
 * @author Alexander Campo <[email protected]>
29
 */
30
class MiddlewareResolver
31
{
32
    /**
33
     * Resolve the middleware name to a class name(s) preserving passed parameters.
34
     * 
35
     * @param  \Closure|string  $name
36
     * @param  array  $map
37
     * @param  array  $middlewareGroups
38
     * @return \Closure|string|array
39
     */
40
    public static function resolve($name, $map, $middlewareGroups)
41
    {
42
        if ($name instanceof Closure) {
43
            return $name;
44
        }
45
        
46
        if (isset($map[$name]) && $map[$name] instanceof Closure) {
47
            return $map[$name];
48
        }
49
        
50
        if (isset($middlewareGroups[$name])) {
51
            return static::parseMiddlewareGroup($name, $map, $middlewareGroups);
52
        }
53
        
54
        [$name, $parameters] = array_pad(explode(':', $name, 2), 2, null);
55
        
56
        return ($map[$name] ?? $name).(! is_null($parameters) ? ':'.$parameters : '');
57
    }
58
    
59
    /**
60
     * Parse the middleware group and format it for usage.
61
     * 
62
     * @param  string  $name
63
     * @param  array  $map
64
     * @param  array  $middlewareGroups
65
     * 
66
     * @return array
67
     */
68
    protected static function parseMiddlewareGroup($name, $map, $middlewareGroups)
69
    {
70
        $results = [];
71
        
72
        foreach ($middlewareGroups[$name] as $middleware) {
73
            if (isset($middlewareGroups[$middleware])) {
74
                $results = array_merge($results, static::parseMiddlewareGroup(
75
                    $middleware, $map, $middlewareGroups
76
                ));
77
                
78
                continue;
79
            }
80
            
81
            [$middleware, $parameters] = array_pad(
82
                explode(':', $middleware, 2), 2, null
83
            );
84
            
85
            if (isset($map[$middleware])) {
86
                $middleware = $map[$middleware];
87
            }
88
            
89
            $results[] = $middleware.($parameters ? ':'.$parameters : '');
90
        }
91
        
92
        return $results;
93
    }
94
}