Skip to content

Commit

Permalink
Merge pull request #1 from TianLiangZhou/hotfix/container-dispatcher
Browse files Browse the repository at this point in the history
修改事件容器问题
  • Loading branch information
TianLiangZhou authored Aug 20, 2018
2 parents b9be5f7 + bf91f1e commit 85ffd56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Surf/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function on($eventName, $eventListener, int $priority = 0)
throw new \Exception("$eventListener not callable");
}
if (!$this->isBoot) {
$this->dispatcher->addListener($eventName, $eventListener, $priority);
$this->get('dispatcher')->addListener($eventName, $eventListener, $priority);
}
return $this;
}
Expand Down
24 changes: 24 additions & 0 deletions src/Surf/Event/ServerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Surf\Event;


use Psr\Container\ContainerInterface;
use Swoole\Server;
use Symfony\Component\EventDispatcher\Event;

Expand All @@ -28,6 +29,11 @@ class ServerEvent extends Event
* @var int
*/
protected $workerId = 0;

/**
* @var null | ContainerInterface
*/
protected $container = null;
/**
* ServerEvent constructor.
* @param Server $server
Expand All @@ -42,4 +48,22 @@ public function __construct(Server $server, int $fd = 0, int $workerId = 0)

$this->workerId = $workerId;
}

/**
* @return null | ContainerInterface
*/
public function getContainer()
{
return $this->container;
}

/**
* @param null $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}


}
10 changes: 8 additions & 2 deletions src/Surf/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ public function onWorkerStart(\Swoole\Server $server, int $workerId)
$server->tick($mill, [$ticker, 'execute']);
}
}
$this->dispatcher->dispatch(Events::SERVER_MANAGER, new ServerEvent($server));
$event = new ServerEvent($server);
$event->setContainer($this->container);
$this->dispatcher->dispatch(Events::SERVER_MANAGER, $event);
}
$this->workerStart($server, $workerId);
$workerNumber = $this->defaultConfig['setting']['worker_num'] ?? 1;
Expand All @@ -263,7 +265,9 @@ public function onConnect(SwooleServer $server, int $fd, int $reactorId)
$redis->hSet(RedisConstant::FULL_FD_WORKER, $fd, $server->worker_id);
}
$this->connect($server, $fd, $reactorId);
$this->dispatcher->dispatch(Events::SERVER_CONNECT, new ServerConnectEvent($server, $fd));
$event = new ServerConnectEvent($server, $fd);
$event->setContainer($this->container);
$this->dispatcher->dispatch(Events::SERVER_CONNECT, $event);
}

/**
Expand All @@ -283,6 +287,8 @@ public function onClose(SwooleServer $server, int $fd, int $reactorId)
$redis->hDel(RedisConstant::FULL_FD_WORKER, $fd);
}
$this->close($server, $fd, $reactorId);
$event = new ServerCloseEvent($server, $fd);
$event->setContainer($this->container);
$this->dispatcher->dispatch(Events::SERVER_CLOSE, new ServerCloseEvent($server, $fd));
}

Expand Down

0 comments on commit 85ffd56

Please sign in to comment.