| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace think\swoole\concerns;
- use think\App;
- use think\console\Output;
- use think\exception\Handle;
- use Throwable;
- trait WithContainer
- {
- /**
- * @var App
- */
- protected $container;
- /**
- * Manager constructor.
- * @param App $container
- */
- public function __construct(App $container)
- {
- $this->container = $container;
- }
- protected function getContainer()
- {
- return $this->container;
- }
- /**
- * 获取配置
- * @param string $name
- * @param null $default
- * @return mixed
- */
- public function getConfig(string $name, $default = null)
- {
- return $this->container->config->get("swoole.{$name}", $default);
- }
- /**
- * 触发事件
- * @param string $event
- * @param null $params
- */
- protected function triggerEvent(string $event, $params = null): void
- {
- $this->container->event->trigger("swoole.{$event}", $params);
- }
- /**
- * 监听事件
- * @param string $event
- * @param $listener
- * @param bool $first
- */
- public function onEvent(string $event, $listener, bool $first = false): void
- {
- $this->container->event->listen("swoole.{$event}", $listener, $first);
- }
- /**
- * Log server error.
- *
- * @param Throwable $e
- */
- public function logServerError(Throwable $e)
- {
- /** @var Handle $handle */
- $handle = $this->container->make(Handle::class);
- $handle->renderForConsole(new Output(), $e);
- $handle->report($e);
- }
- }
|