From 80703b471e3616035330825b1250aff3bd85f5f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Kova=C4=8Devi=C4=87?= Date: Fri, 22 Nov 2019 19:52:06 +0100 Subject: [PATCH] Add laravel 6 support (#36) * add laravel 6 version constraint to composer.json * str_* functions deprecated, use Str facade instead --- composer.json | 4 ++-- src/Middleware/AddHttp2ServerPush.php | 11 ++++++----- tests/AddHttp2ServerPushTest.php | 13 +++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 014ee49..0ece63e 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ ], "require": { "php" : "^7.0", - "illuminate/support": "^5.3", - "illuminate/http": "^5.3", + "illuminate/support": "~5.3|~6.0", + "illuminate/http": "~5.3|~6.0", "symfony/dom-crawler": "^2.7|^3.0|^4.0", "symfony/css-selector": "^2.7|^3.0|^4.0" }, diff --git a/src/Middleware/AddHttp2ServerPush.php b/src/Middleware/AddHttp2ServerPush.php index 9ec531e..22baee2 100644 --- a/src/Middleware/AddHttp2ServerPush.php +++ b/src/Middleware/AddHttp2ServerPush.php @@ -3,6 +3,7 @@ namespace JacobBennett\Http2ServerPush\Middleware; use Closure; +use Illuminate\Support\Str; use Illuminate\Http\Request; use Illuminate\Http\Response; use Symfony\Component\DomCrawler\Crawler; @@ -43,7 +44,7 @@ public function getConfig($key, $default=false) { } return config('http2serverpush.'.$key, $default); } - + /** * @param \Illuminate\Http\Response $response * @@ -69,7 +70,7 @@ protected function generateAndAttachLinkHeaders(Response $response, $limit = nul return !preg_match('%('.$exclude_keywords->implode('|').')%i', $value); }) ->take($limit); - + $sizeLimit = $sizeLimit ?? max(1, intval($this->getConfig('size_limit', 32*1024))); $headersText = trim($headers->implode(',')); while(strlen($headersText) > $sizeLimit) { @@ -136,10 +137,10 @@ private function buildLinkHeaderString($url) ]; $type = collect($linkTypeMap)->first(function ($type, $extension) use ($url) { - return str_contains(strtoupper($url), $extension); + return Str::contains(strtoupper($url), $extension); }); - - + + if(!preg_match('%^https?://%i', $url)) { $basePath = $this->getConfig('base_path', '/'); $url = $basePath . ltrim($url, $basePath); diff --git a/tests/AddHttp2ServerPushTest.php b/tests/AddHttp2ServerPushTest.php index 2b416af..1c78907 100644 --- a/tests/AddHttp2ServerPushTest.php +++ b/tests/AddHttp2ServerPushTest.php @@ -3,6 +3,7 @@ namespace JacobBennett\Http2ServerPush\Test; use Illuminate\Http\Request; +use Illuminate\Support\Str; use JacobBennett\Http2ServerPush\Middleware\AddHttp2ServerPush; use Symfony\Component\HttpFoundation\Response; @@ -14,7 +15,7 @@ public function setUp() $this->middleware = new AddHttp2ServerPush(); } - + /** @test */ public function it_will_not_exceed_size_limit() { @@ -27,7 +28,7 @@ public function it_will_not_exceed_size_limit() $this->assertTrue(strlen($response->headers->get('link')) <= $limit ); $this->assertCount(1, explode(",", $response->headers->get('link'))); } - + /** @test */ public function it_will_not_add_excluded_asset() { @@ -36,10 +37,10 @@ public function it_will_not_add_excluded_asset() $response = $this->middleware->handle($request, $this->getNext('pageWithCssAndJs'), null, null, ['thing']); $this->assertTrue($this->isServerPushResponse($response)); - $this->assertTrue(!str_contains($response->headers, 'thing')); + $this->assertTrue(!Str::contains($response->headers, 'thing')); $this->assertCount(1, explode(",", $response->headers->get('link'))); } - + /** @test */ public function it_will_not_modify_a_response_with_no_server_push_assets() { @@ -114,8 +115,8 @@ public function it_will_return_correct_push_headers_for_multiple_assets() $response = $this->middleware->handle($request, $this->getNext('pageWithCssAndJs')); $this->assertTrue($this->isServerPushResponse($response)); - $this->assertTrue(str_contains($response->headers, 'style')); - $this->assertTrue(str_contains($response->headers, 'script')); + $this->assertTrue(Str::contains($response->headers, 'style')); + $this->assertTrue(Str::contains($response->headers, 'script')); $this->assertCount(2, explode(",", $response->headers->get('link'))); }