connection = $connection; $this->events = $events; } /** * Execute the console command. * * @return void */ public function handle() { $databases = $this->parseDatabases($this->option('databases')); $this->displayConnections($databases); if ($this->option('max')) { $this->dispatchEvents($databases); } } /** * Parse the database into an array of the connections. * * @param string $databases * @return \Illuminate\Support\Collection */ protected function parseDatabases($databases) { return collect(explode(',', $databases))->map(function ($database) { if (! $database) { $database = $this->laravel['config']['database.default']; } $maxConnections = $this->option('max'); return [ 'database' => $database, 'connections' => $connections = $this->getConnectionCount($this->connection->connection($database)), 'status' => $maxConnections && $connections >= $maxConnections ? 'ALERT' : 'OK', ]; }); } /** * Display the databases and their connection counts in the console. * * @param \Illuminate\Support\Collection $databases * @return void */ protected function displayConnections($databases) { $this->newLine(); $this->components->twoColumnDetail('Database name', 'Connections'); $databases->each(function ($database) { $status = '['.$database['connections'].'] '.$database['status']; $this->components->twoColumnDetail($database['database'], $status); }); $this->newLine(); } /** * Dispatch the database monitoring events. * * @param \Illuminate\Support\Collection $databases * @return void */ protected function dispatchEvents($databases) { $databases->each(function ($database) { if ($database['status'] === 'OK') { return; } $this->events->dispatch( new DatabaseBusy( $database['database'], $database['connections'] ) ); }); } }