Skip to content

Commit 28a6df9

Browse files
committed
feat(Http) : ajout de la gestion des en-têtes et amélioration des exceptions
1 parent 6b187d4 commit 28a6df9

6 files changed

Lines changed: 114 additions & 44 deletions

File tree

spec/system/framework/Http/ServerRequest.spec.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use BlitzPHP\Filesystem\Files\UploadedFile;
1313
use BlitzPHP\Http\ServerRequest;
1414

15+
use function Kahlan\expect;
16+
1517
describe('Http / ServerRequest', function (): void {
1618
describe('Detector', function (): void {
1719
it('Custom detector avec des arguments personnalises', function (): void {
@@ -231,14 +233,14 @@
231233
$request = new ServerRequest();
232234

233235
expect(fn() => $request->withUploadedFiles(['avatar' => 'picture']))
234-
->toThrow(new InvalidArgumentException('Fichier invalide à `avatar`'));
236+
->toThrow(new InvalidArgumentException('Fichier invalide à `avatar`.'));
235237
});
236238

237239
it("Remplacement de fichiers avec un fichier invalide imbriquer.", function (): void {
238240
$request = new ServerRequest();
239241

240242
expect(fn() => $request->withUploadedFiles(['user' => ['avatar' => 'not a file']]))
241-
->toThrow(new InvalidArgumentException('Fichier invalide à `user.avatar`'));
243+
->toThrow(new InvalidArgumentException('Fichier invalide à `user.avatar`.'));
242244
});
243245
});
244246
});

src/Config/schemas/app.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'base_url' => Expect::string('auto'),
1616
'charset' => Expect::string('UTF-8'),
1717
'environment' => Expect::string('auto'),
18-
'language' => Expect::string('en'),
18+
'locale' => Expect::string('en'),
1919
'force_global_secure_requests' => Expect::bool(false),
2020
'url_suffix' => Expect::string(''),
2121
'use_absolute_link' => Expect::bool(true),

src/Exceptions/DebugTraceableTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121
*/
2222
trait DebugTraceableTrait
2323
{
24+
/**
25+
* Code par default de l'exceptio
26+
*/
27+
protected int $_defaultCode = 0;
28+
2429
/**
2530
* Ajuste le constructeur de l'exception pour assigner le fichier/la ligne à où
2631
* il est réellement déclenché plutôt que d'être instancié.
2732
*/
28-
final public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
33+
final public function __construct(string $message = '', ?int $code = null, ?Throwable $previous = null)
2934
{
30-
parent::__construct($message, $code, $previous);
35+
parent::__construct($message, $code ?? $this->_defaultCode, $previous);
3136

3237
$trace = $this->getTrace()[0];
3338

src/Exceptions/HttpException.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,50 @@
1313

1414
class HttpException extends FrameworkException
1515
{
16-
public static function methodNotAllowed(string $method): self
16+
/**
17+
* @inheritDoc
18+
*/
19+
protected int $_defaultCode = 500;
20+
21+
/**
22+
* @var array<non-empty-string, array<string>|string>
23+
*/
24+
protected array $headers = [];
25+
26+
/**
27+
* Définir un seul en-tête de réponse HTTP.
28+
*
29+
* @param non-empty-string $header Nom de l'en-tête
30+
* @param array<string>|string|null $value Valeur de l'en-tête
31+
*/
32+
public function setHeader(string $header, array|string|null $value = null): void
33+
{
34+
$this->headers[$header] = $value ?? '';
35+
}
36+
37+
/**
38+
* Définit les en-têtes de réponse HTTP.
39+
*
40+
* @param array<non-empty-string, array<string>|string> $headers Tableau de paires nom/valeur d'en-tête.
41+
*/
42+
public function setHeaders(array $headers): void
43+
{
44+
$this->headers = $headers;
45+
}
46+
47+
/**
48+
* Renvoie le tableau d'en-têtes de réponse.
49+
*
50+
* @return array<non-empty-string, array<string>|string>
51+
*/
52+
public function getHeaders(): array
53+
{
54+
return $this->headers;
55+
}
56+
57+
public static function methodNotAllowed(string $method): MethodNotFoundException
1758
{
18-
return new static(self::lang('HTTP.methodNotAllowed', [$method]));
59+
return new MethodNotFoundException(self::lang('HTTP.methodNotAllowed', [$method]));
1960
}
2061

2162
public static function invalidStatusCode(int $code)

src/Exceptions/MethodNotFoundException.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace BlitzPHP\Exceptions;
1313

14-
use RuntimeException;
15-
1614
/**
1715
* @internal
1816
*/
19-
final class MethodNotFoundException extends RuntimeException
17+
final class MethodNotFoundException extends HttpException
2018
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
protected int $_defaultCode = 405;
2123
}

0 commit comments

Comments
 (0)