Completed
Pull Request — master (#43)
by Owen
02:34
created

Enqueue::getScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Backpack\Base\Traits;
4
5
trait Enqueue
6
{
7
    public static $enqueuedScripts = [];
8
    public static $enqueuedStyles = [];
9
10
    public static function enqueueAsset($type, $id, $source, $dependencies = [])
11
    {
12
        switch ($type) {
13
            case 'css':
14
            case 'style':
15
                return self::enqueueStyle($id, $source, $dependencies);
16
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
17
            case 'script':
18
            case 'javascript':
19
            case 'js':
20
                return self::enqueueScript($id, $source, $dependencies);
21
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
22
        }
23
    }
24
25 View Code Duplication
    public static function enqueueScript($id, $source, $dependencies)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        if (!str_contains($source, '//')) {
28
            $path = '/'.ltrim($source, '/');
29
            $v = filemtime(realpath(ltrim($path, '/')));
30
            $sourceComputed = asset($path.'?v='.$v);
31
        } else {
32
            $sourceComputed = $source;
33
        }
34
35
        $sourceObj = (object) [
36
            'id'     => $id,
37
            'source' => $sourceComputed,
38
            'deps'   => $dependencies,
39
        ];
40
41
        self::$enqueuedScripts[$id] = $sourceObj;
42
43
        return $sourceObj;
44
    }
45
46 View Code Duplication
    public static function enqueueStyle($id, $source, $dependencies)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        if (!str_contains($source, '//')) {
49
            $path = '/'.ltrim($source, '/');
50
            $v = filemtime(realpath(ltrim($path, '/')));
51
            $sourceComputed = asset($path.'?v='.$v);
52
        } else {
53
            $sourceComputed = $source;
54
        }
55
56
        $sourceObj = (object) [
57
            'id'     => $id,
58
            'source' => $sourceComputed,
59
            'deps'   => $dependencies,
60
        ];
61
62
        self::$enqueuedStyles[$id] = $sourceObj;
63
64
        return $sourceObj;
65
    }
66
67
    private static function handleDependencies($items)
68
    {
69
        $items = array_unique($items, SORT_REGULAR); //Strip out duplicate requests
70
        $resolutions = [];
71
        $dependenciesFound = [];
72
        $circularDependencyCounter = 0;
73
74
        while (count($items) > count($resolutions) && $circularDependencyCounter < 20) {
75
            $circularDependencyCounter++;
76
77
            foreach ($items as $itemIndex => $item) {
78
79
                //If I'm an existing depdendency - skip me!
80
                if (isset($dependenciesFound[$item->id])) {
81
                    continue;
82
                }
83
84
                //We assume its found, unless stated below
85
                $resolved = true;
86
87
                //Check through each depdendency to see if its alrady desolved
88
                if (isset($item->deps) && !empty($item->deps)) {
89
                    foreach ($item->deps as $dep) {
90
                        if (!isset($dependenciesFound[$dep])) {
91
                            $resolved = false;
92
                            break;
93
                        }
94
                    }
95
                }
96
97
                if ($resolved) {
98
                    $dependenciesFound[$item->id] = true;
99
                    $resolutions[] = $item;
100
                }
101
            }
102
        }
103
104
        return $resolutions;
105
    }
106
107
    public static function getScripts()
108
    {
109
        return self::handleDependencies(self::$enqueuedScripts);
110
    }
111
112
    public static function getStyles()
113
    {
114
        return self::handleDependencies(self::$enqueuedStyles);
115
    }
116
}
117