You are here

public function PackageController::page in Composer Manager 8

Shows the status of all required packages.

Return value

array Returns a render array as expected by drupal_render().

1 string reference to 'PackageController::page'
composer_manager.routing.yml in ./composer_manager.routing.yml
composer_manager.routing.yml

File

src/Controller/PackageController.php, line 75

Class

PackageController
Controller for displaying the list of required packages.

Namespace

Drupal\composer_manager\Controller

Code

public function page() {
  if (!composer_manager_initialized()) {
    $path = drupal_get_path('module', 'composer_manager');
    $message = t("Composer Manager needs to be initialized before usage. Run <code>php %path/scripts/init.php</code> from the command line.", [
      '%path' => $path,
    ]);
    drupal_set_message($message, 'warning');
    return [];
  }
  try {
    $packages = $this->packageManager
      ->getRequiredPackages();
  } catch (\RuntimeException $e) {
    drupal_set_message(Xss::filterAdmin($e
      ->getMessage()), 'error');
    return [];
  }
  $rows = [];
  foreach ($packages as $package_name => $package) {
    $package_column = [];
    if (!empty($package['homepage'])) {
      $package_column[] = [
        '#type' => 'link',
        '#title' => $package_name,
        '#url' => Url::fromUri($package['homepage']),
        '#options' => [
          'attributes' => [
            'target' => '_blank',
          ],
        ],
      ];
    }
    else {
      $package_column[] = [
        '#plain_text' => $package_name,
      ];
    }
    if (!empty($package['description'])) {
      $package_column[] = [
        '#prefix' => '<div class="description">',
        '#plain_text' => $package['description'],
        '#suffix' => '</div>',
      ];
    }

    // Prepare the installed and required versions.
    $installed_version = $package['version'] ? $package['version'] : $this
      ->t('Not installed');
    $required_version = $this
      ->buildRequiredVersion($package['constraint'], $package['required_by']);

    // Prepare the row classes.
    $class = [];
    if (empty($package['version'])) {
      $class[] = 'error';
    }
    elseif (empty($package['required_by'])) {
      $class[] = 'warning';
    }
    $rows[$package_name] = [
      'class' => $class,
      'data' => [
        'package' => [
          'data' => $package_column,
        ],
        'installed_version' => $installed_version,
        'required_version' => [
          'data' => $required_version,
        ],
      ],
    ];
  }
  $build = [];
  $build['packages'] = [
    '#theme' => 'table',
    '#header' => [
      'package' => $this
        ->t('Package'),
      'installed_version' => $this
        ->t('Installed Version'),
      'required_version' => $this
        ->t('Required Version'),
    ],
    '#rows' => $rows,
    '#caption' => $this
      ->t('Status of Packages Managed by Composer'),
    '#attributes' => [
      'class' => [
        'system-status-report',
      ],
    ],
  ];

  // Display any errors returned by hook_requirements().
  $this->moduleHandler
    ->loadInclude('composer_manager', 'install');
  $requirements = composer_manager_requirements('runtime');
  if ($requirements['composer_manager']['severity'] == REQUIREMENT_ERROR) {
    drupal_set_message($requirements['composer_manager']['description'], 'warning');
  }
  return $build;
}