├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Pusher.php ├── PusherException.php └── PusherInterface.php └── tests └── PusherTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mohamed Elbahja 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Http2Pusher 2 | PHP Http2 Server Pusher 3 | 4 | ## About Http2 Server Push : 5 | 6 | > HTTP/2 Push allows a web server to send resources to a web browser before the browser gets to request them. It is, for the most part, a performance technique that can help some websites load faster. - [wikipedia](https://en.wikipedia.org/wiki/HTTP/2_Server_Push) 7 | 8 | ![http2](https://blog.cloudflare.com/content/images/2015/12/http-2-multiplexing.png) 9 | image by cloudflare 10 | 11 | ## Installation : 12 | 13 | using composer: ```composer require melbahja/http2-pusher``` 14 | 15 | ## Usage : 16 | 17 | get the instance: 18 | ```php 19 | 20 | require 'vendore/autoload.php'; 21 | 22 | use Melbahja\Http2\Pusher; 23 | 24 | $pusher = Pusher::getInstance(); 25 | 26 | ``` 27 | 28 | examples: 29 | 30 | ```php 31 | 32 | // set css file 33 | $pusher->link('/assets/css/style.css'); 34 | 35 | // set css and image and src 36 | $pusher->link('/asstes/css/main.css') 37 | ->src('/assets/js/scripts.js') 38 | ->img('/assets/img/logo.png') 39 | -set(Pusher::IMG, '/assets/img/logo2.png'); 40 | 41 | 42 | // set link with options 43 | $pusher->link('https://fonts.gstatic.com', [ 44 | 'as' => false, 45 | 'rel' => 'preconnect' 46 | ]); 47 | 48 | // rel by default is preload 49 | // as by default is the link type 50 | 51 | 52 | // push header 53 | $pusher->push(); 54 | 55 | ``` 56 | 57 | ## Public methods : 58 | 59 | ``` 60 | Pusher::getInstance(): PusherInterface 61 | 62 | Pusher::link(string $link, array $opts = []): PusherInterface 63 | 64 | Pusher::src(string $link, array $opts = []): PusherInterface 65 | 66 | Pusher::img(string $link, array $opts = []): PusherInterface 67 | 68 | Pusher::set(string $type, string $link, array $opts = []): PusherInterface 69 | 70 | Pusher::getHeader(string $type = null): string 71 | 72 | Pusher::push(string $type = null): void 73 | 74 | Pusher::public function toHeader(string $type, array $urls): null|string 75 | ``` 76 | 77 | ## License : 78 | 79 | [MIT](https://github.com/melbahja/Http2Pusher/blob/master/LICENSE) Copyright (c) 2017 Mohamed Elbahja 80 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "melbahja/http2-pusher", 3 | "type": "library", 4 | "description": "PHP Http2 Server Pusher", 5 | "keywords": ["php71","php7","http","http2","pusher"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Mohamed ELbahja", 10 | "email": "mohamed@elbahja.me", 11 | "homepage": "http://elbahja.me", 12 | "role": "Founder & Developer" 13 | } 14 | ], 15 | 16 | "minimum-stability":"dev", 17 | 18 | "autoload": { 19 | "psr-4": { 20 | "Melbahja\\Http2\\": "src/" 21 | } 22 | }, 23 | 24 | "support": { 25 | "issues": "https://github.com/melbahja/Http2Pusher/issues" 26 | }, 27 | 28 | "require": { 29 | "php": ">=7.1" 30 | }, 31 | 32 | "require-dev": { 33 | "phpunit/phpunit": "~6.3" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Pusher.php: -------------------------------------------------------------------------------- 1 | 'script', 27 | Pusher::IMG => 'image', 28 | Pusher::LINK => 'style' 29 | ]; 30 | 31 | /** 32 | * get instance 33 | * @return PusherInterface 34 | */ 35 | public static function getInstance(): PusherInterface 36 | { 37 | if (static::$instance === null) { 38 | static::$instance = new static; 39 | } 40 | 41 | return static::$instance; 42 | } 43 | 44 | /** 45 | * Set Style 46 | * @param string $link 47 | * @param array $opts 48 | */ 49 | public function link(string $link, array $opts = []): PusherInterface 50 | { 51 | return $this->set(static::LINK, $link, $opts); 52 | } 53 | 54 | /** 55 | * set Script 56 | * @param string $src 57 | * @param array $opts 58 | */ 59 | public function src(string $src, array $opts = []): PusherInterface 60 | { 61 | return $this->set(static::SRC, $src, $opts); 62 | } 63 | 64 | /** 65 | * Set Image 66 | * @param string $img 67 | * @param array $opts 68 | */ 69 | public function img(string $img, array $opts = []): PusherInterface 70 | { 71 | return $this->set(static::IMG, $img, $opts); 72 | } 73 | 74 | /** 75 | * Set Link 76 | * @param string $type 77 | * @param string $link 78 | * @param array $opts 79 | */ 80 | public function set(string $type, string $link, array $opts = []): PusherInterface 81 | { 82 | $this->links[$type][$link] = $opts; 83 | return $this; 84 | } 85 | 86 | /** 87 | * get links 88 | * @param string $type 89 | * @return string 90 | */ 91 | public function getHeader(string $type = null): string 92 | { 93 | $line = []; 94 | 95 | if ($type === null && (bool) $this->links) { 96 | 97 | foreach ($this->links as $type => $urls) 98 | { 99 | $line[] = $this->toHeader($type, $urls); 100 | } 101 | 102 | } elseif (isset($this->links[$type])) { 103 | 104 | $line[] = $this->toHeader($type, $this->links[$type]); 105 | 106 | } else { 107 | 108 | throw new PusherException("header type is not valid"); 109 | } 110 | 111 | return implode(', ', $line); 112 | } 113 | 114 | /** 115 | * Push Headers 116 | * @param string $type 117 | * @return void 118 | */ 119 | public function push(string $type = null): void 120 | { 121 | if (headers_sent($f, $l)) { 122 | throw new PusherException("headers already sent at file: {$f}, line: {$l}"); 123 | } 124 | 125 | header("Link: " . $this->getHeader()); 126 | } 127 | 128 | /** 129 | * urls to header string 130 | * @param string $type 131 | * @param array $urls 132 | * @return string|null 133 | */ 134 | public function toHeader(string $type, array $urls): ?string 135 | { 136 | if ((bool) $urls === false) return null; 137 | 138 | $line = []; 139 | $opts = [ 140 | 'rel' => 'preload', 141 | 'as' => $this->as[$type] ?? false 142 | ]; 143 | 144 | foreach ($urls as $url => $ops) 145 | { 146 | $ops = array_merge($opts, $ops); 147 | $ops = $this->arrayOptionsToStr($ops); 148 | $line[] = "<{$url}>; {$ops}"; 149 | } 150 | 151 | return implode(', ', $line); 152 | } 153 | 154 | /** 155 | * convert options to string 156 | * @param array $options 157 | * @return string 158 | */ 159 | protected function arrayOptionsToStr(array $options): string 160 | { 161 | $opts = []; 162 | 163 | foreach ($options as $k => $v) 164 | { 165 | if ($v === false) continue; 166 | 167 | $opts[] = "{$k}={$v}"; 168 | } 169 | 170 | return implode('; ', $opts); 171 | } 172 | 173 | } 174 | -------------------------------------------------------------------------------- /src/PusherException.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(Pusher::class, $pusher); 21 | return $pusher; 22 | } 23 | 24 | /** 25 | * @depends testGetInstance 26 | */ 27 | public function testSetLink($pusher) 28 | { 29 | $this->assertInstanceOf(Pusher::class, $pusher->link('test.css')); 30 | return $pusher; 31 | } 32 | 33 | /** 34 | * @depends testSetLink 35 | */ 36 | public function testSetSrc($pusher) 37 | { 38 | $this->assertInstanceOf(Pusher::class, $pusher->link('test.js')); 39 | return $pusher; 40 | } 41 | 42 | /** 43 | * @depends testSetSrc 44 | */ 45 | public function testSetImg($pusher) 46 | { 47 | $this->assertInstanceOf(Pusher::class, $pusher->link('test.png')); 48 | return $pusher; 49 | } 50 | 51 | /** 52 | * @depends testSetImg 53 | */ 54 | public function testSetLinkWithOptions($pusher) 55 | { 56 | $this->assertInstanceOf(Pusher::class, $pusher->link('https://fonts.example.com', [ 57 | 'as' => false, 58 | 'rel' => 'preconnect' 59 | ])); 60 | 61 | return $pusher; 62 | } 63 | 64 | /** 65 | * @depends testSetLinkWithOptions 66 | */ 67 | public function testHeader($pusher) 68 | { 69 | $line = $pusher->getHeader(); 70 | $this->assertSame(gettype($line), 'string'); 71 | $this->assertContains('test.css', $line); 72 | $this->assertContains('test.js', $line); 73 | $this->assertContains('test.png', $line); 74 | $this->assertContains('https://fonts.example.com', $line); 75 | $this->assertContains('rel=preconnect', $line); 76 | return $pusher; 77 | } 78 | 79 | /** 80 | * @depends testHeader 81 | */ 82 | public function testPusherException($pusher) 83 | { 84 | $this->expectException(PusherException::class); 85 | $pusher->getHeader('invalid'); 86 | } 87 | } --------------------------------------------------------------------------------