Skip to content

Commit

Permalink
修复服务注入问题
Browse files Browse the repository at this point in the history
  • Loading branch information
youmingdot committed Feb 9, 2018
1 parent cabb076 commit 5e75bcb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Server/ServerExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Lawoole\Application;
use Lawoole\Console\OutputStyle;
use Lawoole\Contracts\Foundation\ApplicationInterface;
use Lawoole\Swoole\Exception\ExceptionHandlerInterface;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Throwable;
Expand All @@ -14,7 +14,7 @@ class ServerExceptionHandler implements ExceptionHandlerInterface
/**
* 服务容器
*
* @var \Lawoole\Contracts\Foundation\ApplicationInterface
* @var \Lawoole\Application
*/
protected $app;

Expand All @@ -28,10 +28,10 @@ class ServerExceptionHandler implements ExceptionHandlerInterface
/**
* 创建异常处理器
*
* @param \Lawoole\Contracts\Foundation\ApplicationInterface $app
* @param \Lawoole\Application $app
* @param \Lawoole\Console\OutputStyle $outputStyle
*/
public function __construct(ApplicationInterface $app, OutputStyle $outputStyle)
public function __construct(Application $app, OutputStyle $outputStyle)
{
$this->app = $app;
$this->outputStyle = $outputStyle;
Expand Down
3 changes: 2 additions & 1 deletion src/Server/ServerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Lawoole\Application;
use Lawoole\Console\OutputStyle;
use Lawoole\Swoole\Handlers\ServerHandlerInterface;
use Lawoole\Swoole\Handlers\ServerSocketBufferHandlerInterface;
Expand Down Expand Up @@ -34,7 +35,7 @@ class ServerHandler implements ServerHandlerInterface, ServerSocketBufferHandler
* @param \Lawoole\Application $app
* @param \Lawoole\Console\OutputStyle $outputStyle
*/
public function __construct($app, OutputStyle $outputStyle)
public function __construct(Application $app, OutputStyle $outputStyle)
{
$this->app = $app;
$this->outputStyle = $outputStyle;
Expand Down
17 changes: 14 additions & 3 deletions src/Server/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ServerManager
/**
* 服务容器
*
* @var \Lawoole\Contracts\Foundation\ApplicationInterface
* @var \Lawoole\Application
*/
protected $app;

Expand All @@ -36,10 +36,17 @@ class ServerManager
*/
protected $server;

/**
* 样式输出
*
* @var \Lawoole\Console\OutputStyle
*/
protected $outputStyle;

/**
* 创建 Swoole 服务管理器
*
* @param \Lawoole\Contracts\Foundation\ApplicationInterface $app
* @param \Lawoole\Application $app
*/
public function __construct($app)
{
Expand Down Expand Up @@ -71,8 +78,10 @@ public function prepare(InputInterface $input, OutputInterface $output)
$this->prepared = true;

if (!$this->app->bound(OutputStyle::class)) {
$this->outputStyle = new OutputStyle($input, $output);

// 共享一个统一的输出
$this->app->instance(OutputStyle::class, new OutputStyle($input, $output));
$this->app->instance(OutputStyle::class, $this->outputStyle);
}

$config = $this->app->make('config')->get('server', []);
Expand All @@ -92,10 +101,12 @@ public function prepare(InputInterface $input, OutputInterface $output)
*/
protected function configureServer($server, array $config)
{
// 设置异常处理器
$server->setExceptionHandler(
$this->app->make(Arr::get($config, 'exception.handler', ServerExceptionHandler::class))
);

// 设置事件处理器
$server->setEventHandler(
$this->app->make(Arr::get($config, 'handler', ServerHandler::class))
);
Expand Down

0 comments on commit 5e75bcb

Please sign in to comment.