Skip to content

Commit 5d4e6ba

Browse files
authored
added http push methods
1 parent dea4055 commit 5d4e6ba

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/WebPHPack.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
<?php
22
/**
3-
* WebPHPack v1.2.2
3+
* WebPHPack v1.2.3
44
* webpack PHP alternative
55
*
66
* @package phreak
77
* @author Simeon Lyubenov <[email protected]> www.webdevlabs.com
88
*
99
*/
1010

11-
namespace Phreak\WebPHPack;
11+
namespace Phreak;
1212

1313
class WebPHPack
1414
{
1515
private $outputHTML;
1616
public $matchString;
1717
public $outputURL;
1818
public $outputPath;
19-
public $jsPath, $cssPath;
20-
public $excludeCSS, $excludeJS;
21-
public $outputJSfilename, $outputCSSfilename;
19+
public $jsPath;
20+
public $cssPath;
21+
public $excludeCSS;
22+
public $excludeJS;
23+
public $outputJSfilename;
24+
public $outputCSSfilename;
2225
public $caching;
26+
public $httpush;
27+
private $headerlinks;
2328

2429
public function __construct($inputHTML)
2530
{
@@ -34,10 +39,14 @@ public function __construct($inputHTML)
3439
$this->excludeJS = [];
3540
$this->excludeCSS = [];
3641
$this->caching = false;
42+
$this->httpush = false;
3743
}
3844

3945
public function output()
4046
{
47+
if ($this->httpush) {
48+
$this->pushHeaders();
49+
}
4150
return $this->outputHTML;
4251
}
4352

@@ -68,6 +77,10 @@ public function combineJS ()
6877
clearstatcache();
6978
$filetime = filemtime($this->outputPath.'/'.$this->outputJSfilename);
7079
$newsrc = str_replace('</head>', '<script async src="'.$this->outputURL.'/'.$this->outputJSfilename.'?'.$filetime.'"></script></head>', $newsrc);
80+
$this->headerlinks[] = [
81+
'link'=>$this->outputURL.'/'.$this->outputJSfilename,
82+
'type'=>'script'
83+
];
7184
$this->outputHTML = $newsrc;
7285
return $this;
7386
}
@@ -99,8 +112,21 @@ public function combineCSS()
99112
clearstatcache();
100113
$filetime = filemtime($this->outputPath.'/'.$this->outputCSSfilename);
101114
$newsrc = str_replace('</head>', '<link href="'.$this->outputURL.'/'.$this->outputCSSfilename.'?'.$filetime.'" rel="stylesheet" media="none" onload="if(media!=\'all\')media=\'all\'">'.'</head>', $newsrc);
115+
$this->headerlinks[] = [
116+
'link'=>$this->outputURL.'/'.$this->outputCSSfilename,
117+
'type'=>'style'
118+
];
102119
$this->outputHTML = $newsrc;
103120
return $this;
104121
}
105122

123+
public function pushHeaders()
124+
{
125+
$pushlinks='';
126+
foreach ($this->headerlinks as $resource => $src) {
127+
$pushlinks .= '<'.$src['link'].'>; rel=preload; as='.$src['type'].', ';
128+
}
129+
header("Link: ".$pushlinks);
130+
}
131+
106132
}

0 commit comments

Comments
 (0)