Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
loader.php
1<?php
2
4
10class Loader
11{
13 protected $file;
14
15 protected $cacheTtl = 60;
16
17 protected $skipMoving = false;
18
19 protected $tagAttributes = [];
20
26 public function __construct(Output\File $file)
27 {
28 $this->file = $file;
29 }
30
37 public function setCacheTtl($cacheTtl)
38 {
39 $this->cacheTtl = (int) $cacheTtl;
40 return $this;
41 }
42
49 public function setSkipMoving($skip)
50 {
51 $this->skipMoving = (bool) $skip;
52 return $this;
53 }
54
61 public function setTagAttributes(array $tagAttributes = [])
62 {
63 $this->tagAttributes = $tagAttributes;
64 return $this;
65 }
66
72 public function getFileUrl()
73 {
74 return $this->file->getUri();
75 }
76
82 public function getString()
83 {
84 $content = $this->getStringJs();
85 $attributes = $this->tagAttributes;
86 if ($this->skipMoving)
87 {
88 $attributes['data-skip-moving'] = 'true';
89 }
90 $attrs = '';
91 foreach ($attributes as $key => $value)
92 {
93 $attrs .= " " . htmlspecialcharsbx($key) . '="'
94 . htmlspecialcharsbx($value) . '"';
95 }
96 return <<<EOD
97<script{$attrs}>
98$content
99</script>
100EOD;
101
102 }
103
109 public function getStringJs()
110 {
111 $path = $this->file->getUri();
112 if (!$path)
113 {
114 return '';
115 }
116
117 $ttl = ($this->cacheTtl ?: 1) * 1000;
118
119 return
120<<<EOD
121 (function(w,d,u){
122 var s=d.createElement('script');s.async=true;s.src=u+'?'+(Date.now()/$ttl|0);
123 var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);
124 })(window,document,'$path');
125EOD;
126
127 }
128}
__construct(Output\File $file)
Definition loader.php:26
setTagAttributes(array $tagAttributes=[])
Definition loader.php:61