-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-dependencies.php
More file actions
60 lines (50 loc) · 1.24 KB
/
js-dependencies.php
File metadata and controls
60 lines (50 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Extends the Dependencies class and adds functionality for javascript files
*/
class JSDependencies extends Dependencies {
/**
* Add javascript source
*/
public function add($name, $url, $dependencies = array()) {
// If dependencies is a string, split into array
if(gettype($dependencies) == 'string') {
$dependencies = preg_split('/,\s?/', $dependencies);
}
// Add
$this -> sources[$name] = (object) array(
'name' => $name,
'url' => $url,
'dependencies' => (array) $dependencies
);
}
/**
* Create script element
*/
public function script_tag($attributes, $body = '') {
$tag = '<script %s>%s</script>';
$attributeStrings = array();
foreach($attributes as $key => $val) {
if(gettype($val) == 'array') {
$val = implode(',', $val);
}
$attributeStrings[] = sprintf('%s="%s"', $key, $val);
}
return sprintf($tag, implode(' ', $attributeStrings), $body);
}
/**
* Print dependencies
*/
public function print_dependencies() {
$sorted = $this -> sort();
foreach($sorted as $source) {
echo $this -> script_tag(array(
'id' => $source -> name,
'src' => $source -> url,
'data-dependencies' => $source -> dependencies
));
echo PHP_EOL;
}
}
}
?>