/** * @param int|string $id * * @return \Illuminate\View\View */ public function edit($id) { $servers = $this->getAvailableServers(); $_cluster = $this->_findCluster($id); $_clusterServers = $this->_clusterServers($id); $_datas = ['web' => null, 'db' => null, 'app' => null]; foreach ($_clusterServers as $_type => $_servers) { $_serverType = strtolower(ServerTypes::nameOf($_type, false)); foreach ($_servers as $_server) { $_datas[$_serverType] = ['id' => $_server->id, 'name' => $_server->server_id_text]; } } return $this->renderView('app.clusters.edit', ['cluster_id' => $id, 'cluster' => $_cluster, 'db' => $servers['db'], 'web' => $servers['web'], 'app' => $servers['app'], 'datas' => $_datas]); }
/** * Returns all servers registered on $clusterId * * @param Cluster|int $clusterId * * @return array */ protected static function _lookupClusterServers($clusterId) { $_cluster = $clusterId instanceof Cluster ? $clusterId : static::_lookupCluster($clusterId); /** @noinspection PhpUndefinedMethodInspection */ $_rows = DB::select(<<<MYSQL SELECT s.id, s.server_id_text, s.server_type_id, csa.cluster_id FROM cluster_server_asgn_t csa JOIN server_t s ON s.id = csa.server_id WHERE csa.cluster_id = :cluster_id MYSQL , [':cluster_id' => $_cluster->id]); // Organize by type $_servers = ['cluster' => $_cluster]; foreach (ServerTypes::getDefinedConstants() as $_name => $_value) { $_servers[$_value] = ['.id' => null, '.ids' => [], '.name' => $_name, 'data' => []]; } /** * @type Server $_server */ foreach ($_rows as $_server) { if (!isset($_servers[$_server->server_type_id])) { continue; } $_servers[$_server->server_type_id]['data'][$_server->server_id_text] = (array) $_server; $_servers[$_server->server_type_id]['.ids'][] = $_server->id; } // Set the single id for quick lookups foreach ($_servers as $_type => $_group) { if (null !== array_get($_group, '.id')) { continue; } if (null !== ($_list = array_get($_group, '.ids'))) { if (!empty($_list) && is_array($_list)) { $_servers[$_type]['.id'] = $_list[0]; continue; } } if (null !== ($_list = array_get($_group, 'data'))) { if (!empty($_list) && is_array($_list)) { foreach ($_list as $_item) { if (isset($_item['id'])) { $_servers[$_type['.id']] = $_item['id']; continue; } } } } } return $_servers; }
/** * @param array $servers * @param string $name * * @return mixed */ protected function extractServerIds(array $servers, $name = 'id') { $_list = ServerTypes::getDefinedConstants(true); $_types = array_flip($_list); foreach ($_list as $_typeId => $_typeName) { $_types[$_typeId] = false; if (null === ($_server = array_get($servers, $_typeId))) { continue; } if (null !== ($_id = array_get($_server, '.' . $name))) { $_types[$_typeId] = $_id; continue; } if (null !== ($_ids = array_get($_server, '.' . $name . 's'))) { if (is_array($_ids) && !empty($_ids)) { $_types[$_typeId] = $_ids[0]; continue; } } } return $_types; }
/** * @param bool|string $create If false, no data will be required. Pass $serverId to have data be required and fill * server_id_text field * * @return array|bool */ protected function _prepareData($create = false) { $_data = []; if (!is_bool($create)) { $_serverId = trim($create); $create = true; try { $this->_findServer($_serverId); $this->writeln('the server-id "' . $_serverId . '" already exists.', 'error'); return false; } catch (ModelNotFoundException $_ex) { // This is what we want... } $_data['server_id_text'] = $_serverId; } // Server type $_serverType = $this->option('server-type'); try { $_type = ServerTypes::defines(trim(strtoupper($_serverType)), true); $_data['server_type_id'] = $_type; } catch (\Exception $_ex) { if ($create) { $this->writeln('the server-type "' . $_serverType . '" is not valid.', 'error'); return false; } } // Mount $_mountId = $this->option('mount-id'); try { $_mount = $this->_findMount($_mountId); $_data['mount_id'] = $_mount->id; } catch (\Exception $_ex) { if ($create) { $this->writeln('the mount-id "' . $_mountId . '" does not exists.', 'error'); return false; } } // Host name if (!$this->optionString('host-name', 'host_text', $_data, $create)) { return false; } // Config (optional) if (!$this->optionArray('config', 'config_text', $_data, $create)) { return false; } $_timestamp = new Carbon(); if (!isset($_data['create_date']) || '0000-00-00 00:00:00' == $_data['create_date']) { $_data['create_date'] = $_timestamp; } if (!isset($_data['lmod_date']) || '0000-00-00 00:00:00' == $_data['lmod_date']) { $_data['lmod_date'] = $_timestamp; } return $_data; }