Skip to content

Commit

Permalink
Add laravel 6 support (#36)
Browse files Browse the repository at this point in the history
* add laravel 6 version constraint to composer.json

* str_* functions deprecated, use Str facade instead
  • Loading branch information
matijakovacevic authored and JacobBennett committed Nov 22, 2019
1 parent cbab330 commit 80703b4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 6 additions & 5 deletions src/Middleware/AddHttp2ServerPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function getConfig($key, $default=false) {
}
return config('http2serverpush.'.$key, $default);
}

/**
* @param \Illuminate\Http\Response $response
*
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions tests/AddHttp2ServerPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,7 +15,7 @@ public function setUp()
$this->middleware = new AddHttp2ServerPush();
}


/** @test */
public function it_will_not_exceed_size_limit()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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')));
}

Expand Down

0 comments on commit 80703b4

Please sign in to comment.