Commit 431af8d7 authored by Fran Garcia-Linares's avatar Fran Garcia-Linares Committed by Tim Plunkett
Browse files

Issue #3280176 by fjgarlin, tim.plunkett, srishtiiee, chrisfromredfin, run...

Issue #3280176 by fjgarlin, tim.plunkett, srishtiiee, chrisfromredfin, run fast think slow -lets4-, bsnodgrass: Decouple front-end (Svelte) from "mockapi" to allow other plugins
parent 4323bef4
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
project_browser.api_get_development_status:
  path: '/drupal-org-proxy/development_status'
  methods: [GET]
  defaults:
    _controller: '\Drupal\project_browser\Controller\ProjectBrowserEndpointController::getAllDevelopmentStatus'
    _title: 'Get all development status values'
  requirements:
    _permission: 'administer modules'
  #options:
  #  no_cache: 'TRUE'
project_browser.api_get_maintenance_status:
  path: '/drupal-org-proxy/maintenance_status'
  methods: [GET]
  defaults:
    _controller: '\Drupal\project_browser\Controller\ProjectBrowserEndpointController::getAllMaintenanceStatus'
    _title: 'Get all maintenance status values'
  requirements:
    _permission: 'administer modules'
  #options:
  #  no_cache: 'TRUE'
project_browser.api_get_security_coverage:
  path: '/drupal-org-proxy/security_coverage'
  methods: [GET]
  defaults:
    _controller: '\Drupal\project_browser\Controller\ProjectBrowserEndpointController::getAllSecurityCoverage'
    _title: 'Get all security coverage values'
  requirements:
    _permission: 'administer modules'
  #options:
  #  no_cache: 'TRUE'
project_browser.api_get_categories:
  path: '/drupal-org-proxy/categories'
  methods: [GET]
+93 −4
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\InfoParserException;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\project_browser\EnabledSourceHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

