-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.jslink.php
89 lines (77 loc) · 2.61 KB
/
class.jslink.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
Example usage:
$js = new JSLink($dir,$relrootdir,$indicator);
Where
- $dir is javascript directory relative to executing php script
- $relrootdir is javascript directory relative to webroot (will be used for actual src in link)
- $indicator is in javascript file as comment leading the javascript filename to be included
default is: php-include
EX javascript:
//php-include somefile.js
$js->request($files);
Where
- $files is an array of javascript filenames to be linked
EX: array('accesskeys.js')
echo $js->jslink();
This line actually prints the <script> tags
*/
class JSLink {
protected $dir;
protected $relrootdir;
protected $html;
protected $requests;
protected $indicator;
protected $dependencies;
function __construct(){
$this->html = null;
$this->requests = array();
$this->dependencies = array();
}
function set_dir($d = null){
$this->dir = $d;
}
function set_relrootdir($rrd = null){
$this->relrootdir = $rrd;
}
function set_indicator($i = 'php-include'){
$this->indicator = $i;
}
function build($js){
$depends = array();
$y = array_push($this->requests,$js);
if(strpos($js,'http://') !== 0 and strpos($js,'https://') !== 0 and $lines = file($this->dir.$js)){
foreach($lines as $line){
if(stripos($line,$this->indicator) !== false){
$filename = trim(substr($line,stripos($line,$this->indicator)+strlen($this->indicator)));
if(!in_array($filename,$this->requests)){
$depends = array_merge($depends,$this->build($filename));
$y = array_push($depends,$filename);
$depends = array_unique($depends);
}
}
}
}
$y = array_push($depends,$js);
$depends = array_unique($depends);
return $depends;
}
function request($req = array()){
if(is_array($req)){
foreach($req as $js){
$this->dependencies = array_unique(array_merge($this->dependencies, $this->build($js)));
}
}
else $this->dependencies = array_unique(array_merge($this->dependencies, $this->build($req)));
}
function deliver(){
$x = null;
foreach($this->dependencies as $d){
if(strpos($d,'http://') !== 0 and strpos($d,'https://') !== 0)
$d = $this->relrootdir . $d;
$x .= '<script type="text/javascript" src="'.$d.'"></script>'."\n\t";
}
return $x;
}
}
?>