Skip to content

Commit b6110fb

Browse files
committed
Merge branch 'main' of github.com:hypervel/components
2 parents 67adf52 + b78d01b commit b6110fb

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/foundation/src/Http/WebsocketKernel.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Hypervel\Foundation\Http;
66

77
use Hyperf\Context\Context;
8-
use Hyperf\Contract\ConfigInterface;
98
use Hyperf\Coordinator\Constants;
109
use Hyperf\Coordinator\CoordinatorManager;
1110
use Hyperf\Engine\Constant;
@@ -16,10 +15,11 @@
1615
use Hyperf\WebSocketServer\Collector\FdCollector;
1716
use Hyperf\WebSocketServer\Context as WsContext;
1817
use Hyperf\WebSocketServer\CoreMiddleware;
19-
use Hyperf\WebSocketServer\Exception\Handler\WebSocketExceptionHandler;
2018
use Hyperf\WebSocketServer\Exception\WebSocketHandShakeException;
2119
use Hyperf\WebSocketServer\Security;
2220
use Hyperf\WebSocketServer\Server as WebSocketServer;
21+
use Hypervel\Foundation\Exceptions\Contracts\ExceptionHandler as ExceptionHandlerContract;
22+
use Hypervel\Foundation\Exceptions\Handler as ExceptionHandler;
2323
use Hypervel\Foundation\Http\Contracts\MiddlewareContract;
2424
use Hypervel\Foundation\Http\Traits\HasMiddleware;
2525
use Psr\Http\Message\ResponseInterface;
@@ -39,10 +39,15 @@ public function initCoreMiddleware(string $serverName): void
3939
$this->serverName = $serverName;
4040
$this->coreMiddleware = make(CoreMiddleware::class, [$this->container, $serverName]);
4141

42-
$config = $this->container->get(ConfigInterface::class);
43-
$this->exceptionHandlers = $config->get('exceptions.handler.' . $serverName, [
44-
WebSocketExceptionHandler::class,
45-
]);
42+
$this->initExceptionHandlers();
43+
}
44+
45+
protected function initExceptionHandlers(): void
46+
{
47+
/* @phpstan-ignore-next-line */
48+
$this->exceptionHandlers = $this->container->bound(ExceptionHandlerContract::class)
49+
? [ExceptionHandlerContract::class]
50+
: [ExceptionHandler::class];
4651
}
4752

4853
/**

src/support/src/DataObject.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ protected static function getDependencyFromUnionType(ReflectionUnionType $type):
220220
{
221221
foreach ($type->getTypes() as $namedType) {
222222
$className = $namedType->getName();
223-
if (is_subclass_of($className, DataObject::class)
224-
|| is_subclass_of($className, DateTimeInterface::class)) {
223+
if (
224+
is_subclass_of($className, DataObject::class)
225+
|| is_subclass_of($className, DateTimeInterface::class)
226+
) {
225227
return $namedType;
226228
}
227229
}
@@ -284,10 +286,18 @@ protected static function resolveDependenciesMap(string $class, array &$visited
284286
];
285287
continue;
286288
}
289+
$dataKey = static::isAutoCasting()
290+
? static::convertPropertyToDataKey($property->getName())
291+
: $property->getName();
292+
if (enum_exists($typeName)) {
293+
$result[$dataKey] = [
294+
'handler' => [$typeName, 'from'],
295+
'nullable' => $allowsNull,
296+
'children' => [],
297+
];
298+
continue;
299+
}
287300
if ($resolver = $customizedDependencies[$typeName] ?? null) {
288-
$dataKey = static::isAutoCasting()
289-
? static::convertPropertyToDataKey($property->getName())
290-
: $property->getName();
291301
$result[$dataKey] = [
292302
'handler' => $resolver,
293303
'nullable' => $allowsNull,

tests/Support/DataObjectTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ public function testMakeWithoutAutoResolve(): void
255255
'city' => 'New York',
256256
'zipCode' => '10001',
257257
],
258+
'gender' => TestGenderEnum::Male,
258259
'created_at' => '2023-01-01 12:00:00',
259260
];
260261

@@ -265,6 +266,7 @@ public function testMakeWithoutAutoResolve(): void
265266
$this->assertSame(['street' => '123 Main St', 'city' => 'New York', 'zipCode' => '10001'], $user->address);
266267
$this->assertIsString($user->createdAt);
267268
$this->assertSame('2023-01-01 12:00:00', $user->createdAt);
269+
$this->assertSame(TestGenderEnum::Male, $user->gender);
268270
}
269271

270272
/**
@@ -279,6 +281,7 @@ public function testMakeWithAutoResolveDataObject(): void
279281
'city' => 'New York',
280282
'zip_code' => '10001',
281283
],
284+
'gender' => 'male',
282285
'created_at' => '2023-01-01 12:00:00',
283286
];
284287

@@ -291,6 +294,7 @@ public function testMakeWithAutoResolveDataObject(): void
291294
$this->assertSame('10001', $user->address->zipCode);
292295
$this->assertInstanceOf(DateTime::class, $user->createdAt);
293296
$this->assertSame('2023-01-01 12:00:00', $user->createdAt->format('Y-m-d H:i:s'));
297+
$this->assertSame(TestGenderEnum::Male, $user->gender);
294298
}
295299

296300
/**
@@ -307,6 +311,7 @@ public function testMakeWithAutoResolveDeepNesting(): void
307311
'city' => 'Boston',
308312
'zip_code' => '02101',
309313
],
314+
'gender' => 'male',
310315
'created_at' => '2023-06-15 09:30:00',
311316
],
312317
];
@@ -320,6 +325,7 @@ public function testMakeWithAutoResolveDeepNesting(): void
320325
$this->assertSame('456 Oak Ave', $company->employee->address->street);
321326
$this->assertSame('Boston', $company->employee->address->city);
322327
$this->assertInstanceOf(DateTime::class, $company->employee->createdAt);
328+
$this->assertSame(TestGenderEnum::Male, $company->employee->gender);
323329
}
324330

325331
/**
@@ -331,13 +337,15 @@ public function testMakeWithAutoResolveNullValues(): void
331337
'name' => 'John Doe',
332338
'address' => null,
333339
'created_at' => null,
340+
'gender' => 'male',
334341
];
335342

336343
$user = TestUserDataObject::make($data, true);
337344

338345
$this->assertSame('John Doe', $user->name);
339346
$this->assertNull($user->address);
340347
$this->assertNull($user->createdAt);
348+
$this->assertSame(TestGenderEnum::Male, $user->gender);
341349
}
342350

343351
protected function getData(): array
@@ -421,6 +429,7 @@ class TestUserDataObject extends DataObject
421429
{
422430
public function __construct(
423431
public string $name,
432+
public TestGenderEnum $gender,
424433
public TestAddressDataObject|array|null $address,
425434
public DateTime|string|null $createdAt,
426435
) {
@@ -438,3 +447,9 @@ public function __construct(
438447
) {
439448
}
440449
}
450+
451+
enum TestGenderEnum: string
452+
{
453+
case Male = 'male';
454+
case Female = 'female';
455+
}

0 commit comments

Comments
 (0)