webhooks-8.x-1.x-dev/modules/webhook/src/Controller/WebhookAutocompleteController.php

modules/webhook/src/Controller/WebhookAutocompleteController.php
<?php

declare(strict_types=1);

namespace Drupal\webhook\Controller;

use Adbar\Dot;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Returns responses for Webhook routes.
 */
final class WebhookAutocompleteController extends ControllerBase {

  /**
   * The current query string.
   *
   * @var string
   */
  protected string $q;

  /**
   * The controller constructor.
   */
  public function __construct(
    private readonly RouteMatchInterface $routeMatch,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): self {
    return new self(
      $container->get('current_route_match'),
    );
  }

  /**
   * Builds the response.
   */
  public function __invoke(Request $request): JsonResponse {
    $this->q = $request->query->get('q');
    $data = ['headers' => [' ' => ' '], 'payload' => [' ' => ' ']];

    /** @var \Drupal\webhook\Entity\WebhookType $webhook_type */
    $webhook_type = $this->routeMatch
      ->getParameter('webhook_type');

    $entity_query = $this->entityTypeManager()->getStorage('webhook')
      ->getQuery();
    $entity_query
      ->condition('bundle', $webhook_type)
      ->accessCheck(FALSE)
      ->sort('created', 'DESC')
      ->range(0, 1);
    $ids = $entity_query->execute();

    /** @var \Drupal\webhook\Entity\Webhook */
    $entity = $this->entityTypeManager()
      ->getStorage('webhook')
      ->load(reset($ids));
    if ($entity) {
      $data['headers'] = $entity->getHeaders();
      $data['payload'] = $entity->getPayload();
    }

    $dot = new Dot($data);
    $data = array_keys($dot->flatten());
    $reduced = static::reduce($data, $this->q);
    $highlighted = static::highlight($reduced, $this->q);
    $data = static::restructure($highlighted, $reduced);
    return new JsonResponse($data);
  }

  /**
   * Restructures an array.
   */
  public static function restructure(array $labels, array $values): array {
    $restructured = [];
    foreach ($labels as $index => $label) {
      $restructured[] = [
        'label' => $label,
        'value' => $values[$index],
      ];
    }
    return $restructured;
  }

  /**
   * Reduces an array.
   */
  public static function reduce(array $array, string $needle): array {
    $reduced = [];
    foreach ($array as $value) {
      if (str_starts_with($value, $needle)) {
        $reduced[] = $value;
      }
    }
    return $reduced;
  }

  /**
   * Highlights elements of an array.
   */
  public static function highlight(array $array, string $highlight): array {
    $highlighted = [];
    foreach ($array as $value) {
      $pos = strpos($value, $highlight);
      if ($pos !== FALSE) {
        $value = substr_replace($value, '<b>' . $highlight . '</b>', $pos, strlen($highlight));
      }
      $highlighted[] = $value;
    }
    return $highlighted;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc