Skip to content

Commit

Permalink
Add Laravel 9 support (#18)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
christoph-kluge authored Aug 20, 2022
1 parent d37560f commit 8ca5a1e
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ dist: bionic
language: php

php:
- 7.4
- 8.0
- 8.1.0

env:
global:
- setup=basic
- xdebug=false
jobs:
- LARAVEL_VERSION=8.*
- LARAVEL_VERSION=9.*

cache:
directories:
Expand Down
36 changes: 21 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,34 @@
"email": "[email protected]"
}],
"require": {
"php": "^7.2.5|^8.0",
"dingo/blueprint": "^0.4",
"illuminate/routing": "^7.0|^8.0",
"illuminate/support": "^7.0|^8.0",
"league/fractal": "^0.19"
"php": "^7.2.5|^8.0|^8.1",
"dingo/blueprint": "dev-patch-1",
"illuminate/routing": "^7.0|^8.0|^9.0",
"illuminate/support": "^7.0|^8.0|^9.0",
"league/fractal": "^0.20"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2",
"illuminate/auth": "^7.0|^8.0",
"illuminate/cache": "^7.0|^8.0",
"illuminate/console": "^7.0|^8.0",
"illuminate/database": "^7.0|^8.0",
"illuminate/events": "^7.0|^8.0",
"illuminate/filesystem": "^7.0|^8.0",
"illuminate/log": "^7.0|^8.0",
"illuminate/pagination": "^7.0|^8.0",
"laravel/lumen-framework": "^7.0|^8.0",
"friendsofphp/php-cs-fixer": "~2|~3",
"illuminate/auth": "^7.0|^8.0|^9.0",
"illuminate/cache": "^7.0|^8.0|^9.0",
"illuminate/console": "^7.0|^8.0|^9.0",
"illuminate/database": "^7.0|^8.0|^9.0",
"illuminate/events": "^7.0|^8.0|^9.0",
"illuminate/filesystem": "^7.0|^8.0|^9.0",
"illuminate/log": "^7.0|^8.0|^9.0",
"illuminate/pagination": "^7.0|^8.0|^9.0",
"laravel/lumen-framework": "^7.0|^8.0|^9.0",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "^8.5|^9.0",
"squizlabs/php_codesniffer": "~2.0",
"php-open-source-saver/jwt-auth": "^1.4"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/dmason30/blueprint"
}
],
"suggest": {
"php-open-source-saver/jwt-auth": "Protect your API with JSON Web Tokens."
},
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InternalHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(Response $response, $message = null, Exception $prev
{
$this->response = $response;

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ResourceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct($message = null, $errors = null, Exception $previous
$this->errors = is_array($errors) ? new MessageBag($errors) : $errors;
}

parent::__construct(422, $message, $previous, $headers, $code);
parent::__construct(422, $message ?? '', $previous, $headers, $code);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dingo\Api\Http\Parser\Accept;
use Illuminate\Http\Request as IlluminateRequest;
use Dingo\Api\Contract\Http\Request as RequestInterface;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;

class Request extends IlluminateRequest implements RequestInterface
{
Expand Down Expand Up @@ -35,8 +36,11 @@ public function createFromIlluminate(IlluminateRequest $old)
$old->cookies->all(), $old->files->all(), $old->server->all(), $old->content
);

if ($session = $old->getSession()) {
$new->setLaravelSession($old->getSession());
try {
if ($session = $old->getSession()) {
$new->setLaravelSession($old->getSession());
}
} catch (SessionNotFoundException $exception) {
}

$new->setRouteResolver($old->getRouteResolver());
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function makeFromJson(JsonResponse $json)
*/
public function morph($format = 'json')
{
$this->content = $this->getOriginalContent();
$this->content = $this->getOriginalContent() ?? '';

$this->fireMorphingEvent();

Expand Down Expand Up @@ -192,7 +192,7 @@ protected function fireMorphingEvent()
/**
* {@inheritdoc}
*/
public function setContent($content)
public function setContent(mixed $content): static
{
// Attempt to set the content string, if we encounter an unexpected value
// then we most likely have an object that cannot be type cast. In that
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Validation/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function stripProtocol($domain)
*/
protected function stripPort($domain)
{
if ($domainStripped = preg_replace(self::PATTERN_STRIP_PROTOCOL, null, $domain)) {
if ($domainStripped = preg_replace(self::PATTERN_STRIP_PROTOCOL, '', $domain)) {
return $domainStripped;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Http/Validation/Prefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Prefix implements Validator
* @param string $prefix
* @return void
*/
public function __construct($prefix)
public function __construct(?string $prefix = null)
{
$this->prefix = $prefix;
$this->prefix = $prefix ?? '';
}

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

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

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

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Provider/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Http\Request as IlluminateRequest;
use Dingo\Api\Routing\Adapter\Laravel as LaravelAdapter;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;

class LaravelServiceProvider extends DingoServiceProvider
{
Expand Down Expand Up @@ -197,8 +198,11 @@ protected function initializeRequest(FormRequest $form, IlluminateRequest $curre

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

if ($session = $current->getSession()) {
$form->setLaravelSession($session);
try {
if ($session = $current->getSession()) {
$form->setLaravelSession($current->getSession());
}
} catch (SessionNotFoundException $exception) {
}

$form->setUserResolver($current->getUserResolver());
Expand Down
8 changes: 6 additions & 2 deletions src/Provider/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Dingo\Api\Routing\Adapter\Lumen as LumenAdapter;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use FastRoute\DataGenerator\GroupCountBased as GcbDataGenerator;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;

class LumenServiceProvider extends DingoServiceProvider
{
Expand Down Expand Up @@ -172,8 +173,11 @@ protected function initializeRequest(FormRequest $form, IlluminateRequest $curre

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

if ($session = $current->getSession()) {
$form->setLaravelSession($session);
try {
if ($session = $current->getSession()) {
$form->setLaravelSession($current->getSession());
}
} catch (SessionNotFoundException $exception) {
}

$form->setUserResolver($current->getUserResolver());
Expand Down
4 changes: 2 additions & 2 deletions src/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getRoutes()
*
* @return \ArrayIterator
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new ArrayIterator($this->getRoutes());
}
Expand All @@ -110,7 +110,7 @@ public function getIterator()
*
* @return int
*/
public function count()
public function count(): int
{
return count($this->getRoutes());
}
Expand Down
5 changes: 4 additions & 1 deletion tests/ChecksLaravelVersionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dingo\Api\Tests;

use Dingo\Api\Tests\Stubs\Application9Stub;
use Dingo\Api\Tests\Stubs\ApplicationStub;
use Dingo\Api\Tests\Stubs\Application8Stub;
use Dingo\Api\Tests\Stubs\Application7Stub;
Expand Down Expand Up @@ -54,7 +55,9 @@ private function getApplicationStub()
$version = str_replace('v', '', $version);

// Return the version stub for the right version
if (version_compare($version, '8.0.0', '>=')) {
if (version_compare($version, '9.0.0', '>=')) {
return new Application9Stub;
} elseif (version_compare($version, '8.0.0', '>=')) {
return new Application8Stub;
} elseif (version_compare($version, '7.0.0', '>=')) {
return new Application7Stub;
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Response/Format/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testMorphingArray()

public function testMorphingUnknownType()
{
$this->assertSame(1, (new Response(1))->morph()->getContent());
$this->assertSame('1', (new Response(1))->morph()->getContent());
}

public function testMorphingEloquentModelWithCamelCasing()
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Response/Format/JsonpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testMorphingArray()

public function testMorphingUnknownType()
{
$this->assertSame(1, (new Response(1))->morph()->getContent());
$this->assertSame('1', (new Response(1))->morph()->getContent());
}

public function testMorphingArrayWithOneTabPrettyPrintIndent()
Expand Down
1 change: 0 additions & 1 deletion tests/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function testNonCastableObjectsSetAsOriginalContent()

$response = new Response($object);

$this->assertNull($response->getContent());
$this->assertSame($object, $response->getOriginalContent());
}

Expand Down
Loading

0 comments on commit 8ca5a1e

Please sign in to comment.