Skip to content

Commit 8ca5a1e

Browse files
Add Laravel 9 support (#18)
* Add laravel 9 support * Add laravel 9 support * make response compatible with symfony * symfony throws now an SessionNotFoundException - keep the behavior * Removed PHP 7.x and added 8.1 * pin php 8.1.0 * Adds laravel 9 stubs for tests * Fixed tests * league/fractal is lacking php 8.1 support * style CI * Style CI and added L9 to travis * request->getSession() throws and exception with new symfony ; keep old behavior * During my api tests I have discovered some scenarios where response()->noContent() was breaking due to "null" values for getContent(). The proper fix seems to ensure we always have an empty "string" * Moved fractal to ^0.20 after the fork annuh/fractal was merged and tagge into master
1 parent d37560f commit 8ca5a1e

16 files changed

+231
-36
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ dist: bionic
33
language: php
44

55
php:
6-
- 7.4
76
- 8.0
7+
- 8.1.0
88

99
env:
1010
global:
1111
- setup=basic
1212
- xdebug=false
1313
jobs:
1414
- LARAVEL_VERSION=8.*
15+
- LARAVEL_VERSION=9.*
1516

1617
cache:
1718
directories:

composer.json

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,34 @@
1313
"email": "[email protected]"
1414
}],
1515
"require": {
16-
"php": "^7.2.5|^8.0",
17-
"dingo/blueprint": "^0.4",
18-
"illuminate/routing": "^7.0|^8.0",
19-
"illuminate/support": "^7.0|^8.0",
20-
"league/fractal": "^0.19"
16+
"php": "^7.2.5|^8.0|^8.1",
17+
"dingo/blueprint": "dev-patch-1",
18+
"illuminate/routing": "^7.0|^8.0|^9.0",
19+
"illuminate/support": "^7.0|^8.0|^9.0",
20+
"league/fractal": "^0.20"
2121
},
2222
"require-dev": {
23-
"friendsofphp/php-cs-fixer": "~2",
24-
"illuminate/auth": "^7.0|^8.0",
25-
"illuminate/cache": "^7.0|^8.0",
26-
"illuminate/console": "^7.0|^8.0",
27-
"illuminate/database": "^7.0|^8.0",
28-
"illuminate/events": "^7.0|^8.0",
29-
"illuminate/filesystem": "^7.0|^8.0",
30-
"illuminate/log": "^7.0|^8.0",
31-
"illuminate/pagination": "^7.0|^8.0",
32-
"laravel/lumen-framework": "^7.0|^8.0",
23+
"friendsofphp/php-cs-fixer": "~2|~3",
24+
"illuminate/auth": "^7.0|^8.0|^9.0",
25+
"illuminate/cache": "^7.0|^8.0|^9.0",
26+
"illuminate/console": "^7.0|^8.0|^9.0",
27+
"illuminate/database": "^7.0|^8.0|^9.0",
28+
"illuminate/events": "^7.0|^8.0|^9.0",
29+
"illuminate/filesystem": "^7.0|^8.0|^9.0",
30+
"illuminate/log": "^7.0|^8.0|^9.0",
31+
"illuminate/pagination": "^7.0|^8.0|^9.0",
32+
"laravel/lumen-framework": "^7.0|^8.0|^9.0",
3333
"mockery/mockery": "~1.0",
3434
"phpunit/phpunit": "^8.5|^9.0",
3535
"squizlabs/php_codesniffer": "~2.0",
3636
"php-open-source-saver/jwt-auth": "^1.4"
3737
},
38+
"repositories": [
39+
{
40+
"type": "vcs",
41+
"url": "https://github.com/dmason30/blueprint"
42+
}
43+
],
3844
"suggest": {
3945
"php-open-source-saver/jwt-auth": "Protect your API with JSON Web Tokens."
4046
},

