Commit 743c279e authored by Antonín Slejška's avatar Antonín Slejška
Browse files

Add the OpenSearchDescriptionXmlService.

parent 588d5bb1
Loading
Loading
Loading
Loading
+4 −0
Changes for opensearchtab.services.yml: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
services:
  opensearchtab.xml:
    class: Drupal\opensearchtab\Services\OpenSearchDescriptionXmlService
    arguments: ["@config.factory", '@request_stack']
+13 −98
Changes for src/Controller/OpenSearchDescriptionController.php: 13 added lines, 98 removed lines.
Original line number Diff line number Diff line
@@ -4,9 +4,9 @@ namespace Drupal\opensearchtab\Controller;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\opensearchtab\Services\OpenSearchDescriptionXmlService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

/**
 * OpenSearchDescriptionController .
@@ -14,44 +14,28 @@ use Symfony\Component\HttpFoundation\Request;
class OpenSearchDescriptionController extends ControllerBase {

  /**
   * System configurations.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $systemConfig;

  /**
   * Open Search Tab configurations.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $ostConfig;

  /**
   * A request object.
   * A response object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   * @var \Symfony\Component\HttpFoundation\Response
   */
  protected $request;
  protected $response;

  /**
   * A response object.
   * Open Search Description XML.
   *
   * @var \Symfony\Component\HttpFoundation\Response
   * @var \Drupal\opensearchtab\Services\OpenSearchDescriptionXmlService
   */
  protected $response;
  protected $openSearchDefinitionXml;

  /**
   * Class constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\opensearchtab\Services\OpenSearchDescriptionXmlService $open_search_definition_xml
   *   Open Search Description XML.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->systemConfig = $config_factory->get('system.site');
    $this->ostConfig = $config_factory->get('opensearchtab.settings');
    $this->request = new Request();
  public function __construct(OpenSearchDescriptionXmlService $open_search_definition_xml) {
    $this->response = new Response();
    $this->openSearchDefinitionXml = $open_search_definition_xml;
  }

  /**
@@ -59,7 +43,7 @@ class OpenSearchDescriptionController extends ControllerBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory')
      $container->get('opensearchtab.xml')
    );
  }

@@ -67,77 +51,8 @@ class OpenSearchDescriptionController extends ControllerBase {
   * {@inheritdoc}
   */
  public function content() {
    // Prepare variables for the XML content.
    $short_name = !empty($this->ostConfig->get('shortname')) ?
      $this->ostConfig->get('shortname') :
      (!empty($this->systemConfig->get('name')) ?
        $this->systemConfig->get('name') : 'Drupal site');
    $description = !empty($this->ostConfig->get('description')) ?
      $this->ostConfig->get('description') :
      (!empty($this->systemConfig->get('slogan')) ?
        $this->systemConfig->get('slogan') : 'Search the site');
    $search_form_path = !empty($this->ostConfig->get('search-form-path')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('search-form-path') :
      $this->request->getSchemeAndHttpHost() . '/search/node';
    $search_path = !empty($this->ostConfig->get('search-path')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('search-path') :
      $this->request->getSchemeAndHttpHost() .
      '/search/node?keys={searchTerms}';
    $suggestions_path_json = !empty($this->ostConfig->get('suggestions-path-json')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('suggestions-path-json') : '';
    $suggestions_path_xml = !empty($this->ostConfig->get('suggestions-path-xml')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('suggestions-path-xml') : '';
    $favicon_path = !empty($this->ostConfig->get('favicon-path')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('favicon-path') : '';

    // Define the XML content.
    $openTag = new \SimpleXMLElement(
      '<OpenSearchDescription></OpenSearchDescription>'
    );
    $openTag->addAttribute(
      'xmlns', 'http://a9.com/-/spec/opensearch/1.1/'
    );
    $openTag->addAttribute(
      'xmlns:xmlns:moz',
      'http://www.mozilla.org/2006/browser/search/'
    );
    $openTag->addChild('ShortName', $short_name);
    $openTag->addChild('Description', $description);
    $searchUrl = $openTag->addChild('Url');
    $searchUrl->addAttribute('type', 'text/html');
    $searchUrl->addAttribute('method', 'get');
    $searchUrl->addAttribute('template', $search_path);
    if ($suggestions_path_json !== '') {
      $suggestionsUrl = $openTag->addChild('Url');
      $suggestionsUrl->addAttribute(
        'type', 'application/x-suggestions+json'
      );
      $suggestionsUrl->addAttribute('method', 'get');
      $suggestionsUrl->addAttribute('template', $suggestions_path_json);
    }
    if ($suggestions_path_xml !== '') {
      $suggestionsUrl = $openTag->addChild('Url');
      $suggestionsUrl->addAttribute(
        'type', 'application/x-suggestions+xml'
      );
      $suggestionsUrl->addAttribute('method', 'get');
      $suggestionsUrl->addAttribute('template', $suggestions_path_xml);
    }
    if ($favicon_path !== '') {
      $faviconUrl = $openTag->addChild('Image', $favicon_path);
      $faviconUrl->addAttribute('height', '16');
      $faviconUrl->addAttribute('width', '16');
      $faviconUrl->addAttribute('type', 'image/x-icon');
    }
    $openTag->addChild('xmlns:moz:SearchForm', $search_form_path);

    // Prepare the response.
    $this->response->setContent($openTag->asXML());
    $this->response->setContent($this->openSearchDefinitionXml->getXml());
    $this->response->headers->set('Content-type', 'text/xml');

    return $this->response;
+147 −0
Changes for src/Services/OpenSearchDescriptionXmlService.php: 147 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains Drupal\opensearchtab\OpenSearchDescriptionXml.
 */

namespace Drupal\opensearchtab\Services;

use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Class OpenSearchDescriptionXmlService.
 *
 * @package Drupal\opensearchtab
 */
class OpenSearchDescriptionXmlService {

  /**
   * System configurations.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $systemConfig;

  /**
   * Open Search Tab configurations.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $ostConfig;

  /**
   * A request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The current request.
   */
  public function __construct(ConfigFactory $config_factory, RequestStack $request_stack) {
    $this->systemConfig = $config_factory->get('system.site');
    $this->ostConfig = $config_factory->get('opensearchtab.settings');
    $this->request = $request_stack->getCurrentRequest();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('request_stack')
    );
  }

  /**
   * Generate the XML code from configs and the current request.
   */
  public function getXml() {
    // Prepare variables for the XML content.
    $short_name = !empty($this->ostConfig->get('shortname')) ?
      $this->ostConfig->get('shortname') :
      (!empty($this->systemConfig->get('name')) ?
        $this->systemConfig->get('name') : 'Drupal site');

    $description = !empty($this->ostConfig->get('description')) ?
      $this->ostConfig->get('description') :
      (!empty($this->systemConfig->get('slogan')) ?
        $this->systemConfig->get('slogan') : 'Search the site');

    $search_form_path = !empty($this->ostConfig->get('search-form-path')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('search-form-path') :
      $this->request->getSchemeAndHttpHost() . '/search/node';

    $search_path = !empty($this->ostConfig->get('search-path')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('search-path') :
      $this->request->getSchemeAndHttpHost() .
      '/search/node?keys={searchTerms}';

    $suggestions_path_json = !empty($this->ostConfig->get('suggestions-path-json')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('suggestions-path-json') : '';

    $suggestions_path_xml = !empty($this->ostConfig->get('suggestions-path-xml')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('suggestions-path-xml') : '';

    $favicon_path = !empty($this->ostConfig->get('favicon-path')) ?
      $this->request->getSchemeAndHttpHost() .
      $this->ostConfig->get('favicon-path') : '';

    // Define the XML content.
    $openTag = new \SimpleXMLElement(
      '<OpenSearchDescription></OpenSearchDescription>'
    );
    $openTag->addAttribute(
      'xmlns', 'http://a9.com/-/spec/opensearch/1.1/'
    );
    $openTag->addAttribute(
      'xmlns:xmlns:moz',
      'http://www.mozilla.org/2006/browser/search/'
    );
    $openTag->addChild('ShortName', $short_name);
    $openTag->addChild('Description', $description);
    $searchUrl = $openTag->addChild('Url');
    $searchUrl->addAttribute('type', 'text/html');
    $searchUrl->addAttribute('method', 'get');
    $searchUrl->addAttribute('template', $search_path);
    if ($suggestions_path_json !== '') {
      $suggestionsUrl = $openTag->addChild('Url');
      $suggestionsUrl->addAttribute(
        'type', 'application/x-suggestions+json'
      );
      $suggestionsUrl->addAttribute('method', 'get');
      $suggestionsUrl->addAttribute('template', $suggestions_path_json);
    }
    if ($suggestions_path_xml !== '') {
      $suggestionsUrl = $openTag->addChild('Url');
      $suggestionsUrl->addAttribute(
        'type', 'application/x-suggestions+xml'
      );
      $suggestionsUrl->addAttribute('method', 'get');
      $suggestionsUrl->addAttribute('template', $suggestions_path_xml);
    }
    if ($favicon_path !== '') {
      $faviconUrl = $openTag->addChild('Image', $favicon_path);
      $faviconUrl->addAttribute('height', '16');
      $faviconUrl->addAttribute('width', '16');
      $faviconUrl->addAttribute('type', 'image/x-icon');
    }
    $openTag->addChild('xmlns:moz:SearchForm', $search_form_path);

    return $openTag->asXML();
  }
}