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; |
|
|
|
|
17
|
|
|
case 'script': |
18
|
|
|
case 'javascript': |
19
|
|
|
case 'js': |
20
|
|
|
return self::enqueueScript($id, $source, $dependencies); |
21
|
|
|
break; |
|
|
|
|
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public static function enqueueScript($id, $source, $dependencies) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
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.