/** * Bind listeners defined in plugins, app and Database */ public function bind() { $listeners = $this->cache->get('app-listeners', function () { foreach ($this->resolver->getListeners() as $file) { try { $binding = $this; require_once $file; } catch (\Throwable $e) { $this->logger->warn("Unable to include {$file}: " . $e->getMessage()); } } $listeners = $this->getListeners(); if ($this->database->isConnected()) { /** @var ModelEx $eventModel */ if ($eventModel = $this->resolver->getModel('Event', true)) { try { foreach ($eventModel::all() as $item) { $attrs = $item->attributesToArray(); list($class, $func) = @explode('@', $attrs['handler']); $event = array_merge($attrs, ['event' => $attrs['name'], 'handler' => [sprintf('\\%s', ltrim($class, '\\')), $func ?? 'index']]); $listeners[] = $event; } } catch (\Exception $e) { } } } return $listeners; }, 300); foreach ($listeners as $listener) { $this->dispatcher->listen($listener['event'], $listener['handler'], $listener['priority'] ?? 99, $listener['data'] ?? ''); } }
protected function startQueryLog() { $this->logFile = $this->tmpDir->getTempDir('logs') . '/query.log'; @unlink($this->logFile); $this->connection->enableQueryLog(); $this->logger->pushHandler(new StreamHandler($this->logFile, LoggerEx::INFO)); $this->dispatcher->listen(QueryExecuted::class, function (QueryExecuted $query) { $sql = preg_replace_callback('/\\?/', function () use($query, &$index) { return sprintf("'%s'", $query->bindings[$index++ ?? 0]); }, $query->sql); $this->logger->info($sql); }); }