Completed
Push — master ( d07f68...5f9bab )
by
unknown
24s
created

Utils::replaceUrlParameterBindings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Routing\Route;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\Adapter\Local;
9
10
class Utils
11
{
12
    public static function getFullUrl(Route $route, array $bindings = []): string
13
    {
14
        $uri = $route->uri();
15
16
        return self::replaceUrlParameterBindings($uri, $bindings);
17
    }
18
19
    /**
20
     * @param array $action
21
     *
22
     * @return array|null
23
     */
24
    public static function getRouteActionUses(array $action)
25
    {
26
        if ($action['uses'] !== null) {
27
            if (is_array($action['uses'])) {
28
                return $action['uses'];
29
            } elseif (is_string($action['uses'])) {
30
                return explode('@', $action['uses']);
31
            }
32
        }
33
        if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
34
            return [
35
                0 => $action[0],
36
                1 => $action[1],
37
            ];
38
        }
39
    }
40
41
    /**
42
     * Transform parameters in URLs into real values (/users/{user} -> /users/2).
43
     * Uses bindings specified by caller, otherwise just uses '1'.
44
     *
45
     * @param string $uri
46
     * @param array $bindings
47
     *
48
     * @return mixed
49
     */
50
    public static function replaceUrlParameterBindings(string $uri, array $bindings)
51
    {
52
        foreach ($bindings as $path => $binding) {
53
            // So we can support partial bindings like
54
            // 'bindings' => [
55
            //  'foo/{type}' => 4,
56
            //  'bar/{type}' => 2
57
            //],
58
            if (Str::is("*$path*", $uri)) {
59
                preg_match('/({.+?})/', $path, $parameter);
60
                $uri = str_replace("{$parameter['1']}", $binding, $uri);
61
            }
62
        }
63
        // Replace any unbound parameters with '1'
64
        $uri = preg_replace('/{(.+?)}/', 1, $uri);
65
66
        return $uri;
67
    }
68
69
    public function dumpException(\Exception $e)
70
    {
71
        if (class_exists(\NunoMaduro\Collision\Handler::class)) {
72
            $handler = new \NunoMaduro\Collision\Handler;
73
            $handler->setInspector(new \Whoops\Exception\Inspector($e));
74
            $handler->setException($e);
75
            $handler->handle();
76
        } else {
77
            dump($e);
78
            echo "You can get better exception output by installing the library \nunomaduro/collision (PHP 7.1+ only).\n";
79
        }
80
    }
81
82
    public static function deleteDirectoryAndContents($dir)
83
    {
84
        $adapter = new Local(realpath(__DIR__.'/../../'));
85
        $fs = new Filesystem($adapter);
86
        $fs->deleteDir($dir);
87
    }
88
}
89