diff --git a/ethereum.routing.yml b/ethereum.routing.yml index 9bf4ac1..38b215b 100644 --- a/ethereum.routing.yml +++ b/ethereum.routing.yml @@ -1,8 +1,9 @@ ethereum.status: - path: '/admin/reports/ethereum' + path: '/admin/reports/ethereum/{server_id}' defaults: _controller: 'Drupal\ethereum\Controller\EthereumController::status' _title: 'Ethereum' + server_id: NULL requirements: _permission: 'administer site configuration' diff --git a/src/Controller/EthereumController.php b/src/Controller/EthereumController.php index 2ecc659..254ce17 100644 --- a/src/Controller/EthereumController.php +++ b/src/Controller/EthereumController.php @@ -9,6 +9,7 @@ use Ethereum\DataType\EthBlockParam; use Ethereum\DataType\EthB; use Drupal\Core\Render\Markup; +use Symfony\Component\HttpFoundation\Request; /** * Controller routines for Ethereum routes. @@ -22,7 +23,7 @@ class EthereumController extends ControllerBase { */ protected $web3; - // @todo Doesn't seem to be propagetad to the library anymore. + // @todo Doesn't seem to be propagated to the library anymore. private $debug = TRUE; /** @@ -63,18 +64,50 @@ public function debug($clear = FALSE) { /** * Displays the ethereum status report page. * - * This page provides a overview about Ethereum functions and usage. + * This page provides an overview of Ethereum functions and usage. * - * @throws \Exception + * @param string|null $server_id + * The id of the server to report on. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The current request. * * @return array - * Render array. Table with current status of the ethereum node. + * Render array. Table with current status of the ethereum node. Or just + * the server select form after initial submission. + * + * @throws \Exception + * If the server can not be loaded. */ - public function status() { - // Default server. - $server = \Drupal::service('ethereum.manager')->getCurrentServer(); + public function status($server_id = NULL, Request $request) { + // Used to tell if the server select form was submitted. + $form_id = $request->request->get('form_id'); + // If the server select form was submitted, we don't need to build the + // report because it will be built once the form redirect is complete. + // But we do need to build the form again otherwise it doesn't process. + if ($form_id && $form_id == 'ethereum_status_server_select_form') { + $server_id = $request->attributes->get('server_id'); + return ['choose_server' => \Drupal::formBuilder()->getForm('Drupal\ethereum\Form\EthereumStatusServerSelectForm', $server_id)]; + } + if ($server_id) { + try { + $server = \Drupal::service('ethereum.manager')->getServer($server_id); + $host = $server->get('url'); + $this->web3 = new Ethereum($host); + } + catch (\Exception $e) { + \Drupal::logger('ethereum')->error($e->getMessage()); + return [ + '#markup' => $e->getMessage(), + ]; + } + } + else { + // Load the server. + $server = \Drupal::service('ethereum.manager')->getCurrentServer(); + } - // Validate active server. + // Validate the server. $liveStatus = $server->validateConnection(); $this->messenger()->addMessage( $liveStatus['message'], ($liveStatus['error']) ? 'error' : 'status' @@ -133,11 +166,9 @@ public function status() { ] ]; - $random_rows[] = [$this->t('JsonRPC standard Methods'), $this->t('Read more about Ethereum JsonRPC-API implementation.')]; $random_rows[] = [$this->t('Ethereum-PHP'), $this->t('Ethereum Web3 PHP API reference and codebase.')]; - // Blocks. $random_rows[] = [$this->t("Block info"), '']; $block_latest = $this->web3->eth_getBlockByNumber(new EthBlockParam('latest'), new EthB(FALSE)); @@ -176,7 +207,6 @@ public function status() { $high_block, ]; - // More. // Ethereum sha3 != standardized sha3, but a "Keccak-256" @@ -206,6 +236,7 @@ public function status() { // $this->debug(); return [ + 'choose_server' => \Drupal::formBuilder()->getForm('Drupal\ethereum\Form\EthereumStatusServerSelectForm', $server_id), 'server_info' => $serverInfo, 'server_live_status' => $serverLiveInfo, 'random_stuff' => $serverRandomRows, diff --git a/src/EthereumManager.php b/src/EthereumManager.php index cdfee2e..ecb63e8 100644 --- a/src/EthereumManager.php +++ b/src/EthereumManager.php @@ -73,6 +73,22 @@ public function getCurrentServer() { return $server; } + /** + * {@inheritdoc} + */ + public function getServer($server_id) { + /** @var \Drupal\ethereum\EthereumServerInterface $server */ + $server = $this->serverStorage->load($server_id); + if (!$server) { + throw new \RuntimeException('Server (' . $server_id . ') does not exist.'); + } + if (!$server->status()) { + throw new \RuntimeException('Server is not enabled.'); + } + + return $server; + } + /** * {@inheritdoc} */ diff --git a/src/EthereumManagerInterface.php b/src/EthereumManagerInterface.php index 7873611..7c82de2 100644 --- a/src/EthereumManagerInterface.php +++ b/src/EthereumManagerInterface.php @@ -15,6 +15,17 @@ interface EthereumManagerInterface { */ public function getCurrentServer(); + /** + * Returns the requested backend server. + * + * @param string $server_id + * The id of the server. + * + * @return \Drupal\ethereum\EthereumServerInterface + * An ethereum_server object. + */ + public function getServer($server_id); + /** * Returns the frontend server. * diff --git a/src/Form/EthereumStatusServerSelectForm.php b/src/Form/EthereumStatusServerSelectForm.php new file mode 100644 index 0000000..b93311b --- /dev/null +++ b/src/Form/EthereumStatusServerSelectForm.php @@ -0,0 +1,89 @@ +ethereumManager = $ethereum_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('ethereum.manager') + ); + } + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'ethereum_status_server_select_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $server_id = NULL) { + $config = $this->config('ethereum.settings'); + $enabled_servers = $this->ethereumManager->getServersAsOptions(TRUE); + + $form['#title'] = $this->t('Check the status of any active server'); + + $form['server'] = [ + '#type' => 'select', + '#title' => $this->t('Reporting on:'), + '#required' => TRUE, + '#description' => $this->t('Choose a server to report on. Only enabled servers are listed.'), + '#options' => $enabled_servers, + '#default_value' => $server_id ?: $config->get('current_server'), + ]; + + $form['actions'] = [ + '#type' => 'actions', + ]; + + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Submit'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + if ($form_state->getValue('server')) { + $form_state->setRedirect( + 'ethereum.status', + ['server_id' => $form_state->getValue('server')] + ); + } + } + +}