1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Features\Components; |
4
|
|
|
|
5
|
|
|
use Flynt\Utils\Asset; |
6
|
|
|
use Flynt\Utils\StringHelpers; |
7
|
|
|
|
8
|
|
|
class Component |
9
|
|
|
{ |
10
|
|
|
public static function enqueueAssets($componentName, array $dependencies = []) |
11
|
|
|
{ |
12
|
|
|
// register dependencies |
13
|
|
|
foreach ($dependencies as $dependency) { |
14
|
|
|
// TODO add a warning if the same script is loaded several times (with different names) in multiple components |
15
|
|
|
Asset::register($dependency); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// collect script dependencies |
19
|
|
|
$scriptDeps = array_reduce($dependencies, function ($list, $dependency) { |
20
|
|
|
if ($dependency['type'] === 'script') { |
21
|
|
|
array_push($list, $dependency['name']); |
22
|
|
|
} |
23
|
|
|
return $list; |
24
|
|
|
}, ['jquery']); // jquery as a default dependency |
25
|
|
|
|
26
|
|
|
// Enqueue Component Scripts if they exist |
27
|
|
|
$scriptAbsPath = Asset::requirePath("Components/{$componentName}/script.js"); |
28
|
|
View Code Duplication |
if (is_file($scriptAbsPath)) { |
|
|
|
|
29
|
|
|
Asset::enqueue([ |
30
|
|
|
'type' => 'script', |
31
|
|
|
'name' => "Flynt/Components/{$componentName}", |
32
|
|
|
'path' => "Components/{$componentName}/script.js", |
33
|
|
|
'dependencies' => $scriptDeps |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// collect style dependencies |
38
|
|
|
$styleDeps = array_reduce($dependencies, function ($list, $dependency) { |
39
|
|
|
if ($dependency['type'] === 'style') { |
40
|
|
|
array_push($list, $dependency['name']); |
41
|
|
|
} |
42
|
|
|
return $list; |
43
|
|
|
}, []); |
44
|
|
|
|
45
|
|
|
// Enqueue Component Styles if they exist |
46
|
|
|
$styleAbsPath = Asset::requirePath("Components/{$componentName}/style.css"); |
47
|
|
View Code Duplication |
if (is_file($styleAbsPath)) { |
|
|
|
|
48
|
|
|
Asset::enqueue([ |
49
|
|
|
'type' => 'style', |
50
|
|
|
'name' => "Flynt/Components/{$componentName}", |
51
|
|
|
'path' => "Components/{$componentName}/style.css", |
52
|
|
|
'dependencies' => $styleDeps |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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.