@@ -38,6 +39,20 @@ class BrowserController extends ControllerBase {
   */
  protected $requestStack;

  /**
   * The EnabledSourceHandler.
   *
   * @var \Drupal\project_browser\EnabledSourceHandler
   */
  protected $enabledSource;

  /**
   * ProjectBrowser cache bin.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBin;

  /**
   * Build the project browser controller.
   *
@@ -47,11 +62,15 @@ class BrowserController extends ControllerBase {
   *   The module extension list.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\project_browser\EnabledSourceHandler $enabled_source
   *   The enabled source.
   */
  public function __construct(ModuleHandlerInterface $module_handler, ModuleExtensionList $module_list, RequestStack $request_stack) {
  public function __construct(ModuleHandlerInterface $module_handler, ModuleExtensionList $module_list, RequestStack $request_stack, EnabledSourceHandler $enabled_source) {
    $this->moduleHandler = $module_handler;
    $this->moduleList = $module_list;
    $this->requestStack = $request_stack;
    $this->enabledSource = $enabled_source;
    $this->cacheBin = $this->cache('project_browser');
  }

  /**
@@ -61,7 +80,8 @@ class BrowserController extends ControllerBase {
    return new static(
      $container->get('module_handler'),
      $container->get('extension.list.module'),
        $container->get('request_stack')
      $container->get('request_stack'),
      $container->get('project_browser.enabled_source'),
    );
  }

@@ -88,10 +108,79 @@ class BrowserController extends ControllerBase {
            'drupal_core_compatibility' => \Drupal::CORE_COMPATIBILITY,
            'module_path' => $this->moduleHandler->getModule('project_browser')->getPath(),
            'origin_url' => $request->getSchemeAndHttpHost() . $request->getBaseUrl(),
            'special_ids' => $this->getSpecialIds(),
          ],
        ],
      ],
    ];
  }

  /**
   * Return special IDs for some vocabularies.
   *
   * @return array
   *   List of special IDs per vocabulary.
   */
  protected function getSpecialIds(): array {
    // All rendered labels could change at any time in the back-end.
    // For now, we assume that these strings will remain the same, regardless
    // of the plugin, as that has been the case for years but bear in mind that
    // the front-end might NOT preselect these entries if the labels ever
    // change.
    $special_ids = [
      'maintenance_status' => [
        'id' => '-1',
        'name' => 'Actively maintained',
      ],
      'security_coverage' => [
        'id' => '-1',
        'name' => 'Covered',
      ],
    ];

    $maintenance_status_values = $this->getDataFromPlugin('maintenance_status', 'getMaintenanceStatuses');
    foreach ($maintenance_status_values as $item) {
      if ($item['name'] == $special_ids['maintenance_status']['name']) {
        $special_ids['maintenance_status']['id'] = $item['id'];
      }
    }

    $security_coverage_values = $this->getDataFromPlugin('security_coverage', 'getSecurityCoverages');
    foreach ($security_coverage_values as $item) {
      if ($item['name'] == $special_ids['security_coverage']['name']) {
        $special_ids['security_coverage']['id'] = $item['id'];
      }
    }

    return $special_ids;
  }

  /**
   * Gets data from the enabled plugin.
   *
   * @param string $cache_key
   *   Cache key to check if available, otherwise set.
   * @param string $method
   *   Plugin method to use if the data is not cached.
   *
   * @return array
   *   Resulting information.
   */
  protected function getDataFromPlugin($cache_key, $method): array {
    $data = [];
    $current_source = $this->enabledSource->getCurrentSource();
    if ($current_source) {
      $cache_key = "project_browser:$cache_key";
      if ($values = $this->cacheBin->get($cache_key)) {
        $data = $values->data;
      }
      elseif (method_exists($current_source, $method)) {
        $data = $current_source->$method();
        $this->cacheBin->set($cache_key, $data);
      }
    }

    return $data;
  }

  /**
+72 −0
Original line number Diff line number Diff line
@@ -185,4 +185,76 @@ class ProjectBrowserEndpointController extends ControllerBase {
    return new JsonResponse($categories);
  }

  /**
   * Returns a list of development status values.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   */
  public function getAllDevelopmentStatus(Request $request) {
    $current_source = $this->enabledSource->getCurrentSource();
    if (!$current_source) {
      return new JsonResponse([], Response::HTTP_ACCEPTED);
    }

    $cache_key = 'project_browser:development_status';
    if ($values = $this->cacheBin->get($cache_key)) {
      $values = $values->data;
    }
    else {
      $values = $current_source->getDevelopmentStatuses();
      $this->cacheBin->set($cache_key, $values);
    }

    return new JsonResponse($values);
  }

  /**
   * Returns a list of maintenance status values.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   */
  public function getAllMaintenanceStatus(Request $request) {
    $current_source = $this->enabledSource->getCurrentSource();
    if (!$current_source) {
      return new JsonResponse([], Response::HTTP_ACCEPTED);
    }

    $cache_key = 'project_browser:maintenance_status';
    if ($values = $this->cacheBin->get($cache_key)) {
      $values = $values->data;
    }
    else {
      $values = $current_source->getMaintenanceStatuses();
      $this->cacheBin->set($cache_key, $values);
    }

    return new JsonResponse($values);
  }

  /**
   * Returns a list of security coverage values.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   */
  public function getAllSecurityCoverage(Request $request) {
    $current_source = $this->enabledSource->getCurrentSource();
    if (!$current_source) {
      return new JsonResponse([], Response::HTTP_ACCEPTED);
    }

    $cache_key = 'project_browser:security_coverage';
    if ($values = $this->cacheBin->get($cache_key)) {
      $values = $values->data;
    }
    else {
      $values = $current_source->getSecurityCoverages();
      $this->cacheBin->set($cache_key, $values);
    }

    return new JsonResponse($values);
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -6,12 +6,12 @@ use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\Xss;

/**
 * Represents a single project from Drupal.org.
 * Represents a single project as it will be consumed by the front-end.
 */
class DrupalOrgProject {

  /**
   * Public properties. These are the properties that are exposed to JS.
   * Public properties.
   */
  public $author = [];

+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace Drupal\project_browser\DrupalOrg;
use Composer\Semver\Semver;

/**
 * Represents a single release from Drupal.org.
 * Represents a single release as it will be consumed by the front-end.
 */
class DrupalOrgRelease {

Loading