Skip to content

Commit d457d26

Browse files
committed
Optimized
1 parent e647cb1 commit d457d26

File tree

12 files changed

+55
-243
lines changed

12 files changed

+55
-243
lines changed

app/Controller/Controller.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace App\Controller;
1414

15-
use App\Kernel\Http\Response;
1615
use Hyperf\HttpServer\Contract\RequestInterface;
16+
use Hyperf\HttpServer\Contract\ResponseInterface;
1717
use Psr\Container\ContainerInterface;
1818

1919
abstract class Controller
@@ -24,19 +24,19 @@ abstract class Controller
2424
protected $container;
2525

2626
/**
27-
* @var Response
27+
* @var RequestInterface
2828
*/
29-
protected $response;
29+
protected $request;
3030

3131
/**
32-
* @var RequestInterface
32+
* @var ResponseInterface
3333
*/
34-
protected $request;
34+
protected $response;
3535

3636
public function __construct(ContainerInterface $container)
3737
{
3838
$this->container = $container;
39-
$this->response = $container->get(Response::class);
4039
$this->request = $container->get(RequestInterface::class);
40+
$this->response = $container->get(ResponseInterface::class);
4141
}
4242
}

app/Controller/IndexController.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ public function index()
1818
{
1919
$user = $this->request->input('user', 'Hyperf');
2020
$method = $this->request->getMethod();
21-
$file = $this->request->file('file');
2221

23-
return $this->response->success([
24-
'user' => $user,
22+
return [
2523
'method' => $method,
26-
'message' => 'Hello Hyperf.',
27-
'file' => $file ? $file->getFilename() : null,
28-
]);
24+
'message' => "Hello $user.",
25+
];
2926
}
3027
}

app/Exception/BusinessException.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://doc.hyperf.io
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace App\Exception\Handler;
14+
15+
use Hyperf\Contract\StdoutLoggerInterface;
16+
use Hyperf\ExceptionHandler\ExceptionHandler;
17+
use Hyperf\HttpMessage\Stream\SwooleStream;
18+
use Psr\Http\Message\ResponseInterface;
19+
use Throwable;
20+
21+
class AppExceptionHandler extends ExceptionHandler
22+
{
23+
/**
24+
* @var StdoutLoggerInterface
25+
*/
26+
protected $logger;
27+
28+
public function __construct(StdoutLoggerInterface $logger)
29+
{
30+
$this->logger = $logger;
31+
}
32+
33+
public function handle(Throwable $throwable, ResponseInterface $response)
34+
{
35+
$this->logger->error($throwable->getTraceAsString());
36+
return $response->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
37+
}
38+
39+
public function isValid(Throwable $throwable): bool
40+
{
41+
return true;
42+
}
43+
}

app/Exception/Handler/BusinessExceptionHandler.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

app/Kernel/Functions.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

app/Kernel/Http/Response.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

app/Kernel/Log/LoggerFactory.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

bin/hyperf.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
1111

12-
// TODO: Later move to the right place
1312
\Swoole\Runtime::enableCoroutine(true);
1413

1514
require BASE_PATH . '/vendor/autoload.php';

config/autoload/exceptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
return [
1414
'handler' => [
1515
'http' => [
16-
App\Exception\Handler\BusinessExceptionHandler::class,
16+
App\Exception\Handler\AppExceptionHandler::class,
1717
],
1818
],
1919
];

0 commit comments

Comments
 (0)