src/Exception/InternalHttpException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(Response $response, $message = null, Exception $prev
2929
{
3030
$this->response = $response;
3131

32-
parent::__construct($response->getStatusCode(), $message, $previous, $headers, $code);
32+
parent::__construct($response->getStatusCode(), $message ?? '', $previous, $headers, $code);
3333
}
3434

3535
/**

src/Exception/ResourceException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct($message = null, $errors = null, Exception $previous
3434
$this->errors = is_array($errors) ? new MessageBag($errors) : $errors;
3535
}
3636

37-
parent::__construct(422, $message, $previous, $headers, $code);
37+
parent::__construct(422, $message ?? '', $previous, $headers, $code);
3838
}
3939

4040
/**

src/Http/Request.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Dingo\Api\Http\Parser\Accept;
66
use Illuminate\Http\Request as IlluminateRequest;
77
use Dingo\Api\Contract\Http\Request as RequestInterface;
8+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
89

910
class Request extends IlluminateRequest implements RequestInterface
1011
{
@@ -35,8 +36,11 @@ public function createFromIlluminate(IlluminateRequest $old)
3536
$old->cookies->all(), $old->files->all(), $old->server->all(), $old->content
3637
);
3738

38-
if ($session = $old->getSession()) {
39-
$new->setLaravelSession($old->getSession());
39+
try {
40+
if ($session = $old->getSession()) {
41+
$new->setLaravelSession($old->getSession());
42+
}
43+
} catch (SessionNotFoundException $exception) {
4044
}
4145

4246
$new->setRouteResolver($old->getRouteResolver());

src/Http/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static function makeFromJson(JsonResponse $json)
124124
*/
125125
public function morph($format = 'json')
126126
{
127-
$this->content = $this->getOriginalContent();
127+
$this->content = $this->getOriginalContent() ?? '';
128128

129129
$this->fireMorphingEvent();
130130

@@ -192,7 +192,7 @@ protected function fireMorphingEvent()
192192
/**
193193
* {@inheritdoc}
194194
*/
195-
public function setContent($content)
195+
public function setContent(mixed $content): static
196196
{
197197
// Attempt to set the content string, if we encounter an unexpected value
198198
// then we most likely have an object that cannot be type cast. In that

src/Http/Validation/Domain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function stripProtocol($domain)
6262
*/
6363
protected function stripPort($domain)
6464
{
65-
if ($domainStripped = preg_replace(self::PATTERN_STRIP_PROTOCOL, null, $domain)) {
65+
if ($domainStripped = preg_replace(self::PATTERN_STRIP_PROTOCOL, '', $domain)) {
6666
return $domainStripped;
6767
}
6868

src/Http/Validation/Prefix.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Prefix implements Validator
2020
* @param string $prefix
2121
* @return void
2222
*/
23-
public function __construct($prefix)
23+
public function __construct(?string $prefix = null)
2424
{
25-
$this->prefix = $prefix;
25+
$this->prefix = $prefix ?? '';
2626
}
2727

2828
/**
@@ -38,7 +38,7 @@ public function validate(Request $request)
3838

3939
$path = $this->filterAndExplode($request->getPathInfo());
4040

41-
return ! is_null($this->prefix) && $prefix == array_slice($path, 0, count($prefix));
41+
return ! empty($this->prefix) && $prefix == array_slice($path, 0, count($prefix));
4242
}
4343

4444
/**

src/Provider/LaravelServiceProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Http\Request as IlluminateRequest;
1717
use Dingo\Api\Routing\Adapter\Laravel as LaravelAdapter;
1818
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
19+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1920

2021
class LaravelServiceProvider extends DingoServiceProvider
2122
{
@@ -197,8 +198,11 @@ protected function initializeRequest(FormRequest $form, IlluminateRequest $curre
197198

198199
$form->setJson($current->json());
199200

200-
if ($session = $current->getSession()) {
201-
$form->setLaravelSession($session);
201+
try {
202+
if ($session = $current->getSession()) {
203+
$form->setLaravelSession($current->getSession());
204+
}
205+
} catch (SessionNotFoundException $exception) {
202206
}
203207

204208
$form->setUserResolver($current->getUserResolver());

src/Provider/LumenServiceProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Dingo\Api\Routing\Adapter\Lumen as LumenAdapter;
1717
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
1818
use FastRoute\DataGenerator\GroupCountBased as GcbDataGenerator;
19+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1920

2021
class LumenServiceProvider extends DingoServiceProvider
2122
{
@@ -172,8 +173,11 @@ protected function initializeRequest(FormRequest $form, IlluminateRequest $curre
172173

173174
$form->setJson($current->json());
174175

175-
if ($session = $current->getSession()) {
176-
$form->setLaravelSession($session);
176+
try {
177+
if ($session = $current->getSession()) {
178+
$form->setLaravelSession($current->getSession());
179+
}
180+
} catch (SessionNotFoundException $exception) {
177181
}
178182

179183
$form->setUserResolver($current->getUserResolver());

src/Routing/RouteCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getRoutes()
100100
*
101101
* @return \ArrayIterator
102102
*/
103-
public function getIterator()
103+
public function getIterator(): \Traversable
104104
{
105105
return new ArrayIterator($this->getRoutes());
106106
}
@@ -110,7 +110,7 @@ public function getIterator()
110110
*
111111
* @return int
112112
*/
113-
public function count()
113+
public function count(): int
114114
{
115115
return count($this->getRoutes());
116116
}

tests/ChecksLaravelVersionTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dingo\Api\Tests;
44

5+
use Dingo\Api\Tests\Stubs\Application9Stub;
56
use Dingo\Api\Tests\Stubs\ApplicationStub;
67
use Dingo\Api\Tests\Stubs\Application8Stub;
78
use Dingo\Api\Tests\Stubs\Application7Stub;
@@ -54,7 +55,9 @@ private function getApplicationStub()
5455
$version = str_replace('v', '', $version);
5556

5657
// Return the version stub for the right version
57-
if (version_compare($version, '8.0.0', '>=')) {
58+
if (version_compare($version, '9.0.0', '>=')) {
59+
return new Application9Stub;
60+
} elseif (version_compare($version, '8.0.0', '>=')) {
5861
return new Application8Stub;
5962
} elseif (version_compare($version, '7.0.0', '>=')) {
6063
return new Application7Stub;

tests/Http/Response/Format/JsonTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testMorphingArray()
7878

7979
public function testMorphingUnknownType()
8080
{
81-
$this->assertSame(1, (new Response(1))->morph()->getContent());
81+
$this->assertSame('1', (new Response(1))->morph()->getContent());
8282
}
8383

8484
public function testMorphingEloquentModelWithCamelCasing()

tests/Http/Response/Format/JsonpTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function testMorphingArray()
7575

7676
public function testMorphingUnknownType()
7777
{
78-
$this->assertSame(1, (new Response(1))->morph()->getContent());
78+
$this->assertSame('1', (new Response(1))->morph()->getContent());
7979
}
8080

8181
public function testMorphingArrayWithOneTabPrettyPrintIndent()

tests/Http/ResponseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function testNonCastableObjectsSetAsOriginalContent()
4141

4242
$response = new Response($object);
4343

44-
$this->assertNull($response->getContent());
4544
$this->assertSame($object, $response->getOriginalContent());
4645
}
4746

0 commit comments

Comments
 (0)