Skip to content

Commit a2b9278

Browse files
author
Harlan Wilton
committed
Fixing Laravel 7 inconsistencies
1 parent 05299b0 commit a2b9278

File tree

6 files changed

+259
-52
lines changed

6 files changed

+259
-52
lines changed

composer.json

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@
1414
}],
1515
"require": {
1616
"php": "^7.1",
17-
"dingo/blueprint": "^0.3",
18-
"illuminate/routing": "^5.5 || ^6.0",
19-
"illuminate/support": "^5.5 || ^6.0",
17+
"illuminate/routing": "^5.5 || ^6.0 || ^7.0",
18+
"illuminate/support": "^5.5 || ^6.0 || ^7.0",
2019
"league/fractal": "^0.17"
2120
},
2221
"require-dev": {
2322
"phpdocumentor/reflection-docblock": "3.3.2",
2423
"friendsofphp/php-cs-fixer": "~2",
25-
"illuminate/auth": "^5.5 || ^6.0",
26-
"illuminate/cache": "^5.5 || ^6.0",
27-
"illuminate/console": "^5.5 || ^6.0",
28-
"illuminate/database": "^5.5 || ^6.0",
29-
"illuminate/events": "^5.5 || ^6.0",
30-
"illuminate/filesystem": "^5.5 || ^6.0",
31-
"illuminate/log": "^5.5 || ^6.0",
32-
"illuminate/pagination": "^5.5 || ^6.0",
33-
"laravel/lumen-framework": "^5.5 || ^6.0",
24+
"illuminate/auth": "^5.5 || ^6.0 || ^7.0",
25+
"illuminate/cache": "^5.5 || ^6.0 || ^7.0",
26+
"illuminate/console": "^5.5 || ^6.0 || ^7.0",
27+
"illuminate/database": "^5.5 || ^6.0 || ^7.0",
28+
"illuminate/events": "^5.5 || ^6.0 || ^7.0",
29+
"illuminate/filesystem": "^5.5 || ^6.0 || ^7.0",
30+
"illuminate/log": "^5.5 || ^6.0 || ^7.0",
31+
"illuminate/pagination": "^5.5 || ^6.0 || ^7.0",
32+
"laravel/lumen-framework": "^5.5 || ^6.0 || ^7.0",
3433
"mockery/mockery": "~1.0",
3534
"phpunit/phpunit": "^4.8.35 || ^5.4.3 || ^6.5",
3635
"squizlabs/php_codesniffer": "~2.0",
@@ -70,4 +69,4 @@
7069
},
7170
"minimum-stability": "dev",
7271
"prefer-stable": true
73-
}
72+
}

src/Contract/Debug/ExceptionHandler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
namespace Dingo\Api\Contract\Debug;
44

5-
use Exception;
6-
75
interface ExceptionHandler
86
{
97
/**
108
* Handle an exception.
119
*
12-
* @param \Exception $exception
10+
* @param \Throwable|\Exception $exception
1311
*
1412
* @return \Illuminate\Http\Response
1513
*/
16-
public function handle(Exception $exception);
14+
public function handle($exception);
1715
}

src/Exception/Handler.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
namespace Dingo\Api\Exception;
44

5+
use Dingo\Api\Http\Request;
56
use Exception;
7+
use Illuminate\Database\Eloquent\ModelNotFoundException;
68
use ReflectionFunction;
79
use Illuminate\Support\Str;
810
use Illuminate\Http\Response;
911
use Dingo\Api\Contract\Debug\ExceptionHandler;
1012
use Dingo\Api\Contract\Debug\MessageBagErrors;
1113
use Illuminate\Validation\ValidationException;
14+
use Symfony\Component\Console\Output\OutputInterface;
1215
use Symfony\Component\HttpFoundation\Response as BaseResponse;
1316
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1417
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
1518
use Illuminate\Contracts\Debug\ExceptionHandler as IlluminateExceptionHandler;
19+
use Throwable;
1620

