You are here

class DefaultController in Schema 8

Default controller for the schema module.

Hierarchy

Expanded class hierarchy of DefaultController

File

src/Controller/DefaultController.php, line 14
Contains \Drupal\schema\Controller\DefaultController.

Namespace

Drupal\schema\Controller
View source
class DefaultController extends ControllerBase {
  public function schema_sql($engine = NULL) {
    $schema = schema_get_schema(TRUE);
    $sql = '';
    foreach ($schema as $name => $table) {
      if (substr($name, 0, 1) == '#') {
        continue;
      }
      if ($engine) {
        $stmts = call_user_func('schema_' . $engine . '_create_table_sql', $table);
      }
      else {
        $stmts = schema_dbobject()
          ->getCreateTableSql($name, $table);
      }
      $sql .= implode(";\n", $stmts) . ";\n\n";
    }
    return array(
      '#type' => 'textarea',
      '#rows' => 30,
      '#value' => $sql,
      '#attributes' => array(
        'style' => 'width:100%;',
      ),
    );
  }
  public function schema_show() {
    $schema = schema_get_schema(TRUE);
    return array(
      '#type' => 'textarea',
      '#rows' => 30,
      '#value' => var_export($schema, TRUE),
      '#attributes' => array(
        'style' => 'width:100%;',
      ),
    );
  }
  public function schema_compare() {
    $build = array();
    $states = array(
      'same' => t('Match'),
      'different' => t('Mismatch'),
      'missing' => t('Missing'),
    );
    $descs = array(
      'same' => t('Tables for which the schema and database agree.'),
      'different' => t('Tables for which the schema and database are different.'),
      'missing' => t('Tables in the schema that are not present in the database.'),
    );
    $schema = schema_get_schema(TRUE);
    $info = schema_compare_schemas($schema);

    // The info array is keyed by state (same/different/missing/extra/warn). For missing,
    // the value is a simple array of table names. For warn, it is a simple array of warnings.
    // Get those out of the way first
    if (isset($info['warn'])) {
      foreach ($info['warn'] as $message) {
        drupal_set_message($message, 'warning');
      }
      unset($info['warn']);
    }
    $build['extra'] = array(
      '#type' => 'details',
      '#title' => t('Extra (@count)', array(
        '@count' => isset($info['extra']) ? count($info['extra']) : 0,
      )),
      '#description' => t('Tables in the database that are not present in the schema. This indicates previously installed modules that are disabled but not un-installed or modules that do not use the Schema API.'),
      '#weight' => 50,
    );
    $build['extra']['tablelist'] = array(
      '#theme' => 'item_list',
      '#items' => isset($info['extra']) ? $info['extra'] : array(),
    );
    unset($info['extra']);

    // For the other states, the value is an array keyed by module name. Each value
    // in that array is an array keyed by tablename, and each of those values is an
    // array containing 'status' (same as the state), an array of reasons, and an array of notes.
    $weight = 0;
    foreach ($info as $state => $modules) {

      // We'll fill in the fieldset title below, once we have the counts
      $build[$state] = array(
        '#type' => 'details',
        '#description' => $descs[$state],
        '#weight' => $weight++,
      );
      $counts[$state] = 0;
      foreach ($modules as $module => $tables) {
        $counts[$state] += count($tables);
        $build[$state][$module] = array(
          '#type' => 'details',
          '#title' => $module,
        );
        switch ($state) {
          case 'same':
          case 'missing':
            $build[$state][$module]['tablelist'] = array(
              '#theme' => 'item_list',
              '#items' => array_keys($tables),
            );
            break;
          case 'different':
            $items = array();
            foreach ($tables as $name => $stuff) {
              $build[$state][$module][$name] = array(
                '#type' => 'details',
                '#title' => $name,
              );
              $build[$state][$module][$name]['reasons'] = array(
                '#theme' => 'item_list',
                '#items' => array_merge($tables[$name]['reasons'], $tables[$name]['notes']),
              );
            }
            break;
        }
      }
    }

    // Fill in counts in titles
    foreach ($states as $state => $description) {
      $build[$state]['#title'] = t('@state (@count)', array(
        '@state' => $states[$state],
        '@count' => isset($counts[$state]) ? $counts[$state] : 0,
      ));
    }
    return $build;
  }
  public function schema_describe() {
    $build = array();
    $schema = schema_get_schema(TRUE);
    ksort($schema);
    $row_hdrs = array(
      t('Name'),
      t('Type[:Size]'),
      t('Null?'),
      t('Default'),
    );
    $default_table_description = t('TODO: please describe this table!');
    $default_field_description = t('TODO: please describe this field!');
    foreach ($schema as $t_name => $t_spec) {
      $rows = array();
      foreach ($t_spec['fields'] as $c_name => $c_spec) {
        $row = array();
        $row[] = $c_name;
        $type = $c_spec['type'];
        if (!empty($c_spec['length'])) {
          $type .= '(' . $c_spec['length'] . ')';
        }
        if (!empty($c_spec['scale']) && !empty($c_spec['precision'])) {
          $type .= '(' . $c_spec['precision'] . ', ' . $c_spec['scale'] . ' )';
        }
        if (!empty($c_spec['size']) && $c_spec['size'] != 'normal') {
          $type .= ':' . $c_spec['size'];
        }
        if ($c_spec['type'] == 'int' && !empty($c_spec['unsigned'])) {
          $type .= ', unsigned';
        }
        $row[] = $type;
        $row[] = !empty($c_spec['not null']) ? 'NO' : 'YES';
        $row[] = isset($c_spec['default']) ? is_string($c_spec['default']) ? '\'' . $c_spec['default'] . '\'' : $c_spec['default'] : '';
        $rows[] = $row;
        if (!empty($c_spec['description']) && $c_spec['description'] != $default_field_description) {
          $desc = _schema_process_description($c_spec['description']);
          $rows[] = array(
            array(
              'colspan' => count($row_hdrs),
              'data' => $desc,
            ),
          );
        }
        else {
          drupal_set_message(_schema_process_description(t('Field {!table}.@field has no description.', array(
            '!table' => $t_name,
            '@field' => $c_name,
          ))), 'warning');
        }
      }
      if (empty($t_spec['description']) || $t_spec['description'] == $default_table_description) {
        drupal_set_message(_schema_process_description(t('Table {!table} has no description.', array(
          '!table' => $t_name,
        ))), 'warning');
      }
      $build[$t_name] = array(
        '#type' => 'fieldset',
        '#title' => t('@table (@module module)', array(
          '@table' => $t_name,
          '@module' => isset($t_spec['module']) ? $t_spec['module'] : '',
        )),
        '#description' => !empty($t_spec['description']) ? _schema_process_description($t_spec['description']) : '',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#attributes' => array(
          'id' => 'table-' . $t_name,
        ),
      );
      $build[$t_name]['content'] = array(
        '#theme' => 'table',
        '#header' => $row_hdrs,
        '#rows' => $rows,
      );
    }
    return $build;
  }
  public function schema_inspect() {
    $build = array();
    $schema = schema_get_schema(TRUE);
    $inspect = schema_dbobject()
      ->inspect();
    foreach ($inspect as $name => $table) {
      $module = isset($schema[$name]['module']) ? $schema[$name]['module'] : 'Unknown';
      if (!isset($build[$module])) {
        $build[$module] = array(
          '#type' => 'details',
          '#title' => $module,
        );
      }
      $build[$module][$name] = array(
        '#type' => 'textarea',
        '#rows' => 10,
        '#value' => schema_phpprint_table($name, $table),
        '#attributes' => array(
          'style' => 'width:100%;',
        ),
      );
    }

    // Sort by keys, i.e. the module name, then insert weights respectively.
    ksort($build);
    $i = 0;
    array_map(function ($v) use ($i) {
      if ($v['#title'] == 'Unknown') {
        $weight = -50;
      }
      else {
        $weight = $i++;
      }
      $v['#weight'] = $weight;
    }, $build);
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
DefaultController::schema_compare public function
DefaultController::schema_describe public function
DefaultController::schema_inspect public function
DefaultController::schema_show public function
DefaultController::schema_sql public function
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.