nortmas
10/11/2017 - 11:34 AM

Toolbar item.

<?php

/**
 * Implements hook_toolbar().
 */
function acc_assistant_toolbar() {

  $env = acc_get_environment();

  if ($env == 'dev' || $env == 'local') return [];

  $link = [
    '#type' => 'link',
    '#title' => t('Release: @git', array('@git' => acc_get_git_state($env))),
    '#url' => Url::fromRoute('<nolink>', []),
    '#attributes' => [
      'style' => 'display: block; padding-left: 10px; background-color: inherit; cursor: inherit; color: #fc0; font-weight: 100;'
    ],
  ];

  return [
    'git_info' => [
      '#type' => 'toolbar_item',
      'tab' => $link,
      '#weight' => 999999,
      '#cache' => [
        'max-age' => 0,
      ]
    ],
  ];
}

/**
 * Get current state of git.
 *
 * @param $env string with current environment
 *
 * @return string , can be local, dev, test, prod
 */
function acc_get_git_state($env) {
  if ($env == 'dev' || $env == 'test' || $env == 'prod') {
    $git = shell_exec("drush @accuchekde." . $env . " ac-environment-info accuchekde " . $env . " | grep vcs_path | awk  -F'/' '{print $2}'");
  }
  else {
    $git = shell_exec("describe --tags --abbrev=0");

    if ($git === NULL) {
      $git = shell_exec("git log -1 --pretty=format:'%s (%ci)' --abbrev-commit `git merge-base local-dev dev`");
    };
  }
  return $git;
}

/**
 * Get a current environment name.
 *
 * @return string, can be local, dev, test, prod
 */
function acc_get_environment() {
  if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
    return $_ENV['AH_SITE_ENVIRONMENT'];
  }
  else {
    return 'local';
  }
}