1721
class Handler implements ExceptionHandler, IlluminateExceptionHandler
1822
{
@@ -47,14 +51,14 @@ class Handler implements ExceptionHandler, IlluminateExceptionHandler
4751
/**
4852
* The parent Illuminate exception handler instance.
4953
*
50-
* @var \Illuminate\Contracts\Debug\ExceptionHandler
54+
* @var IlluminateExceptionHandler
5155
*/
5256
protected $parentHandler;
5357

5458
/**
5559
* Create a new exception handler instance.
5660
*
57-
* @param \Illuminate\Contracts\Debug\ExceptionHandler $parentHandler
61+
* @param IlluminateExceptionHandler $parentHandler
5862
* @param array $format
5963
* @param bool $debug
6064
*
@@ -70,51 +74,51 @@ public function __construct(IlluminateExceptionHandler $parentHandler, array $fo
7074
/**
7175
* Report or log an exception.
7276
*
73-
* @param \Exception $exception
77+
* @param Throwable $exception
7478
*
7579
* @return void
7680
*/
77-
public function report(Exception $exception)
81+
public function report(Throwable $throwable)
7882
{
79-
$this->parentHandler->report($exception);
83+
$this->parentHandler->report($throwable);
8084
}
8185

8286
/**
8387
* Determine if the exception should be reported.
8488
*
85-
* @param \Exception $e
89+
* @param Throwable $e
8690
*
8791
* @return bool
8892
*/
89-
public function shouldReport(Exception $e)
93+
public function shouldReport(Throwable $e)
9094
{
9195
return true;
9296
}
9397

9498
/**
9599
* Render an exception into an HTTP response.
96100
*
97-
* @param \Dingo\Api\Http\Request $request
98-
* @param \Exception $exception
99-
*
100-
* @throws \Exception
101+
* @param Request $request
102+
* @param Throwable $exception
101103
*
102104
* @return mixed
105+
*@throws Exception
106+
*
103107
*/
104-
public function render($request, Exception $exception)
108+
public function render($request, Throwable $exception)
105109
{
106110
return $this->handle($exception);
107111
}
108112

109113
/**
110114
* Render an exception to the console.
111115
*
112-
* @param \Symfony\Component\Console\Output\OutputInterface $output
113-
* @param \Exception $exception
116+
* @param OutputInterface $output
117+
* @param Throwable $exception
114118
*
115119
* @return mixed
116120
*/
117-
public function renderForConsole($output, Exception $exception)
121+
public function renderForConsole($output, Throwable $exception)
118122
{
119123
return $this->parentHandler->renderForConsole($output, $exception);
120124
}
@@ -136,14 +140,14 @@ public function register(callable $callback)
136140
/**
137141
* Handle an exception if it has an existing handler.
138142
*
139-
* @param \Exception $exception
143+
* @param Throwable|Exception $exception
140144
*
141-
* @return \Illuminate\Http\Response
145+
* @return Response
142146
*/
143-
public function handle(Exception $exception)
147+
public function handle($exception)
144148
{
145149
// Convert Eloquent's 500 ModelNotFoundException into a 404 NotFoundHttpException
146-
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
150+
if ($exception instanceof ModelNotFoundException) {
147151
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
148152
}
149153

@@ -167,13 +171,13 @@ public function handle(Exception $exception)
167171
/**
168172
* Handle a generic error response if there is no handler available.
169173
*
170-
* @param \Exception $exception
174+
* @param Throwable $exception
171175
*
172-
* @throws \Exception
176+
* @return Response
177+
*@throws Throwable
173178
*
174-
* @return \Illuminate\Http\Response
175179
*/
176-
protected function genericResponse(Exception $exception)
180+
protected function genericResponse(Throwable $exception)
177181
{
178182
$replacements = $this->prepareReplacements($exception);
179183

@@ -193,11 +197,11 @@ protected function genericResponse(Exception $exception)
193197
/**
194198
* Get the status code from the exception.
195199
*
196-
* @param \Exception $exception
200+
* @param Throwable $exception
197201
*
198202
* @return int
199203
*/
200-
protected function getStatusCode(Exception $exception)
204+
protected function getStatusCode(Throwable $exception)
201205
{
202206
if ($exception instanceof ValidationException) {
203207
return $exception->status;
@@ -209,23 +213,23 @@ protected function getStatusCode(Exception $exception)
209213
/**
210214
* Get the headers from the exception.
211215
*
212-
* @param \Exception $exception
216+
* @param Throwable $exception
213217
*
214218
* @return array
215219
*/
216-
protected function getHeaders(Exception $exception)
220+
protected function getHeaders(Throwable $exception)
217221
{
218222
return $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : [];
219223
}
220224

221225
/**
222226
* Prepare the replacements array by gathering the keys and values.
223227
*
224-
* @param \Exception $exception
228+
* @param Throwable $exception
225229
*
226230
* @return array
227231
*/
228-
protected function prepareReplacements(Exception $exception)
232+
protected function prepareReplacements(Throwable $exception)
229233
{
230234
$statusCode = $this->getStatusCode($exception);
231235

@@ -322,7 +326,7 @@ protected function newResponseArray()
322326
/**
323327
* Get the exception status code.
324328
*
325-
* @param \Exception $exception
329+
* @param Exception $exception
326330
* @param int $defaultStatusCode
327331
*
328332
* @return int

src/Http/Response.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct($content, $status = 200, $headers = [], Binding $bin
8787
*/
8888
public static function makeFromExisting(IlluminateResponse $old)
8989
{
90-
$new = static::create($old->getOriginalContent(), $old->getStatusCode());
90+
$new = new static($old->getOriginalContent(), $old->getStatusCode());
9191

9292
$new->headers = $old->headers;
9393

@@ -112,7 +112,7 @@ public static function makeFromJson(JsonResponse $json)
112112
$content = json_decode($json->getContent(), true);
113113
}
114114

115-
$new = static::create($content, $json->getStatusCode());
115+
$new = new static($content, $json->getStatusCode());
116116

117117
$new->headers = $json->headers;
118118

@@ -196,11 +196,15 @@ public function setContent($content)
196196
// then we most likely have an object that cannot be type cast. In that
197197
// case we'll simply leave the content as null and set the original
198198
// content value and continue.
199+
if (!empty($content) && is_object($content) && !$this->shouldBeJson($content)) {
200+
$this->original = $content;
201+
return $this;
202+
}
203+
199204
try {
200205
return parent::setContent($content);
201206
} catch (UnexpectedValueException $exception) {
202207
$this->original = $content;
203-
204208
return $this;
205209
}
206210
}

tests/ChecksLaravelVersionTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Dingo\Api\Tests;
44

5+
use Dingo\Api\Tests\Stubs\Application7Stub;
56
use Dingo\Api\Tests\Stubs\ApplicationStub;
67
use Dingo\Api\Tests\Stubs\Application6Stub;
78
use Dingo\Api\Tests\Stubs\Application58Stub;
89

910
trait ChecksLaravelVersionTrait
1011
{
1112
public $installed_file_path = __DIR__.'/../vendor/composer/installed.json';
12-
public $current_release = '5.8';
13+
public $current_release = '7.0';
1314

1415
private function getFrameworkVersion()
1516
{
@@ -38,7 +39,9 @@ private function getApplicationStub()
3839
$version = str_replace('v', '', $version);
3940

4041
// Return the version stub for the right version
41-
if (version_compare($version, '6.0.0', '>=')) {
42+
if (version_compare($version, '7.0.0', '>=')) {
43+
return new Application7Stub;
44+
} else if (version_compare($version, '6.0.0', '>=')) {
4245
return new Application6Stub;
4346
} elseif (version_compare($version, '5.8.0', '>=')) {
4447
return new Application58Stub;

0 commit comments

Comments
 (0)